/
gc.c
328 lines (260 loc) · 7.38 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
#include "gc.h"
#include <string.h>
#include <malloc.h>
#include <stdio.h>
#include <stdarg.h>
/* Constants */
#define GC_INITIAL_MEM 1024
#define BROKEN_HEART ((gc_ops*)-1)
static uintptr_t *working_mem = NULL;
static uintptr_t *free_mem = NULL;
static uintptr_t *free_ptr = NULL;
static uintptr_t mem_size;
#ifdef TEST_STRESS_GC
static int in_gc = 0;
#endif
static gc_handle gc_root_stack;
static gc_handle gc_root_hooks;
static uint32_t gc_n_temp_roots = 0;
static gc_handle *gc_temp_roots[MAX_EXTERNAL_ROOTS_FRAME];
void *_gc_try_alloc(uint32_t n) {
if(free_ptr - working_mem + n <= mem_size) {
void *h = free_ptr;
free_ptr += n;
return h;
}
return NULL;
}
void* _gc_alloc(uint32_t n) {
void *handle;
#ifdef TEST_STRESS_GC
if(!in_gc) {
gc_gc();
}
#endif
handle = _gc_try_alloc(n);
if(!handle) {
/* Insufficient memory, trigger a GC */
gc_gc();
handle = _gc_try_alloc(n);
if(!handle) {
/* This triggers another GC. If we were clever, we would
use some heuristic to guess when we're running out of
memory, and could gc_realloc above, instead of GCing first. */
gc_realloc(n);
handle = _gc_try_alloc(n);
}
}
assert(handle);
return handle;
}
void *gc_alloc(gc_ops *ops, uint32_t n) {
gc_chunk *handle = _gc_alloc(n);
handle->ops = ops;
return handle;
}
void gc_relocate_root(void);
/* GC control */
void gc_init() {
if(free_mem) {
free(free_mem);
free(working_mem);
}
free_mem = malloc(GC_INITIAL_MEM * sizeof(uintptr_t));
working_mem = malloc(GC_INITIAL_MEM * sizeof(uintptr_t));
assert(!(((uintptr_t)free_mem) & 0x3));
assert(!(((uintptr_t)working_mem) & 0x3));
free_ptr = working_mem;
mem_size = GC_INITIAL_MEM;
gc_root_hooks = NIL;
gc_root_stack = NIL;
gc_n_temp_roots = 0;
gc_register_gc_root_hook(gc_relocate_root);
}
/* GC internals */
void gc_relocate_nop(gc_chunk *val UNUSED) {
/* nop */
}
typedef struct gc_external_roots {
gc_chunk header;
gc_handle next_frame;
uint32_t nroots;
gc_handle *roots[0];
} gc_external_roots;
static void gc_relocate_external_roots(gc_external_roots *v) {
uint32_t i;
gc_external_roots *r = (gc_external_roots*)v;
gc_relocate(&r->next_frame);
for(i = 0; i < r->nroots; i++) {
gc_relocate(r->roots[i]);
}
}
static uint32_t gc_len_external_roots(gc_external_roots *v) {
return v->nroots + 2;
}
static gc_ops gc_external_root_ops = {
.op_relocate = (gc_relocate_op)gc_relocate_external_roots,
.op_len = (gc_len_op) gc_len_external_roots
};
void gc_register_roots(gc_handle* root0, ...) {
int nroots = 1;
va_list ap;
gc_external_roots *frame;
int i;
va_start(ap, root0);
while(va_arg(ap, gc_handle*)) nroots++;
va_end(ap);
assert(nroots <= MAX_EXTERNAL_ROOTS_FRAME);
/*
* Because the roots may already be live, we can't perform GC
* until we've safely registered them. Use _try_alloc to try to
* put them in the heap without risking a GC.
*/
frame = (gc_external_roots*)_gc_try_alloc(3 + nroots);
if(!frame) {
/*
* If there's no room on the heap, we stash them in a
* statically allocated buffer, do the GC, and then move them
* into the heap.
*/
i = 0;
va_start(ap, root0);
gc_temp_roots[i++] = root0;
while(i < nroots)
gc_temp_roots[i++] = va_arg(ap, gc_handle*);
va_end(ap);
gc_n_temp_roots = nroots;
frame = (gc_external_roots*)_gc_alloc(3 + nroots);
gc_n_temp_roots = 0;
}
assert(frame);
frame->header.ops = &gc_external_root_ops;
frame->nroots = nroots;
i = 0;
va_start(ap, root0);
frame->roots[i++] = root0;
while(--nroots)
frame->roots[i++] = va_arg(ap, gc_handle*);
va_end(ap);
frame->next_frame = gc_root_stack;
gc_root_stack = gc_tag_pointer(frame);
}
void gc_pop_roots() {
assert(!NILP(gc_root_stack));
gc_root_stack = UNTAG_PTR(gc_root_stack, gc_external_roots)->next_frame;
}
typedef struct gc_root_hook {
gc_chunk header;
gc_handle next;
gc_hook *hook;
} gc_root_hook;
static void gc_relocate_root_hook(gc_root_hook *hook) {
gc_relocate(&hook->next);
}
static uint32_t gc_len_root_hook(gc_chunk *chunk) {
return 2;
}
static struct gc_ops gc_root_hook_ops = {
.op_relocate = (gc_relocate_op)gc_relocate_root_hook,
.op_len = (gc_len_op)gc_len_root_hook
};
void gc_register_gc_root_hook(gc_hook *hook_fun) {
gc_root_hook *hook = (gc_root_hook*)gc_alloc(&gc_root_hook_ops, 3);
hook->next = gc_root_hooks;
hook->hook = hook_fun;
gc_root_hooks = gc_tag_pointer(hook);
}
void gc_relocate(gc_handle *v) {
int len;
uintptr_t *reloc;
gc_chunk *val;
if(gc_numberp(*v))
return;
val = UNTAG_PTR(*v, gc_chunk);
if((uintptr_t*)val < free_mem
|| (uintptr_t*)val >= (free_mem + mem_size)) {
return;
}
if(val->ops == BROKEN_HEART) {
*v = val->data[0];
return;
}
assert(val->ops);
len = val->ops->op_len(val) + 1;
reloc = _gc_alloc(len);
memcpy(reloc, val, sizeof(uintptr_t) * len);
val->ops = BROKEN_HEART;
*v = val->data[0] = gc_tag_pointer(reloc);
}
void gc_relocate_root() {
int i;
gc_relocate(&gc_root_stack);
gc_relocate(&gc_root_hooks);
if(gc_n_temp_roots) {
for(i = 0; i < gc_n_temp_roots; i++) {
gc_relocate(gc_temp_roots[i]);
}
}
}
void gc_protect_roots() {
gc_root_hook *hook = UNTAG_PTR(gc_root_hooks, gc_root_hook);
while(hook) {
assert(hook->header.ops == &gc_root_hook_ops);
hook->hook();
hook = UNTAG_PTR(hook->next, gc_root_hook);
}
}
void gc_gc() {
uint32_t old_avail = gc_free_mem();
printf("Entering garbage collection...");
uint32_t *scan;
uint32_t *t;
#ifdef TEST_STRESS_GC
if(in_gc) {
printf("GC internal error -- recursive GC!\n");
abort();
}
in_gc = 1;
#endif
t = working_mem;
working_mem = free_mem;
free_mem = t;
scan = free_ptr = working_mem;
gc_protect_roots();
while(scan != free_ptr) {
gc_chunk *chunk = (gc_chunk*)scan;
assert(chunk->ops);
chunk->ops->op_relocate(chunk);
scan += chunk->ops->op_len(chunk) + 1;
if(scan > working_mem + mem_size) {
printf("GC internal error -- ran off the end of memory!\n");
abort();
}
}
#ifdef TEST_STRESS_GC
in_gc = 0;
#endif
#ifndef NDEBUG
/* Zero the old free memory to help catch code that accidentally
holds onto gc_handle's during GC.*/
memset(free_mem, 0, sizeof(uintptr_t) * mem_size);
#endif
printf("Done (freed %d words)\n", gc_free_mem() - old_avail);
}
void gc_realloc(uint32_t need) {
printf("GC realloc forced!\n");
uint32_t new_size = mem_size;
while(new_size - mem_size < need) new_size <<= 1;
printf("New memory: %d words\n", new_size);
free(free_mem);
free_mem = malloc(new_size * sizeof(uintptr_t));
assert(!(((uintptr_t)free_mem) & 0x3));
gc_gc();
free(free_mem);
mem_size = new_size;
free_mem = malloc(mem_size * sizeof(uintptr_t));
assert(!(((uintptr_t)free_mem) & 0x3));
}
uint32_t gc_free_mem() {
return mem_size - (free_ptr - working_mem);
}