-
Notifications
You must be signed in to change notification settings - Fork 1
/
gc.c
470 lines (412 loc) · 15.1 KB
/
gc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/**
* Copyright 2023 Slawomir Maludzinski
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "gc.h"
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
gc * gc_new(heap_size_t size)
{
heap_size_t i = 0;
gc * collector = (gc *)malloc(sizeof(gc));
collector->free[0] = 1;
collector->free[1] = 1;
collector->heap[0] = (gc_heap *)malloc(sizeof(gc_heap) * size);
collector->heap[1] = (gc_heap *)malloc(sizeof(gc_heap) * size);
collector->heap_idx = 0;
collector->size = size;
for (i = 0; i < size; i++)
{
collector->heap[0][i].mark = 0;
collector->heap[0][i].object_value = NULL;
collector->heap[1][i].mark = 0;
collector->heap[1][i].object_value = NULL;
}
return collector;
}
void gc_delete(gc * collector)
{
heap_size_t mi;
for (mi = 0; mi < collector->size; mi++)
{
void * object_value;
if ((object_value = collector->heap[collector->heap_idx][mi].object_value) != NULL)
{
object_delete(object_value);
}
}
if (collector->heap[0])
{
free(collector->heap[0]);
}
if (collector->heap[1])
{
free(collector->heap[1]);
}
free(collector);
}
void gc_mark(gc * collector, heap_ptr addr)
{
if (addr == 0)
{
return;
}
if (collector->heap[collector->heap_idx][addr].mark == 1)
{
return;
}
if (collector->heap[collector->heap_idx][addr].object_value == NULL)
{
return;
}
switch (collector->heap[collector->heap_idx][addr].object_value->type)
{
case OBJECT_UNKNOWN:
assert(0);
break;
case OBJECT_ATOM:
collector->heap[collector->heap_idx][addr].mark = 1;
break;
case OBJECT_INT:
collector->heap[collector->heap_idx][addr].mark = 1;
break;
case OBJECT_REF:
collector->heap[collector->heap_idx][addr].mark = 1;
gc_mark(collector, collector->heap[collector->heap_idx][addr].object_value->ref_value.ref);
break;
case OBJECT_STRUCT:
{
unsigned int i;
collector->heap[collector->heap_idx][addr].mark = 1;
object * value = collector->heap[collector->heap_idx][addr].object_value;
for (i = 0; i < value->struct_value.size; i++)
{
gc_mark(collector, value->struct_value.refs[i]);
}
}
break;
}
}
void gc_run(
gc * collector,
gc_stack * omfalos, stack_ptr stack_size,
gc_stack * trail, stack_ptr trail_size)
{
// mark reachable objects
stack_ptr si;
for (si = 0; si <= stack_size; si++)
{
if (omfalos[si].type == STACK_TYPE_HEAP_PTR)
{
gc_mark(collector, omfalos[si].addr);
}
}
// move reachable objects
heap_size_t mi;
unsigned int curr_mem = collector->heap_idx;
unsigned int next_mem = 1 - curr_mem;
for (mi = 0; mi < collector->free[curr_mem]; mi++)
{
if (collector->heap[curr_mem][mi].mark == 1)
{ // move object from one side to other
collector->heap[curr_mem][mi].mark = 0;
collector->heap[next_mem][collector->free[next_mem]] = collector->heap[curr_mem][mi];
collector->heap[curr_mem][mi].back_ptr = collector->free[next_mem];
collector->free[next_mem]++;
}
else
{ // reclaim memory
if (collector->heap[curr_mem][mi].object_value != NULL)
{
object_delete(collector->heap[curr_mem][mi].object_value);
collector->heap[curr_mem][mi].object_value = NULL;
}
}
}
// change addresses in objects
for (mi = 0; mi < collector->free[next_mem]; mi++)
{
if (collector->heap[next_mem][mi].object_value != NULL)
{
switch (collector->heap[next_mem][mi].object_value->type)
{
case OBJECT_UNKNOWN:
assert(0);
break;
case OBJECT_ATOM:
break;
case OBJECT_INT:
break;
case OBJECT_REF:
{
heap_ptr * ref_ptr = &(collector->heap[next_mem][mi].object_value->ref_value.ref);
*ref_ptr = collector->heap[curr_mem][*ref_ptr].back_ptr;
}
break;
case OBJECT_STRUCT:
{
object * object_value = collector->heap[next_mem][mi].object_value;
for (unsigned int idx = 0; idx < object_value->struct_value.size; idx++)
{
heap_ptr * ref_ptr = &(object_value->struct_value.refs[idx]);
*ref_ptr = collector->heap[curr_mem][*ref_ptr].back_ptr;
}
}
break;
}
}
}
// change addresses in stack
for (si = 0; si <= stack_size; si++)
{
if (omfalos[si].type == STACK_TYPE_HEAP_PTR)
{
heap_ptr * ref_ptr = &(omfalos[si].addr);
*ref_ptr = collector->heap[curr_mem][*ref_ptr].back_ptr;
}
}
// change addresses in trail
for (si = 0; si <= trail_size; si++)
{
if (trail[si].type == STACK_TYPE_HEAP_PTR)
{
heap_ptr * ref_ptr = &(trail[si].addr);
*ref_ptr = collector->heap[curr_mem][*ref_ptr].back_ptr;
}
}
// reset unused memory
for (mi = 0; mi < collector->free[curr_mem]; mi++)
{
collector->heap[curr_mem][mi].object_value = NULL;
}
// change memory side
collector->free[curr_mem] = 1;
collector->heap_idx = next_mem;
}
heap_ptr gc_alloc_any(gc * collector, object * value)
{
heap_ptr loc = collector->free[collector->heap_idx];
if (loc == 0)
{
fprintf(stderr, "out of memory\n");
exit(1);
}
collector->heap[collector->heap_idx][loc].mark = 0;
collector->heap[collector->heap_idx][loc].object_value = value;
collector->free[collector->heap_idx]++;
return loc;
}
heap_ptr gc_alloc_atom(gc * collector, atom_idx_t idx)
{
return gc_alloc_any(collector, object_new_atom(idx));
}
heap_ptr gc_alloc_int(gc * collector, int value)
{
return gc_alloc_any(collector, object_new_int(value));
}
heap_ptr gc_alloc_anon(gc * collector)
{
heap_ptr value = gc_alloc_any(collector, object_new_anon());
collector->heap[collector->heap_idx][value].object_value->ref_value.ref = value;
return value;
}
heap_ptr gc_alloc_var(gc * collector)
{
heap_ptr value = gc_alloc_any(collector, object_new_var());
collector->heap[collector->heap_idx][value].object_value->ref_value.ref = value;
return value;
}
heap_ptr gc_alloc_ref(gc * collector, heap_ptr ptr_value)
{
return gc_alloc_any(collector, object_new_ref(ptr_value));
}
heap_ptr gc_alloc_struct(gc * collector, heap_size_t size, pc_ptr addr)
{
return gc_alloc_any(collector, object_new_struct(size, addr));
}
object_type gc_get_object_type(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
if (collector->heap[collector->heap_idx][addr].object_value != NULL)
{
return collector->heap[collector->heap_idx][addr].object_value->type;
}
return OBJECT_UNKNOWN;
}
heap_ptr gc_get_hp(gc * collector)
{
return collector->free[collector->heap_idx];
}
void gc_reset_hp(gc * collector, heap_ptr new_hp)
{
if (new_hp >= collector->free[collector->heap_idx])
{
return;
}
heap_ptr h_ptr;
for (h_ptr = new_hp; h_ptr < collector->free[collector->heap_idx]; h_ptr++)
{
void * object_value;
if ((object_value = collector->heap[collector->heap_idx][h_ptr].object_value) != NULL)
{
object_delete(object_value);
collector->heap[collector->heap_idx][h_ptr].mark = 0;
collector->heap[collector->heap_idx][h_ptr].object_value = NULL;
}
}
collector->free[collector->heap_idx] = new_hp;
}
atom_idx_t gc_get_atom_idx(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_ATOM);
return collector->heap[collector->heap_idx][addr].object_value->atom_value.idx;
}
int gc_get_int_value(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_INT);
return collector->heap[collector->heap_idx][addr].object_value->int_value.value;
}
void gc_set_int_value(gc * collector, heap_ptr addr, int value)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_INT);
collector->heap[collector->heap_idx][addr].object_value->int_value.value = value;
}
heap_ptr gc_get_anon_ref(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref;
}
heap_ptr gc_get_var_ref(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref;
}
heap_ptr gc_get_ref_ref(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref;
}
heap_size_t gc_get_struct_size(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_STRUCT);
return collector->heap[collector->heap_idx][addr].object_value->struct_value.size;
}
pc_ptr gc_get_struct_addr(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_STRUCT);
return collector->heap[collector->heap_idx][addr].object_value->struct_value.addr;
}
heap_ptr gc_get_struct_ref(gc * collector, heap_ptr addr, heap_size_t idx)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_STRUCT);
assert(collector->heap[collector->heap_idx][addr].object_value->struct_value.size > idx);
return collector->heap[collector->heap_idx][addr].object_value->struct_value.refs[idx];
}
atom_idx_t gc_set_atom_idx(gc * collector, heap_ptr addr, atom_idx_t idx)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_ATOM);
return collector->heap[collector->heap_idx][addr].object_value->atom_value.idx = idx;
}
heap_ptr gc_set_anon_ref(gc * collector, heap_ptr addr, heap_ptr ref)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref = ref;
}
heap_ptr gc_set_var_ref(gc * collector, heap_ptr addr, heap_ptr ref)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref = ref;
}
heap_ptr gc_set_ref_ref(gc * collector, heap_ptr addr, heap_ptr ref)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
return collector->heap[collector->heap_idx][addr].object_value->ref_value.ref = ref;
}
heap_ptr gc_reset_ref(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_REF);
collector->heap[collector->heap_idx][addr].object_value->ref_value.ref = addr;
return addr;
}
heap_ptr gc_set_struct_ref(gc * collector, heap_ptr addr, heap_size_t idx, heap_ptr ref)
{
assert(collector->size > addr);
assert(collector->heap[collector->heap_idx][addr].object_value->type == OBJECT_STRUCT);
assert(collector->heap[collector->heap_idx][addr].object_value->struct_value.size > idx);
return collector->heap[collector->heap_idx][addr].object_value->struct_value.refs[idx] = ref;
}
void gc_print_ref(gc * collector, heap_ptr addr)
{
assert(collector->size > addr);
object_print_str(collector->heap[collector->heap_idx][addr].object_value, NULL, 0);
}
void gc_print_ref_str(gc * collector, heap_ptr addr, char ** strtab_array, unsigned int strtab_size)
{
assert(collector->size > addr);
switch (collector->heap[collector->heap_idx][addr].object_value->type)
{
case OBJECT_UNKNOWN:
printf("%s\n", object_type_str(OBJECT_UNKNOWN));
break;
case OBJECT_ATOM:
object_print_str(collector->heap[collector->heap_idx][addr].object_value, strtab_array, strtab_size);
break;
case OBJECT_INT:
object_print_str(collector->heap[collector->heap_idx][addr].object_value, strtab_array, strtab_size);
break;
case OBJECT_REF:
if (addr != collector->heap[collector->heap_idx][addr].object_value->ref_value.ref)
{
gc_print_ref_str(collector, collector->heap[collector->heap_idx][addr].object_value->ref_value.ref, strtab_array, strtab_size);
}
break;
case OBJECT_STRUCT:
{
unsigned int i = 0;
printf("%s/%u\n", object_type_str(OBJECT_STRUCT), collector->heap[collector->heap_idx][addr].object_value->struct_value.size);
for (i = 0; i < collector->heap[collector->heap_idx][addr].object_value->struct_value.size; i++)
{
gc_print_ref_str(collector, collector->heap[collector->heap_idx][addr].object_value->struct_value.refs[i], strtab_array, strtab_size);
}
}
break;
}
}
gc_stack * gc_stack_new(stack_size_t size)
{
gc_stack * stack = (gc_stack *)malloc(size * sizeof(gc_stack));
return stack;
}
void gc_stack_delete(gc_stack * stack) { free(stack); }