[16cfd8c] | 1 | #include "memory_pool.h" |
---|
| 2 | |
---|
[46f1d20] | 3 | extern "C" { |
---|
| 4 | #include <stdlib.h> |
---|
| 5 | #include <string.h> |
---|
| 6 | } |
---|
[16cfd8c] | 7 | |
---|
[46f1d20] | 8 | #include "collector.h" |
---|
[4ef8fb3] | 9 | #include "object_header.h" |
---|
| 10 | |
---|
| 11 | const size_t gc_pool_header_size = (size_t)( &(((gc_memory_pool*)NULL)->start_p) ); |
---|
[16cfd8c] | 12 | |
---|
| 13 | void 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 | |
---|
[4ef8fb3] | 19 | card_table_t* new = (card_table_t*)malloc(sizeof(card_table_t)); |
---|
[16cfd8c] | 20 | this->cards = new; |
---|
[46f1d20] | 21 | ctor_card(this->cards); |
---|
[16cfd8c] | 22 | |
---|
| 23 | this->end_p = ((uint8_t*)this) + size; |
---|
| 24 | this->free_p = this->start_p; |
---|
| 25 | |
---|
[46f1d20] | 26 | check( gc_pool_of( (void*)this ) == this); |
---|
[4ef8fb3] | 27 | check(this->cards); |
---|
| 28 | gc_reset_pool(this); |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | void dtor(gc_memory_pool *const this) |
---|
| 32 | { |
---|
[46f1d20] | 33 | dtor_card(this->cards); |
---|
[4ef8fb3] | 34 | free(this->cards); |
---|
| 35 | } |
---|
| 36 | |
---|
| 37 | void gc_reset_pool(gc_memory_pool *const this) |
---|
| 38 | { |
---|
| 39 | this->free_p = this->start_p; |
---|
[46f1d20] | 40 | #ifndef NDEBUG |
---|
| 41 | memset(this->start_p, 0xCD, gc_pool_size_total(this)); |
---|
[4ef8fb3] | 42 | #endif |
---|
| 43 | |
---|
| 44 | check(this->cards); |
---|
| 45 | reset(this->cards); |
---|
| 46 | |
---|
[46f1d20] | 47 | check(gc_pool_size_left(this) == gc_pool_size_total(this)); |
---|
[4ef8fb3] | 48 | } |
---|
| 49 | |
---|
| 50 | void* 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 | |
---|
[46f1d20] | 64 | void ?{}( gc_pool_object_iterator* this, |
---|
[4ef8fb3] | 65 | struct gc_object_header* start_object |
---|
[46f1d20] | 66 | #ifndef NDEBUG |
---|
[4ef8fb3] | 67 | , intptr_t pool_start |
---|
| 68 | , intptr_t pool_end |
---|
| 69 | #endif |
---|
| 70 | ) |
---|
| 71 | { |
---|
| 72 | this->object = start_object; |
---|
[46f1d20] | 73 | #ifndef NDEBUG |
---|
[4ef8fb3] | 74 | this->lower_limit = pool_start; |
---|
| 75 | this->upper_limit = pool_end; |
---|
| 76 | #endif |
---|
| 77 | |
---|
[46f1d20] | 78 | check( ((intptr_t)start_object) >= this->lower_limit ); |
---|
| 79 | check( ((intptr_t)start_object) <= this->upper_limit ); |
---|
[4ef8fb3] | 80 | } |
---|
| 81 | |
---|
[46f1d20] | 82 | void ^?{}( gc_pool_object_iterator* this ) {} |
---|
| 83 | |
---|
[4ef8fb3] | 84 | gc_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; |
---|
[46f1d20] | 88 | intptr_t start_obj; |
---|
[4ef8fb3] | 89 | |
---|
| 90 | do |
---|
| 91 | { |
---|
| 92 | check(card < CARDS_COUNT); |
---|
[46f1d20] | 93 | start_obj = (intptr_t)object_at(this->cards, card); |
---|
[4ef8fb3] | 94 | check(card != 0 || start_obj); |
---|
| 95 | card--; |
---|
| 96 | } |
---|
[76af36f] | 97 | while(start_obj > member_add || !(start_obj)); |
---|
[4ef8fb3] | 98 | |
---|
[46f1d20] | 99 | check( start_obj ); |
---|
| 100 | |
---|
[4ef8fb3] | 101 | struct gc_object_header* start_obj_typed = (struct gc_object_header*)start_obj; |
---|
| 102 | |
---|
[46f1d20] | 103 | return (gc_pool_object_iterator) { |
---|
| 104 | start_obj_typed |
---|
| 105 | #ifndef NDEBUG |
---|
[4ef8fb3] | 106 | , (intptr_t)this->start_p |
---|
| 107 | , (intptr_t)this->free_p |
---|
| 108 | #endif |
---|
[46f1d20] | 109 | }; |
---|
[4ef8fb3] | 110 | } |
---|
| 111 | |
---|
| 112 | bool ?!=?(const gc_pool_object_iterator lhs, const gc_pool_object_iterator rhs) |
---|
| 113 | { |
---|
| 114 | return lhs.object != rhs.object; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | gc_pool_object_iterator begin(gc_memory_pool* const this) |
---|
| 118 | { |
---|
| 119 | struct gc_object_header* start_obj = (struct gc_object_header*)this->start_p; |
---|
[46f1d20] | 120 | return (gc_pool_object_iterator) { |
---|
| 121 | start_obj |
---|
| 122 | #ifndef NDEBUG |
---|
[4ef8fb3] | 123 | , (intptr_t)this->start_p |
---|
| 124 | , (intptr_t)this->free_p |
---|
| 125 | #endif |
---|
[46f1d20] | 126 | }; |
---|
[16cfd8c] | 127 | } |
---|
| 128 | |
---|
[4ef8fb3] | 129 | gc_pool_object_iterator end(gc_memory_pool* const this) |
---|
| 130 | { |
---|
[46f1d20] | 131 | return (gc_pool_object_iterator) { |
---|
| 132 | (struct gc_object_header*)this->free_p |
---|
| 133 | #ifndef NDEBUG |
---|
[4ef8fb3] | 134 | , (intptr_t)this->start_p |
---|
| 135 | , (intptr_t)this->free_p |
---|
| 136 | #endif |
---|
[46f1d20] | 137 | }; |
---|
[4ef8fb3] | 138 | } |
---|
| 139 | |
---|
| 140 | gc_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; |
---|
[46f1d20] | 144 | check(next_ptr > it->lower_limit); |
---|
| 145 | check(next_ptr <= it->upper_limit); |
---|
[4ef8fb3] | 146 | |
---|
| 147 | struct gc_object_header* next_obj = ((struct gc_object_header*)next_ptr); |
---|
[46f1d20] | 148 | check(next_ptr == it->upper_limit || is_valid(next_obj)); |
---|
[4ef8fb3] | 149 | |
---|
| 150 | it->object = next_obj; |
---|
| 151 | return it; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | const struct gc_object_header* *?(const gc_pool_object_iterator it) |
---|
| 155 | { |
---|
| 156 | return it.object; |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | struct gc_object_header* *?(gc_pool_object_iterator it) |
---|
| 160 | { |
---|
| 161 | return it.object; |
---|
| 162 | } |
---|