source: examples/gc_no_raii/src/internal/object_header.c @ e53e5aa

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since e53e5aa 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
RevLine 
[df4aea7]1#include "object_header.h"
2
3#include <stdint.h>
4
[46f1d20]5#include "collector.h"
[df4aea7]6#include "globals.h"
7#include "gcpointers.h"
8
[16cfd8c]9void ctor(gc_object_header* const this, size_t inSize)
[df4aea7]10{
[46f1d20]11        #ifndef NDEBUG
[df4aea7]12                this->canary_start = CANARY_VALUE;
13        #endif
[16cfd8c]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
[46f1d20]21        #ifndef NDEBUG
[16cfd8c]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{
[46f1d20]28        #ifndef NDEBUG
[16cfd8c]29                this->canary_start = CANARY_VALUE;
30        #endif
31
[df4aea7]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;
[16cfd8c]37
[46f1d20]38        #ifndef NDEBUG
[df4aea7]39                this->canary_end = CANARY_VALUE;
40        #endif
41
[16cfd8c]42        gcpointer_t* root = this->root_chain;
43        while(root)
44        {
[46f1d20]45                check(gc_get_object_ptr( (void*)root->ptr ) == other);
[16cfd8c]46                root->ptr = ((intptr_t)this) + sizeof(gc_object_header);
[df4aea7]47
[46f1d20]48                check(gc_get_object_ptr( (void*)root->ptr ) == this);
[16cfd8c]49                root = root->next;
50        }
[df4aea7]51
[16cfd8c]52        gcpointer_t* type = other->type_chain;
[df4aea7]53
[16cfd8c]54        while(type)
55        {
56                check((intptr_t)type < (intptr_t)((intptr_t)other + other->size));
[df4aea7]57
[16cfd8c]58                size_t offset = (intptr_t)type - (intptr_t)other;
[46f1d20]59                check(offset < this->size);
[df4aea7]60
[16cfd8c]61                gcpointer_t* member_ptr = (gcpointer_t*)( (intptr_t)this + offset );
[df4aea7]62
[16cfd8c]63                if(!this->type_chain) this->type_chain = member_ptr;
[df4aea7]64
[16cfd8c]65                size_t next_offset = type->next ? (intptr_t)type->next - (intptr_t)other : 0;
[46f1d20]66                check(next_offset < this->size);
[df4aea7]67
[16cfd8c]68                gcpointer_t* next_ptr = type->next ? (gcpointer_t*)((intptr_t)this + next_offset) : NULL;
[df4aea7]69
[16cfd8c]70                member_ptr->ptr = type->ptr;
71                member_ptr->next = next_ptr;
[df4aea7]72
[16cfd8c]73                type = type->next;
[df4aea7]74        }
[16cfd8c]75
[46f1d20]76        check(is_valid(this));
[df4aea7]77}
78
[46f1d20]79#ifndef NDEBUG
80        bool is_valid(const gc_object_header* const this)
[df4aea7]81        {
[46f1d20]82                check((intptr_t)this->canary_start == (intptr_t)CANARY_VALUE);
83                check((intptr_t)this->canary_end == (intptr_t)CANARY_VALUE);
[df4aea7]84
[46f1d20]85                check(this->is_forwarded == ( (intptr_t)this->forward != (intptr_t)NULL));
[df4aea7]86
87                check(this->size < POOL_SIZE_BYTES);
88
89                gcpointer_t* root = this->root_chain;
90                while(root)
91                {
[76af36f]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);
[df4aea7]93
94                        root = root->next;
95                }
96
[46f1d20]97                gcpointer_t* type = this->type_chain;
[df4aea7]98                while(type)
99                {
100                        check((intptr_t)type > (intptr_t)this);
[46f1d20]101                        check((intptr_t)type < (intptr_t)(((intptr_t)this) + this->size));
[df4aea7]102
103                        type = type->next;
104                }
105
106                return true;
107        }
[76af36f]108        #else
109        #error blarg
[df4aea7]110#endif
Note: See TracBrowser for help on using the repository browser.