source: tests/zombies/gc_no_raii/src/gcpointers.c @ 9939dc3

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since 9939dc3 was fd54fef, checked in by Michael Brooks <mlbrooks@…>, 4 years ago

Converting the project to use the new syntax for otype, dtype and ttytpe.

Changed prelude (gen), libcfa and test suite to use it. Added a simple deprecation rule of the old syntax to the parser; we might wish to support both syntaxes "officially," like with an extra CLI switch, but this measure should serve as a simple reminder for our team to try the new syntax.

  • Property mode set to 100644
File size: 3.0 KB
RevLine 
[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]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);
[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
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
[6643e72]45void ?{}(gcpointer_t* this)
[385c130]46{
47        this->ptr = (intptr_t)NULL;
48        this->next = NULL;
49}
50
[6643e72]51void ?{}(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]59void ?{}(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]67void ^?{}(gcpointer_t* this)
[385c130]68{
69        unregister_ptr(this);
70}
71
[76af36f]72gcpointer_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]82bool gcpointer_equal(const gcpointer_t* this, const gcpointer_t* rhs)
[385c130]83{
84        return this->ptr == rhs->ptr;
85}
86
[4c1403c]87bool gcpointer_not_equal(const gcpointer_t* this, const gcpointer_t* rhs)
[385c130]88{
89        return this->ptr != rhs->ptr;
90}
91
[4c1403c]92bool 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
[fd54fef]115forall(T) void ?{}(gcpointer(T)* this) {
[6643e72]116        (&this->internal) {};
117}
118
[fd54fef]119forall(T) void ?{}(gcpointer(T)* this, void* address) {
[6643e72]120        (&this->internal) { address };
121}
122
[fd54fef]123forall(T) void ?{}(gcpointer(T)* this, gcpointer(T) other) {
[76af36f]124        (&this->internal) { other.internal };
[6643e72]125}
126
[fd54fef]127forall(T) void ^?{}(gcpointer(T)* this) {
[6643e72]128        ^?{}(&this->internal);
129}
130
[fd54fef]131forall(T) gcpointer(T) ?=?(gcpointer(T)* this, gcpointer(T) rhs) {
[76af36f]132        this->internal = rhs.internal;
133        return *this;
134}
[6643e72]135//
136// forall(otype T) T *?(gcpointer(T) this);
137
[fd54fef]138forall(T) T* get(gcpointer(T)* this) {
[6643e72]139        return (T*)this->internal.ptr;
140}
141//
142// //Logical operators
[fd54fef]143forall(T) int ?!=?(gcpointer(T) this, int zero) {
[46f1d20]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);
Note: See TracBrowser for help on using the repository browser.