[15db1ab] | 1 | #include "gcpointers.h" |
---|
| 2 | |
---|
[bee4283] | 3 | // #include "gc.h" |
---|
[e47f529] | 4 | #include "internal/collector.h" |
---|
[c5833e8] | 5 | #include "internal/object_header.h" |
---|
[29ad0ac] | 6 | #include "internal/state.h" |
---|
[15db1ab] | 7 | |
---|
[385c130] | 8 | void register_ptr(gcpointer_t* this) |
---|
| 9 | { |
---|
| 10 | if(gcpointer_null(this)) return; |
---|
| 11 | |
---|
| 12 | if(gc_is_managed(this)) |
---|
| 13 | { |
---|
| 14 | gc_object_header* obj = gc_get_object_for_ref(gc_get_state(), (void*)this); |
---|
| 15 | check(obj); |
---|
[46f1d20] | 16 | check(is_valid(obj)); |
---|
| 17 | check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || !obj->type_chain); |
---|
[385c130] | 18 | this->next = obj->type_chain; |
---|
| 19 | obj->type_chain = this; |
---|
[46f1d20] | 20 | check(is_valid(obj)); |
---|
[385c130] | 21 | } |
---|
| 22 | else |
---|
| 23 | { |
---|
| 24 | gc_object_header* obj = gc_get_object_ptr((void*)this->ptr); |
---|
| 25 | check(obj); |
---|
[46f1d20] | 26 | check(is_valid(obj)); |
---|
[76af36f] | 27 | check(!obj->root_chain || this->ptr == obj->root_chain->ptr); |
---|
| 28 | check(!obj->root_chain || gc_is_managed(this) == gc_is_managed(obj->root_chain)); |
---|
[385c130] | 29 | this->next = obj->root_chain; |
---|
| 30 | obj->root_chain = this; |
---|
[46f1d20] | 31 | check(is_valid(obj)); |
---|
[385c130] | 32 | } |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | void unregister_ptr(gcpointer_t* this) |
---|
| 36 | { |
---|
| 37 | if(gcpointer_null(this)) return; |
---|
| 38 | |
---|
| 39 | gcpointer_t** prev_next_ptr = gc_find_previous_ref(this); |
---|
| 40 | check((*prev_next_ptr) == this); |
---|
| 41 | |
---|
| 42 | (*prev_next_ptr) = this->next; |
---|
| 43 | } |
---|
| 44 | |
---|
[6643e72] | 45 | void ?{}(gcpointer_t* this) |
---|
[385c130] | 46 | { |
---|
| 47 | this->ptr = (intptr_t)NULL; |
---|
| 48 | this->next = NULL; |
---|
| 49 | } |
---|
| 50 | |
---|
[6643e72] | 51 | void ?{}(gcpointer_t* this, void* address) |
---|
[385c130] | 52 | { |
---|
| 53 | this->ptr = (intptr_t)address; |
---|
| 54 | this->next = NULL; |
---|
| 55 | |
---|
| 56 | register_ptr(this); |
---|
| 57 | } |
---|
| 58 | |
---|
[6643e72] | 59 | void ?{}(gcpointer_t* this, gcpointer_t other) |
---|
[385c130] | 60 | { |
---|
[6643e72] | 61 | this->ptr = other.ptr; |
---|
[385c130] | 62 | this->next = NULL; |
---|
| 63 | |
---|
| 64 | register_ptr(this); |
---|
| 65 | } |
---|
| 66 | |
---|
[6643e72] | 67 | void ^?{}(gcpointer_t* this) |
---|
[385c130] | 68 | { |
---|
| 69 | unregister_ptr(this); |
---|
| 70 | } |
---|
| 71 | |
---|
[76af36f] | 72 | gcpointer_t ?=?(gcpointer_t* this, gcpointer_t rhs) |
---|
[385c130] | 73 | { |
---|
[76af36f] | 74 | unregister_ptr(this); |
---|
| 75 | this->ptr = rhs.ptr; |
---|
| 76 | register_ptr(this); |
---|
[385c130] | 77 | |
---|
[76af36f] | 78 | return *this; |
---|
[385c130] | 79 | } |
---|
| 80 | |
---|
| 81 | //Logical operators |
---|
[4c1403c] | 82 | bool gcpointer_equal(const gcpointer_t* this, const gcpointer_t* rhs) |
---|
[385c130] | 83 | { |
---|
| 84 | return this->ptr == rhs->ptr; |
---|
| 85 | } |
---|
| 86 | |
---|
[4c1403c] | 87 | bool gcpointer_not_equal(const gcpointer_t* this, const gcpointer_t* rhs) |
---|
[385c130] | 88 | { |
---|
| 89 | return this->ptr != rhs->ptr; |
---|
| 90 | } |
---|
| 91 | |
---|
[4c1403c] | 92 | bool gcpointer_null(const gcpointer_t* this) |
---|
[385c130] | 93 | { |
---|
| 94 | return this->ptr == (intptr_t)NULL; |
---|
| 95 | } |
---|
[6643e72] | 96 | |
---|
[4c1403c] | 97 | #ifndef NDEBUG |
---|
| 98 | bool is_valid(const gcpointer_t* this) { |
---|
| 99 | if(gcpointer_null(this)) return true; |
---|
| 100 | |
---|
| 101 | gc_object_header* obj = gc_get_object_ptr((void*)this->ptr); |
---|
| 102 | check(obj); |
---|
| 103 | check(is_valid(obj)); |
---|
| 104 | check(!obj->root_chain || this->ptr == obj->root_chain->ptr); |
---|
| 105 | |
---|
| 106 | if( !gc_is_managed(this)) |
---|
| 107 | { |
---|
| 108 | check( !(this->next) || this->ptr == this->next->ptr ); |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | return true; |
---|
| 112 | } |
---|
| 113 | #endif |
---|
| 114 | |
---|
[6643e72] | 115 | forall(otype T) void ?{}(gcpointer(T)* this) { |
---|
| 116 | (&this->internal) {}; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | forall(otype T) void ?{}(gcpointer(T)* this, void* address) { |
---|
| 120 | (&this->internal) { address }; |
---|
| 121 | } |
---|
| 122 | |
---|
[76af36f] | 123 | forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T) other) { |
---|
| 124 | (&this->internal) { other.internal }; |
---|
[6643e72] | 125 | } |
---|
| 126 | |
---|
| 127 | forall(otype T) void ^?{}(gcpointer(T)* this) { |
---|
| 128 | ^?{}(&this->internal); |
---|
| 129 | } |
---|
| 130 | |
---|
[76af36f] | 131 | forall(otype T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs) { |
---|
| 132 | this->internal = rhs.internal; |
---|
| 133 | return *this; |
---|
| 134 | } |
---|
[6643e72] | 135 | // |
---|
| 136 | // forall(otype T) T *?(gcpointer(T) this); |
---|
| 137 | |
---|
| 138 | forall(otype T) T* get(gcpointer(T)* this) { |
---|
| 139 | return (T*)this->internal.ptr; |
---|
| 140 | } |
---|
| 141 | // |
---|
| 142 | // //Logical operators |
---|
[46f1d20] | 143 | forall(otype T) int ?!=?(gcpointer(T) this, int zero) { |
---|
| 144 | return this.internal.ptr != 0; |
---|
| 145 | } |
---|
[6643e72] | 146 | // forall(otype T) int ?!=?(gcpointer(T) this, gcpointer(T) rhs); |
---|
| 147 | // forall(otype T) int ?==?(gcpointer(T) this, gcpointer(T) rhs); |
---|