source: src/examples/gc_no_raii/src/internal/memory_pool.c @ bee4283

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since bee4283 was 4ef8fb3, checked in by Thierry Delisle <tdelisle@…>, 8 years ago

fixed compilation of garbage collector

  • Property mode set to 100644
File size: 3.4 KB
Line 
1#include "memory_pool.h"
2
3#include <stdlib>
4
5#include "object_header.h"
6
7const size_t gc_pool_header_size = (size_t)(  &(((gc_memory_pool*)NULL)->start_p) );
8
9void ctor(gc_memory_pool *const this, size_t size, gc_memory_pool* next, gc_memory_pool* mirror, uint8_t type)
10{
11        this->mirror = mirror;
12        this->next = next;
13        this->type_code = type;
14
15        card_table_t* new = (card_table_t*)malloc(sizeof(card_table_t));
16        this->cards = new;
17        ctor(this->cards);
18
19        this->end_p = ((uint8_t*)this) + size;
20        this->free_p = this->start_p;
21
22        check(gc_pool_of(this) == this);
23        check(this->cards);
24        gc_reset_pool(this);
25}
26
27void dtor(gc_memory_pool *const this)
28{
29        dtor(this->cards);
30        free(this->cards);
31}
32
33void gc_reset_pool(gc_memory_pool *const this)
34{
35        this->free_p = this->start_p;
36        #if _DEBUG
37                memset(this->start_p, 0xCD, gc_pool_total_size(this));
38        #endif
39
40        check(this->cards);
41        reset(this->cards);
42
43        check(gc_pool_size_left(this) == gc_pool_total_size(this));
44}
45
46void* gc_pool_allocate(gc_memory_pool *const this, size_t size, bool zero)
47{
48        void* ret = this->free_p;
49
50        this->free_p += size;
51
52        if (zero) memset(ret, 0x00, size);
53
54        check(this->cards);
55        register_object(this->cards, ret);
56
57        return ret;
58}
59
60void ctor(
61                gc_pool_object_iterator* const this,
62                struct gc_object_header* start_object
63                #if _DEBUG
64                        , intptr_t pool_start
65                        , intptr_t pool_end
66                #endif
67        )
68{
69        this->object = start_object;
70        #if _DEBUG
71                this->lower_limit = pool_start;
72                this->upper_limit = pool_end;
73        #endif
74
75        check( ((intptr_t)start_object) >= lower_limit );
76        check( ((intptr_t)start_object) <= upper_limit );
77}
78
79gc_pool_object_iterator gc_pool_iterator_for(gc_memory_pool* const this, void* member)
80{
81        size_t card = card_of(member);
82        intptr_t member_add = (intptr_t)member;
83        void* start_obj;
84        intptr_t start_obj_add;
85
86        do
87        {
88                check(card < CARDS_COUNT);
89                start_obj = object_at(this->cards, card);
90                check(card != 0 || start_obj);
91                card--;
92                start_obj_add = (intptr_t)start_obj;
93        }
94        while(start_obj_add > member_add || start_obj_add != 0);
95
96        check(start_obj);
97       
98        struct gc_object_header* start_obj_typed = (struct gc_object_header*)start_obj;
99
100        gc_pool_object_iterator it;
101        ctor( &it,
102                start_obj_typed,
103                #if _DEBUG
104                        , (intptr_t)this->start_p
105                        , (intptr_t)this->free_p
106                #endif
107        );
108        return it;
109}
110
111bool ?!=?(const gc_pool_object_iterator lhs, const gc_pool_object_iterator rhs)
112{
113        return lhs.object != rhs.object;
114}
115
116gc_pool_object_iterator begin(gc_memory_pool* const this)
117{
118        struct gc_object_header* start_obj = (struct gc_object_header*)this->start_p;
119        gc_pool_object_iterator it;
120        ctor( &it,
121                start_obj,
122                #if _DEBUG
123                        , (intptr_t)this->start_p
124                        , (intptr_t)this->free_p
125                #endif
126        );
127        return it;
128}
129
130gc_pool_object_iterator end(gc_memory_pool* const this)
131{
132        gc_pool_object_iterator it;
133        ctor( &it,
134                (struct gc_object_header*)this->free_p,
135                #if _DEBUG
136                        , (intptr_t)this->start_p
137                        , (intptr_t)this->free_p
138                #endif
139        );
140        return it;
141}
142
143gc_pool_object_iterator* ++?(gc_pool_object_iterator* it)
144{
145        struct gc_object_header* object = it->object;
146        intptr_t next_ptr = ((intptr_t)object) + object->size;
147        check(next_ptr > lower_limit);
148        check(next_ptr <= upper_limit);
149
150        struct gc_object_header* next_obj = ((struct gc_object_header*)next_ptr);
151        check(next_ptr == upper_limit || is_valide(next_obj));
152
153        it->object = next_obj;
154        return it;
155}
156
157const struct gc_object_header* *?(const gc_pool_object_iterator it)
158{
159        return it.object;
160}
161
162struct gc_object_header* *?(gc_pool_object_iterator it)
163{
164        return it.object;
165}
Note: See TracBrowser for help on using the repository browser.