1 | #include "memory_pool.h" |
---|
2 | |
---|
3 | #include <stdlib> |
---|
4 | |
---|
5 | #include "object_header.h" |
---|
6 | |
---|
7 | const size_t gc_pool_header_size = (size_t)( &(((gc_memory_pool*)NULL)->start_p) ); |
---|
8 | |
---|
9 | void 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 | |
---|
27 | void dtor(gc_memory_pool *const this) |
---|
28 | { |
---|
29 | dtor(this->cards); |
---|
30 | free(this->cards); |
---|
31 | } |
---|
32 | |
---|
33 | void 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 | |
---|
46 | void* 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 | |
---|
60 | void 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 | |
---|
79 | gc_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 | |
---|
111 | bool ?!=?(const gc_pool_object_iterator lhs, const gc_pool_object_iterator rhs) |
---|
112 | { |
---|
113 | return lhs.object != rhs.object; |
---|
114 | } |
---|
115 | |
---|
116 | gc_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 | |
---|
130 | gc_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 | |
---|
143 | gc_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 | |
---|
157 | const struct gc_object_header* *?(const gc_pool_object_iterator it) |
---|
158 | { |
---|
159 | return it.object; |
---|
160 | } |
---|
161 | |
---|
162 | struct gc_object_header* *?(gc_pool_object_iterator it) |
---|
163 | { |
---|
164 | return it.object; |
---|
165 | } |
---|