| 1 | #include "gcpointers.h"
|
|---|
| 2 |
|
|---|
| 3 | // #include "gc.h"
|
|---|
| 4 | #include "internal/collector.h"
|
|---|
| 5 | #include "internal/object_header.h"
|
|---|
| 6 | #include "internal/state.h"
|
|---|
| 7 |
|
|---|
| 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);
|
|---|
| 16 | check(is_valid(obj));
|
|---|
| 17 | check(gc_is_managed(this) == gc_is_managed(obj->type_chain) || !obj->type_chain);
|
|---|
| 18 | this->next = obj->type_chain;
|
|---|
| 19 | obj->type_chain = this;
|
|---|
| 20 | check(is_valid(obj));
|
|---|
| 21 | }
|
|---|
| 22 | else
|
|---|
| 23 | {
|
|---|
| 24 | gc_object_header* obj = gc_get_object_ptr((void*)this->ptr);
|
|---|
| 25 | check(obj);
|
|---|
| 26 | check(is_valid(obj));
|
|---|
| 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));
|
|---|
| 29 | this->next = obj->root_chain;
|
|---|
| 30 | obj->root_chain = this;
|
|---|
| 31 | check(is_valid(obj));
|
|---|
| 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 |
|
|---|
| 45 | void ?{}(gcpointer_t* this)
|
|---|
| 46 | {
|
|---|
| 47 | this->ptr = (intptr_t)NULL;
|
|---|
| 48 | this->next = NULL;
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | void ?{}(gcpointer_t* this, void* address)
|
|---|
| 52 | {
|
|---|
| 53 | this->ptr = (intptr_t)address;
|
|---|
| 54 | this->next = NULL;
|
|---|
| 55 |
|
|---|
| 56 | register_ptr(this);
|
|---|
| 57 | }
|
|---|
| 58 |
|
|---|
| 59 | void ?{}(gcpointer_t* this, gcpointer_t other)
|
|---|
| 60 | {
|
|---|
| 61 | this->ptr = other.ptr;
|
|---|
| 62 | this->next = NULL;
|
|---|
| 63 |
|
|---|
| 64 | register_ptr(this);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void ^?{}(gcpointer_t* this)
|
|---|
| 68 | {
|
|---|
| 69 | unregister_ptr(this);
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | gcpointer_t ?=?(gcpointer_t* this, gcpointer_t rhs)
|
|---|
| 73 | {
|
|---|
| 74 | unregister_ptr(this);
|
|---|
| 75 | this->ptr = rhs.ptr;
|
|---|
| 76 | register_ptr(this);
|
|---|
| 77 |
|
|---|
| 78 | return *this;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | //Logical operators
|
|---|
| 82 | bool gcpointer_equal(const gcpointer_t* this, const gcpointer_t* rhs)
|
|---|
| 83 | {
|
|---|
| 84 | return this->ptr == rhs->ptr;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | bool gcpointer_not_equal(const gcpointer_t* this, const gcpointer_t* rhs)
|
|---|
| 88 | {
|
|---|
| 89 | return this->ptr != rhs->ptr;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | bool gcpointer_null(const gcpointer_t* this)
|
|---|
| 93 | {
|
|---|
| 94 | return this->ptr == (intptr_t)NULL;
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 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 |
|
|---|
| 115 | forall(T) void ?{}(gcpointer(T)* this) {
|
|---|
| 116 | (&this->internal) {};
|
|---|
| 117 | }
|
|---|
| 118 |
|
|---|
| 119 | forall(T) void ?{}(gcpointer(T)* this, void* address) {
|
|---|
| 120 | (&this->internal) { address };
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | forall(T) void ?{}(gcpointer(T)* this, gcpointer(T) other) {
|
|---|
| 124 | (&this->internal) { other.internal };
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | forall(T) void ^?{}(gcpointer(T)* this) {
|
|---|
| 128 | ^?{}(&this->internal);
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | forall(T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs) {
|
|---|
| 132 | this->internal = rhs.internal;
|
|---|
| 133 | return *this;
|
|---|
| 134 | }
|
|---|
| 135 | //
|
|---|
| 136 | // forall(otype T) T *?(gcpointer(T) this);
|
|---|
| 137 |
|
|---|
| 138 | forall(T) T* get(gcpointer(T)* this) {
|
|---|
| 139 | return (T*)this->internal.ptr;
|
|---|
| 140 | }
|
|---|
| 141 | //
|
|---|
| 142 | // //Logical operators
|
|---|
| 143 | forall(T) int ?!=?(gcpointer(T) this, int zero) {
|
|---|
| 144 | return this.internal.ptr != 0;
|
|---|
| 145 | }
|
|---|
| 146 | // forall(otype T) int ?!=?(gcpointer(T) this, gcpointer(T) rhs);
|
|---|
| 147 | // forall(otype T) int ?==?(gcpointer(T) this, gcpointer(T) rhs);
|
|---|