source: examples/gc_no_raii/src/gcpointers.c @ 72828a8

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 72828a8 was bf71cfd, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Moved up many directories in source

  • Property mode set to 100644
File size: 3.1 KB
Line 
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
8void 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
35void 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
45void ?{}(gcpointer_t* this)
46{
47        this->ptr = (intptr_t)NULL;
48        this->next = NULL;
49}
50
51void ?{}(gcpointer_t* this, void* address)
52{
53        this->ptr = (intptr_t)address;
54        this->next = NULL;
55
56        register_ptr(this);
57}
58
59void ?{}(gcpointer_t* this, gcpointer_t other)
60{
61        this->ptr = other.ptr;
62        this->next = NULL;
63
64        register_ptr(this);
65}
66
67void ^?{}(gcpointer_t* this)
68{
69        unregister_ptr(this);
70}
71
72gcpointer_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
82bool gcpointer_equal(const gcpointer_t* this, const gcpointer_t* rhs)
83{
84        return this->ptr == rhs->ptr;
85}
86
87bool gcpointer_not_equal(const gcpointer_t* this, const gcpointer_t* rhs)
88{
89        return this->ptr != rhs->ptr;
90}
91
92bool 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
115forall(otype T) void ?{}(gcpointer(T)* this) {
116        (&this->internal) {};
117}
118
119forall(otype T) void ?{}(gcpointer(T)* this, void* address) {
120        (&this->internal) { address };
121}
122
123forall(otype T) void ?{}(gcpointer(T)* this, gcpointer(T) other) {
124        (&this->internal) { other.internal };
125}
126
127forall(otype T) void ^?{}(gcpointer(T)* this) {
128        ^?{}(&this->internal);
129}
130
131forall(otype 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
138forall(otype T) T* get(gcpointer(T)* this) {
139        return (T*)this->internal.ptr;
140}
141//
142// //Logical operators
143forall(otype 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);
Note: See TracBrowser for help on using the repository browser.