1 | #include "state.h" |
---|
2 | |
---|
3 | #include <stdlib.h> |
---|
4 | |
---|
5 | //general purpouse includes |
---|
6 | #include "tools.h" |
---|
7 | |
---|
8 | //platform abstraction includes |
---|
9 | #include "allocate-pool.h" |
---|
10 | |
---|
11 | //gc internal includes |
---|
12 | #include "globals.h" |
---|
13 | #include "memory_pool.h" |
---|
14 | // #include "memory_pool_iterator.h" |
---|
15 | #include "object_header.h" |
---|
16 | |
---|
17 | void gc_state_swap(gc_state *const this); |
---|
18 | void gc_state_sweep_roots(gc_state *const this, worklist_t* worklist); |
---|
19 | void gc_state_clear(gc_state *const this); |
---|
20 | void gc_state_calc_usage(gc_state *const this); |
---|
21 | |
---|
22 | #if DEBUG |
---|
23 | bool gc_state_roots_match(gc_state *const this); |
---|
24 | bool gc_state_no_from_space_ref(gc_state *const this); |
---|
25 | #endif |
---|
26 | |
---|
27 | gc_state* gc_get_state() |
---|
28 | { |
---|
29 | static gc_state s; |
---|
30 | if(!s.is_initialized) gc_state_ctor(&s); |
---|
31 | return &s; |
---|
32 | } |
---|
33 | |
---|
34 | void gc_state_ctor(gc_state *const this) |
---|
35 | { |
---|
36 | this->from_code = 0; |
---|
37 | this->to_space = NULL; |
---|
38 | this->from_space = NULL; |
---|
39 | this->total_space = 0; |
---|
40 | this->used_space = 0; |
---|
41 | // state->pools_table(); |
---|
42 | |
---|
43 | gc_allocate_pool(this); |
---|
44 | |
---|
45 | this->is_initialized = true; |
---|
46 | } |
---|
47 | |
---|
48 | // bool state::is_in_heap(void* address) const |
---|
49 | // { |
---|
50 | // memory_pool* target_pool = gc_pool_of(address); |
---|
51 | // |
---|
52 | // auto first = pools_table.cbegin(); |
---|
53 | // auto last = pools_table.cend(); |
---|
54 | // auto result = std::find(first, last, target_pool); |
---|
55 | // return result != last && (*result)->is_from_space(); |
---|
56 | // } |
---|
57 | |
---|
58 | // bool state::is_in_to_space(void* address) const |
---|
59 | // { |
---|
60 | // const memory_pool* target_pool = pool_of(address); |
---|
61 | // |
---|
62 | // auto first = pools_table.cbegin(); |
---|
63 | // auto last = pools_table.cend(); |
---|
64 | // auto result = std::find(first, last, target_pool); |
---|
65 | // return result != last && !(*result)->is_from_space(); |
---|
66 | // } |
---|
67 | // |
---|
68 | // gc_object_header* gc_get_object_for_ref(gc_state* state, void* member) |
---|
69 | // { |
---|
70 | // intptr_t target = ((intptr_t)member); |
---|
71 | // if(!gc_is_in_heap(state, member)) return NULL; |
---|
72 | // |
---|
73 | // gc_memory_pool* pool = gc_pool_of(member); |
---|
74 | // gc_pool_object_iterator it = gc_pool_iterator_for(pool, member); |
---|
75 | // gc_pool_object_iterator end = gc_pool_end(pool); |
---|
76 | // |
---|
77 | // while(it != end) |
---|
78 | // { |
---|
79 | // gc_object_header* object = *it; |
---|
80 | // { |
---|
81 | // intptr_t start = ((intptr_t)object); |
---|
82 | // intptr_t end = ((intptr_t)start + object->size); |
---|
83 | // if(start < target && end > target) |
---|
84 | // { |
---|
85 | // return object; |
---|
86 | // } |
---|
87 | // } |
---|
88 | // ++it; |
---|
89 | // } |
---|
90 | // |
---|
91 | // checkf(false, "is_in_heap() and iterator_for() return inconsistent data"); |
---|
92 | // abort(); |
---|
93 | // return NULL; |
---|
94 | // } |
---|
95 | |
---|
96 | void* gc_state_try_allocate(gc_state *const this, size_t size) |
---|
97 | { |
---|
98 | gc_memory_pool* pool = this->from_space; |
---|
99 | while(pool != (gc_memory_pool*)0) |
---|
100 | { |
---|
101 | if(gc_pool_size_left(pool) > size) |
---|
102 | { |
---|
103 | return gc_pool_allocate(pool, size, true); |
---|
104 | } |
---|
105 | pool = pool->next; |
---|
106 | } |
---|
107 | |
---|
108 | return (void*)0; |
---|
109 | } |
---|
110 | |
---|
111 | void gc_state_allocate_pool(gc_state *const this) |
---|
112 | { |
---|
113 | gc_memory_pool* old_from_space = this->from_space; |
---|
114 | gc_memory_pool* old_to_space = this->to_space; |
---|
115 | |
---|
116 | this->from_space = (gc_memory_pool*)(pal_allocPool(POOL_SIZE_BYTES, 1)); |
---|
117 | this->to_space = (gc_memory_pool*)(pal_allocPool(POOL_SIZE_BYTES, 1)); |
---|
118 | |
---|
119 | gc_memory_pool_ctor(this->from_space, POOL_SIZE_BYTES, old_from_space, this->to_space, this->from_code); |
---|
120 | gc_memory_pool_ctor(this->to_space, POOL_SIZE_BYTES, old_to_space, this->from_space, (~this->from_code) & 0x01); |
---|
121 | |
---|
122 | this->total_space += gc_pool_size_used(this->from_space); |
---|
123 | |
---|
124 | // pools_table.push_back(from_space); |
---|
125 | // pools_table.push_back(to_space); |
---|
126 | } |
---|
127 | |
---|
128 | void gc_state_collect(gc_state *const this) |
---|
129 | { |
---|
130 | // DEBUG("collecting"); |
---|
131 | // DEBUG("previous usage " << this->used_space << " / " << this->total_space); |
---|
132 | |
---|
133 | worklist_t worklist; |
---|
134 | // vector_ctor(&worklist); |
---|
135 | gc_state_sweep_roots(this, &worklist); |
---|
136 | |
---|
137 | while(!empty(&worklist)) |
---|
138 | { |
---|
139 | void** ref = back(&worklist); |
---|
140 | pop_back(&worklist); |
---|
141 | gc_state_process_reference(this, ref, &worklist); |
---|
142 | } |
---|
143 | // |
---|
144 | // check(gc_state_roots_match(this)); |
---|
145 | // check(gc_state_no_from_space_ref(this)); |
---|
146 | // |
---|
147 | // gc_state_swap(this); |
---|
148 | // |
---|
149 | // gc_state_calc_usage(this); |
---|
150 | // |
---|
151 | // if(gc_state_needs_collect(this)) gc_state_allocate_pool(this); |
---|
152 | |
---|
153 | // DEBUG("done"); |
---|
154 | // dtor(&worklist); |
---|
155 | } |
---|
156 | // |
---|
157 | // void state::swap() |
---|
158 | // { |
---|
159 | // std::swap(from_space, to_space); |
---|
160 | // |
---|
161 | // memory_pool* pool = to_space; |
---|
162 | // while(pool) |
---|
163 | // { |
---|
164 | // pool->reset(); |
---|
165 | // pool = pool->next(); |
---|
166 | // } |
---|
167 | // |
---|
168 | // from_code = (~from_code) & 0x01; |
---|
169 | // |
---|
170 | // #if _DEBUG |
---|
171 | // { |
---|
172 | // memory_pool* pool = from_space; |
---|
173 | // while(pool) |
---|
174 | // { |
---|
175 | // check(pool->is_from_space()); |
---|
176 | // pool = pool->next(); |
---|
177 | // } |
---|
178 | // |
---|
179 | // pool = to_space; |
---|
180 | // while(pool) |
---|
181 | // { |
---|
182 | // check(!pool->is_from_space()); |
---|
183 | // pool = pool->next(); |
---|
184 | // } |
---|
185 | // } |
---|
186 | // #endif |
---|
187 | // } |
---|
188 | // |
---|
189 | // void state::sweep_roots(std::vector<void**>& worklist) |
---|
190 | // { |
---|
191 | // memory_pool* pool = from_space; |
---|
192 | // while(pool) |
---|
193 | // { |
---|
194 | // for(object_header* object : *pool) |
---|
195 | // { |
---|
196 | // if(!object->root_chain) continue; |
---|
197 | // |
---|
198 | // copy_object(object); |
---|
199 | // |
---|
200 | // scan_object(object->forward, worklist); |
---|
201 | // } |
---|
202 | // |
---|
203 | // pool = pool->next(); |
---|
204 | // } |
---|
205 | // } |
---|
206 | // |
---|
207 | // void state::clear() |
---|
208 | // { |
---|
209 | // memory_pool* pool = from_space; |
---|
210 | // while(pool) |
---|
211 | // { |
---|
212 | // pool->reset(); |
---|
213 | // pool = pool->next(); |
---|
214 | // } |
---|
215 | // |
---|
216 | // pool = to_space; |
---|
217 | // while(pool) |
---|
218 | // { |
---|
219 | // pool->reset(); |
---|
220 | // pool = pool->next(); |
---|
221 | // } |
---|
222 | // } |
---|
223 | // |
---|
224 | // void state::calc_usage() |
---|
225 | // { |
---|
226 | // total_space = 0; |
---|
227 | // used_space = 0; |
---|
228 | // |
---|
229 | // memory_pool* pool = from_space; |
---|
230 | // while(pool) |
---|
231 | // { |
---|
232 | // size_t size = pool->size(); |
---|
233 | // size_t used = size - pool->size_left(); |
---|
234 | // check(used <= size); |
---|
235 | // total_space += size; |
---|
236 | // used_space += used; |
---|
237 | // |
---|
238 | // pool = pool->next(); |
---|
239 | // } |
---|
240 | // } |
---|
241 | // |
---|
242 | // #if _DEBUG |
---|
243 | // bool state::roots_match() |
---|
244 | // { |
---|
245 | // memory_pool* pool = to_space; |
---|
246 | // while(pool) |
---|
247 | // { |
---|
248 | // size_t size = 0; |
---|
249 | // for(object_header* object : *pool) |
---|
250 | // { |
---|
251 | // size += object->size; |
---|
252 | // |
---|
253 | // gcpointer_base* ptr = object->root_chain; |
---|
254 | // while(ptr) |
---|
255 | // { |
---|
256 | // check(get_object_ptr(ptr->m_ptr) == object); |
---|
257 | // ptr = ptr->m_next; |
---|
258 | // } |
---|
259 | // } |
---|
260 | // |
---|
261 | // check(size + pool->size_left() == pool->size()); |
---|
262 | // |
---|
263 | // pool = pool->next(); |
---|
264 | // } |
---|
265 | // |
---|
266 | // return true; |
---|
267 | // } |
---|
268 | // |
---|
269 | // bool state::no_from_space_ref() |
---|
270 | // { |
---|
271 | // memory_pool* pool = to_space; |
---|
272 | // while(pool) |
---|
273 | // { |
---|
274 | // void** potential_ref = (void**)pool->m_start; |
---|
275 | // while(potential_ref < (void**)pool->m_free) |
---|
276 | // { |
---|
277 | // check(!is_in_heap(*potential_ref)); |
---|
278 | // potential_ref++; |
---|
279 | // } |
---|
280 | // |
---|
281 | // pool = pool->next(); |
---|
282 | // } |
---|
283 | // |
---|
284 | // return true; |
---|
285 | // } |
---|
286 | // #endif |
---|