-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathgc.c
363 lines (322 loc) · 7.09 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
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "gc.h"
#include "eval.h"
#include "struct.h"
#include "continue.h"
#include "jit.h"
#include "fail.h"
static char *to_start, *to_pos, *to_end;
static char *from_start, *from_end;
static int gc_verbose;
void gc_init(int heap_size, int verbose) {
to_start = malloc(heap_size);
to_pos = to_start;
to_end = to_start + heap_size;
from_start = malloc(heap_size);
from_end = from_start + heap_size;
gc_verbose = verbose;
}
static void collect_garbage(void *p1, void *p2, void *p3, void *p4);
/************************************************************/
static int enabled = 0;
void enable_gc()
{
enabled++;
}
void disable_gc()
{
enabled--;
}
/************************************************************/
typedef struct gcable {
int tag;
void *forward;
} gcable;
static int align_size(int sz)
{
/* make sure there's enough room for a warding pointer: */
if (sz < sizeof(gcable))
sz = sizeof(gcable);
/* 16-byte alignment: */
if ((sz % 16) > 0) {
sz += (16 - (sz % 16));
}
return sz;
}
/************************************************************/
void *gc_malloc4(int sz, void *p1, void *p2, void *p3, void *p4)
{
if (enabled) {
sz = align_size(sz);
while (1) {
if (to_pos + sz < to_end) {
void *p = to_pos;
to_pos += sz;
return p;
} else {
collect_garbage(p1, p2, p3, p4);
if (to_pos + sz >= to_end)
return fail("out of memory");
}
}
} else {
return malloc(sz);
}
}
void *gc_malloc0(int sz)
{
return gc_malloc4(sz, NULL, NULL, NULL, NULL);
}
void *gc_malloc1(int sz, void *p1)
{
return gc_malloc4(sz, p1, NULL, NULL, NULL);
}
void *gc_malloc2(int sz, void *p1, void *p2)
{
return gc_malloc4(sz, p1, p2, NULL, NULL);
}
void *gc_malloc3(int sz, void *p1, void *p2, void *p3)
{
return gc_malloc4(sz, p1, p2, p3, NULL);
}
int gc_is_collectable(void *p)
{
return (!((uintptr_t)p & 0x1)
&& ((char*)p >= to_start)
&& ((char*)p < to_end));
}
/************************************************************/
static int gcable_size(int tag)
{
int sz;
switch (tag) {
# if !FIXNUM_ENCODING
case num_type:
sz = sizeof(num_val);
break;
# endif
case func_type:
sz = sizeof(func_val);
break;
case sym_type:
sz = sizeof(symbol);
break;
case plus_type:
case minus_type:
case times_type:
case app_type:
sz = sizeof(bin_op_expr);
break;
case lambda_type:
sz = sizeof(lambda_expr);
break;
case env_type:
sz = sizeof(env);
break;
case done_type:
sz = sizeof(cont);
break;
case right_of_bin_type:
sz = sizeof(right_of_bin);
break;
case finish_bin_type:
sz = sizeof(finish_bin);
break;
case right_of_app_type:
sz = sizeof(right_of_app);
break;
case finish_app_type:
sz = sizeof(finish_app);
break;
case finish_if0_type:
sz = sizeof(finish_if0);
break;
# if USE_JIT
case right_jitted_type:
sz = sizeof(right_jitted);
break;
case finish_jitted_type:
sz = sizeof(finish_jitted);
break;
case interp_type:
sz = sizeof(interp);
break;
# endif
default:
fail("bad tag for sizeof");
return 0;
}
return align_size(sz);
}
static void paint_gray(void *_pp)
/* Paint the object at *pp gray and update *pp, or
if the object is already gray/black, just update
*pp using the forwarding pointer. */
{
void **pp = (void **)_pp;
/* First, check whether the referenced object is gcable,
because it might instead have been allocated with
gc disabled, or it might be a number encoded with an
odd address: */
if (!((uintptr_t)(*pp) & 0x1)
&& ((char*)*pp >= from_start)
&& ((char*)*pp < from_end)) {
/* It's a reference to a gcable object */
gcable *p = (gcable *)*pp;
if (p->tag == -1) {
/* already gray or black, so use forwarding pointer */
*pp = p->forward;
} else {
/* paint it gray: */
void *dest = to_pos;
int sz;
sz = gcable_size(p->tag);
to_pos += sz;
memcpy(dest, p, sz);
/* install forwarding pointer: */
p->tag = -1;
p->forward = dest;
*pp = dest;
}
}
}
static void follow_one_gray_pointer(void *p)
{
switch (((gcable *)p)->tag) {
case num_type:
break;
case func_type:
{
func_val *fv = (func_val *)p;
paint_gray(&fv->lam);
paint_gray(&fv->e);
}
break;
case sym_type:
break;
case plus_type:
case minus_type:
case times_type:
case app_type:
{
bin_op_expr *bn = (bin_op_expr *)p;
paint_gray(&bn->left);
paint_gray(&bn->right);
}
break;
case lambda_type:
{
lambda_expr *lam = (lambda_expr *)p;
paint_gray(&lam->arg_name);
paint_gray(&lam->body);
}
break;
case env_type:
{
env *e = (env *)p;
paint_gray(&e->id);
paint_gray(&e->val);
paint_gray(&e->rest);
}
break;
case done_type:
break;
case right_of_bin_type:
{
right_of_bin *gl = (right_of_bin *)p;
paint_gray(&gl->right);
paint_gray(&gl->env);
paint_gray(&gl->rest);
}
break;
case finish_bin_type:
{
finish_bin *gr = (finish_bin *)p;
paint_gray(&gr->left_val);
paint_gray(&gr->rest);
}
break;
case right_of_app_type:
{
right_of_app *gl = (right_of_app *)p;
paint_gray(&gl->right);
paint_gray(&gl->env);
paint_gray(&gl->rest);
}
break;
case finish_app_type:
{
finish_app *gr = (finish_app *)p;
paint_gray(&gr->left_val);
paint_gray(&gr->rest);
}
break;
case finish_if0_type:
{
finish_if0 *gi = (finish_if0 *)p;
paint_gray(&gi->thn);
paint_gray(&gi->els);
paint_gray(&gi->env);
paint_gray(&gi->rest);
}
break;
# if USE_JIT
case right_jitted_type:
{
right_jitted *gj = (right_jitted *)p;
paint_gray(&gj->j.rest);
paint_gray(&gj->env);
}
break;
case finish_jitted_type:
{
finish_jitted *gj = (finish_jitted *)p;
paint_gray(&gj->j.rest);
paint_gray(&gj->val);
}
break;
case interp_type:
{
interp *gi = (interp *)p;
paint_gray(&gi->expr);
paint_gray(&gi->rest);
}
break;
# endif
default:
fail("bad tag for paint_gray content");
break;
}
}
static void collect_garbage(void *p1, void *p2, void *p3, void *p4)
{
char *old_start, *old_end, *gray_pos;
int old_size = to_pos - to_start;
old_start = to_start;
old_end = to_end;
to_start = from_start;
to_end = from_end;
to_pos = to_start;
from_start = old_start;
from_end = old_end;
paint_gray(&expr);
paint_gray(&e);
paint_gray(&todos);
paint_gray(&val);
if (p1) paint_gray(p1);
if (p2) paint_gray(p2);
if (p3) paint_gray(p3);
if (p4) paint_gray(p4);
# if USE_JIT
push_jit_roots(paint_gray);
# endif
gray_pos = to_start;
while (gray_pos < to_pos) {
follow_one_gray_pointer(gray_pos);
gray_pos += gcable_size(((gcable *)gray_pos)->tag);
}
if (gc_verbose)
printf("[collected %d]\n", (int)(old_size - (to_pos - to_start)));
}