source: examples/gc_no_raii/src/internal/object_header.c @ 5569a31

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

Moved up many directories in source

  • Property mode set to 100644
File size: 2.4 KB
Line 
1#include "object_header.h"
2
3#include <stdint.h>
4
5#include "collector.h"
6#include "globals.h"
7#include "gcpointers.h"
8
9void ctor(gc_object_header* const this, size_t inSize)
10{
11        #ifndef NDEBUG
12                this->canary_start = CANARY_VALUE;
13        #endif
14
15        this->size = inSize;
16        this->root_chain = NULL;
17        this->type_chain = NULL;
18        this->forward = NULL;
19        this->is_forwarded = false;
20
21        #ifndef NDEBUG
22                this->canary_end = CANARY_VALUE;
23        #endif
24}
25
26void copy_ctor(gc_object_header* const this, const gc_object_header* const other)
27{
28        #ifndef NDEBUG
29                this->canary_start = CANARY_VALUE;
30        #endif
31
32        this->size = other->size;
33        this->root_chain = other->root_chain;
34        this->type_chain = NULL;
35        this->forward = NULL;
36        this->is_forwarded = false;
37
38        #ifndef NDEBUG
39                this->canary_end = CANARY_VALUE;
40        #endif
41
42        gcpointer_t* root = this->root_chain;
43        while(root)
44        {
45                check(gc_get_object_ptr( (void*)root->ptr ) == other);
46                root->ptr = ((intptr_t)this) + sizeof(gc_object_header);
47
48                check(gc_get_object_ptr( (void*)root->ptr ) == this);
49                root = root->next;
50        }
51
52        gcpointer_t* type = other->type_chain;
53
54        while(type)
55        {
56                check((intptr_t)type < (intptr_t)((intptr_t)other + other->size));
57
58                size_t offset = (intptr_t)type - (intptr_t)other;
59                check(offset < this->size);
60
61                gcpointer_t* member_ptr = (gcpointer_t*)( (intptr_t)this + offset );
62
63                if(!this->type_chain) this->type_chain = member_ptr;
64
65                size_t next_offset = type->next ? (intptr_t)type->next - (intptr_t)other : 0;
66                check(next_offset < this->size);
67
68                gcpointer_t* next_ptr = type->next ? (gcpointer_t*)((intptr_t)this + next_offset) : NULL;
69
70                member_ptr->ptr = type->ptr;
71                member_ptr->next = next_ptr;
72
73                type = type->next;
74        }
75
76        check(is_valid(this));
77}
78
79#ifndef NDEBUG
80        bool is_valid(const gc_object_header* const this)
81        {
82                check((intptr_t)this->canary_start == (intptr_t)CANARY_VALUE);
83                check((intptr_t)this->canary_end == (intptr_t)CANARY_VALUE);
84
85                check(this->is_forwarded == ( (intptr_t)this->forward != (intptr_t)NULL));
86
87                check(this->size < POOL_SIZE_BYTES);
88
89                gcpointer_t* root = this->root_chain;
90                while(root)
91                {
92                        checkf(gc_get_object_ptr( (void*)root->ptr ) == this, (const char*)"Expected %lX got %lX\n", gc_get_object_ptr( (void*)root->ptr ), this);
93
94                        root = root->next;
95                }
96
97                gcpointer_t* type = this->type_chain;
98                while(type)
99                {
100                        check((intptr_t)type > (intptr_t)this);
101                        check((intptr_t)type < (intptr_t)(((intptr_t)this) + this->size));
102
103                        type = type->next;
104                }
105
106                return true;
107        }
108        #else
109        #error blarg
110#endif
Note: See TracBrowser for help on using the repository browser.