source: src/examples/gc_no_raii/src/internal/memory_pool.c @ 76af36f

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

many small fixes in garbage collector, mostly RAII related

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