[15db1ab] | 1 | #include "gcpointers.h" |
---|
| 2 | |
---|
| 3 | #include "gc.h" |
---|
| 4 | |
---|
| 5 | void register_ptr(gcpointer_t* this) |
---|
| 6 | { |
---|
| 7 | if(gcpointer_null(this)) return; |
---|
| 8 | |
---|
| 9 | _Bool managed = gc_is_managed(this); |
---|
| 10 | |
---|
| 11 | if(managed) |
---|
| 12 | { |
---|
| 13 | gc_object_header* obj = gc_get_object_for_ref(this); |
---|
| 14 | check(obj); |
---|
| 15 | check(gc_obj_is_valide(obj)); |
---|
| 16 | check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || obj->type_chain == NULL); |
---|
| 17 | m_next = obj->type_chain; |
---|
| 18 | obj->type_chain = this; |
---|
| 19 | check(obj->is_valide()); |
---|
| 20 | } |
---|
| 21 | else |
---|
| 22 | { |
---|
| 23 | gc_object_header* obj = gc_get_object_ptr(this->ptr); |
---|
| 24 | check(obj); |
---|
| 25 | check(gc_obj_is_valide(obj)); |
---|
| 26 | check(gc_is_managed(this) == gc_is_managed(obj->root_chain) || obj->root_chain == NULL); |
---|
| 27 | m_next = obj->root_chain; |
---|
| 28 | obj->root_chain = this; |
---|
| 29 | check(gc_obj_is_valide(obj)); |
---|
| 30 | } |
---|
| 31 | } |
---|
| 32 | |
---|
| 33 | void unregister_ptr(gcpointer_t* this) |
---|
| 34 | { |
---|
| 35 | if(gcpointer_null(this)) return; |
---|
| 36 | |
---|
| 37 | gcpointer_t** prev_next_ptr = gc_find_previous_ref(this); |
---|
| 38 | check((*prev_next_ptr) == this); |
---|
| 39 | |
---|
| 40 | (*prev_next_ptr) = this->next; |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | void gcpointer_ctor(gcpointer_t* this) |
---|
| 44 | { |
---|
| 45 | this->ptr = NULL; |
---|
| 46 | this->next = NULL; |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | void gcpointer_ctor(gcpointer_t* this, void* address) |
---|
| 50 | { |
---|
| 51 | this->ptr = address; |
---|
| 52 | this->next = NULL; |
---|
| 53 | |
---|
| 54 | register_ptr(this); |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | void gcpointer_ctor(gcpointer_t* this, gcpointer_t* other) |
---|
| 58 | { |
---|
| 59 | this->ptr = other->ptr; |
---|
| 60 | this->next = NULL; |
---|
| 61 | |
---|
| 62 | register_ptr(this); |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | void gcpointer_dtor(gcpointer_t* this) |
---|
| 66 | { |
---|
| 67 | unregister_ptr(this); |
---|
| 68 | } |
---|
| 69 | |
---|
| 70 | gcpointer_t* gcpointer_assign(gcpointer_t* this, gcpointer_t* rhs) |
---|
| 71 | { |
---|
| 72 | if(this != rhs && this->ptr != rhs->ptr) |
---|
| 73 | { |
---|
| 74 | unregister_ptr(this); |
---|
| 75 | |
---|
| 76 | this->ptr = rhs->ptr; |
---|
| 77 | |
---|
| 78 | register_ptr(this); |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | return this; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | //Logical operators |
---|
| 85 | int gcpointer_equal(gcpointer_t* this, gcpointer_t* rhs) |
---|
| 86 | { |
---|
| 87 | return this->ptr == rhs->ptr; |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | int gcpointer_not_equal(gcpointer_t* this, gcpointer_t* rhs) |
---|
| 91 | { |
---|
| 92 | return this->ptr != rhs->ptr; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | int gcpointer_null(gcpointer_t* this) |
---|
| 96 | { |
---|
| 97 | return this->ptr == NULL; |
---|
| 98 | } |
---|