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

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

Moved up many directories in source

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