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

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since dcac8bf1 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
RevLine 
[16cfd8c]1#include "memory_pool.h"
2
[46f1d20]3extern "C" {
4        #include <stdlib.h>
5        #include <string.h>
6}
[16cfd8c]7
[46f1d20]8#include "collector.h"
[4ef8fb3]9#include "object_header.h"
10
11const size_t gc_pool_header_size = (size_t)(  &(((gc_memory_pool*)NULL)->start_p) );
[16cfd8c]12
[24bc651]13void ?{}(gc_memory_pool* this, size_t size, gc_memory_pool* next, gc_memory_pool* mirror, uint8_t type)
[16cfd8c]14{
15        this->mirror = mirror;
16        this->next = next;
17        this->type_code = type;
18
[24bc651]19        this->cards = ( (card_table_t*)malloc(sizeof(card_table_t)) ){};
[16cfd8c]20
21        this->end_p = ((uint8_t*)this) + size;
22        this->free_p = this->start_p;
23
[46f1d20]24        check( gc_pool_of( (void*)this ) == this);
[4ef8fb3]25        check(this->cards);
26        gc_reset_pool(this);
27}
28
[24bc651]29void ^?{}(gc_memory_pool* this)
[4ef8fb3]30{
[24bc651]31        ^(&this->cards){};
[4ef8fb3]32        free(this->cards);
33}
34
35void gc_reset_pool(gc_memory_pool *const this)
36{
37        this->free_p = this->start_p;
[46f1d20]38        #ifndef NDEBUG
39                memset(this->start_p, 0xCD, gc_pool_size_total(this));
[4ef8fb3]40        #endif
41
42        check(this->cards);
43        reset(this->cards);
44
[46f1d20]45        check(gc_pool_size_left(this) == gc_pool_size_total(this));
[4ef8fb3]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
[46f1d20]62void ?{}(       gc_pool_object_iterator* this,
[4ef8fb3]63                struct gc_object_header* start_object
[46f1d20]64                #ifndef NDEBUG
[4ef8fb3]65                        , intptr_t pool_start
66                        , intptr_t pool_end
67                #endif
68        )
69{
70        this->object = start_object;
[46f1d20]71        #ifndef NDEBUG
[4ef8fb3]72                this->lower_limit = pool_start;
73                this->upper_limit = pool_end;
74        #endif
75
[46f1d20]76        check( ((intptr_t)start_object) >= this->lower_limit );
77        check( ((intptr_t)start_object) <= this->upper_limit );
[4ef8fb3]78}
79
[46f1d20]80void ^?{}( gc_pool_object_iterator* this ) {}
81
[4ef8fb3]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;
[46f1d20]86        intptr_t start_obj;
[4ef8fb3]87
88        do
89        {
90                check(card < CARDS_COUNT);
[46f1d20]91                start_obj = (intptr_t)object_at(this->cards, card);
[4ef8fb3]92                check(card != 0 || start_obj);
93                card--;
94        }
[76af36f]95        while(start_obj > member_add || !(start_obj));
[4ef8fb3]96
[46f1d20]97        check( start_obj );
98
[4ef8fb3]99        struct gc_object_header* start_obj_typed = (struct gc_object_header*)start_obj;
100
[46f1d20]101        return (gc_pool_object_iterator) {
102                start_obj_typed
103                #ifndef NDEBUG
[4ef8fb3]104                        , (intptr_t)this->start_p
105                        , (intptr_t)this->free_p
106                #endif
[46f1d20]107        };
[4ef8fb3]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;
[46f1d20]118        return (gc_pool_object_iterator) {
119                start_obj
120                #ifndef NDEBUG
[4ef8fb3]121                        , (intptr_t)this->start_p
122                        , (intptr_t)this->free_p
123                #endif
[46f1d20]124        };
[16cfd8c]125}
126
[4ef8fb3]127gc_pool_object_iterator end(gc_memory_pool* const this)
128{
[46f1d20]129        return (gc_pool_object_iterator) {
130                (struct gc_object_header*)this->free_p
131                #ifndef NDEBUG
[4ef8fb3]132                        , (intptr_t)this->start_p
133                        , (intptr_t)this->free_p
134                #endif
[46f1d20]135        };
[4ef8fb3]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;
[46f1d20]142        check(next_ptr > it->lower_limit);
143        check(next_ptr <= it->upper_limit);
[4ef8fb3]144
145        struct gc_object_header* next_obj = ((struct gc_object_header*)next_ptr);
[46f1d20]146        check(next_ptr == it->upper_limit || is_valid(next_obj));
[4ef8fb3]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.