This repository has been archived by the owner on Feb 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
gc.c
688 lines (595 loc) · 13.6 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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
#include "object.h"
#include "state.h"
#define PAGE_UNITS ((PIC_HEAP_PAGE_SIZE - offsetof(struct heap_page, u)) / sizeof(union header))
#define alignof(type) offsetof(struct { char c; type m; }, m)
#define roundup(n,unit) (((n) + (unit) - 1) / (unit) * (unit))
struct object {
union {
struct basic basic;
struct symbol sym;
struct string str;
struct blob blob;
struct pair pair;
struct vector vec;
struct dict dict;
struct weak weak;
struct data data;
struct record rec;
struct proc proc;
struct frame frame;
struct port port;
struct error err;
struct irep irep;
} u;
};
struct free_region {
union header *ptr;
size_t size;
};
union header {
struct free_region s;
char alignment[roundup(sizeof(struct free_region), alignof(struct object))];
};
struct heap_page {
struct heap_page *next;
union {
union header basep[1];
struct object alignment;
} u;
};
struct heap {
union header base, *freep;
struct heap_page *pages;
struct weak *weaks; /* weak map chain */
};
#define unitsof(type) ((type2size(type) + sizeof(union header) - 1) / sizeof(union header))
struct heap *
pic_heap_open(pic_state *pic)
{
struct heap *heap;
heap = pic_malloc(pic, sizeof(struct heap));
heap->base.s.ptr = &heap->base;
heap->base.s.size = 0; /* not 1, since it must never be used for allocation */
heap->freep = &heap->base;
heap->pages = NULL;
heap->weaks = NULL;
return heap;
}
void
pic_heap_close(pic_state *pic, struct heap *heap)
{
struct heap_page *page;
while (heap->pages) {
page = heap->pages;
heap->pages = heap->pages->next;
pic_free(pic, page);
}
pic_free(pic, heap);
}
#if PIC_USE_LIBC
void *
pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
{
if (size != 0) {
return realloc(ptr, size);
}
free(ptr);
return NULL;
}
#endif
void *
pic_malloc(pic_state *pic, size_t size)
{
void *ptr;
ptr = pic->allocf(pic->userdata, NULL, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
return ptr;
}
void *
pic_realloc(pic_state *pic, void *ptr, size_t size)
{
ptr = pic->allocf(pic->userdata, ptr, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
return ptr;
}
void *
pic_calloc(pic_state *pic, size_t count, size_t size)
{
void *ptr;
size *= count;
ptr = pic->allocf(pic->userdata, NULL, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
memset(ptr, 0, size);
return ptr;
}
void
pic_free(pic_state *pic, void *ptr)
{
pic->allocf(pic->userdata, ptr, 0);
}
static void
gc_protect(pic_state *pic, struct object *obj)
{
if (pic->ai >= pic->arena_size) {
pic->arena_size = pic->arena_size * 2 + 1;
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size);
}
pic->arena[pic->ai++] = obj;
}
pic_value
pic_protect(pic_state *pic, pic_value v)
{
if (! obj_p(pic, v))
return v;
gc_protect(pic, obj_ptr(pic, v));
return v;
}
size_t
pic_enter(pic_state *pic)
{
return pic->ai;
}
void
pic_leave(pic_state *pic, size_t state)
{
pic->ai = state;
}
void *
pic_alloca(pic_state *pic, size_t n)
{
static const pic_data_type t = { "pic_alloca", pic_free };
return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t));
}
/* MARK */
PIC_STATIC_INLINE bool
is_alive(struct object *obj)
{
return obj->u.basic.tt & GC_MARK;
}
PIC_STATIC_INLINE void
mark(struct object *obj)
{
obj->u.basic.tt |= GC_MARK;
}
static void gc_mark_object(pic_state *, struct object *);
static void
gc_mark(pic_state *pic, pic_value v)
{
if (! obj_p(pic, v))
return;
gc_mark_object(pic, obj_ptr(pic, v));
}
static void
gc_mark_object(pic_state *pic, struct object *obj)
{
loop:
if (is_alive(obj))
return;
mark(obj);
#define LOOP(o) obj = (struct object *)(o); goto loop
switch (obj_type(pic, obj)) {
case PIC_TYPE_PAIR: {
gc_mark(pic, obj->u.pair.car);
if (obj_p(pic, obj->u.pair.cdr)) {
LOOP(obj_ptr(pic, obj->u.pair.cdr));
}
break;
}
case PIC_TYPE_FRAME: {
int i;
for (i = 0; i < obj->u.frame.regc; ++i) {
gc_mark(pic, obj->u.frame.regs[i]);
}
if (obj->u.frame.up) {
LOOP(obj->u.frame.up);
}
break;
}
case PIC_TYPE_PROC_FUNC: {
if (obj->u.proc.env) {
LOOP(obj->u.proc.env);
}
break;
}
case PIC_TYPE_PROC_IREP: {
if (obj->u.proc.env) {
gc_mark_object(pic, (struct object *)obj->u.proc.env);
}
LOOP(obj->u.proc.u.irep);
break;
}
case PIC_TYPE_IREP: {
size_t i;
for (i = 0; i < obj->u.irep.objc; ++i) {
gc_mark(pic, obj->u.irep.obj[i]);
}
for (i = 0; i < obj->u.irep.irepc; ++i) {
gc_mark_object(pic, (struct object *)obj->u.irep.irep[i]);
}
break;
}
case PIC_TYPE_PORT: {
break;
}
case PIC_TYPE_ERROR: {
gc_mark_object(pic, (struct object *)obj->u.err.type);
gc_mark(pic, obj->u.err.irrs);
LOOP(obj->u.err.msg);
break;
}
case PIC_TYPE_STRING: {
break;
}
case PIC_TYPE_VECTOR: {
int i;
for (i = 0; i < obj->u.vec.len; ++i) {
gc_mark(pic, obj->u.vec.data[i]);
}
break;
}
case PIC_TYPE_BLOB: {
break;
}
case PIC_TYPE_DATA: {
break;
}
case PIC_TYPE_DICT: {
pic_value key, val;
int it = 0;
while (pic_dict_next(pic, obj_value(pic, &obj->u.dict), &it, &key, &val)) {
gc_mark(pic, key);
gc_mark(pic, val);
}
break;
}
case PIC_TYPE_RECORD: {
gc_mark(pic, obj->u.rec.datum);
LOOP(obj->u.rec.type);
break;
}
case PIC_TYPE_SYMBOL: {
LOOP(obj->u.sym.str);
break;
}
case PIC_TYPE_WEAK: {
struct weak *weak = (struct weak *)obj;
weak->prev = pic->heap->weaks;
pic->heap->weaks = weak;
break;
}
default:
PIC_UNREACHABLE();
}
}
static void
gc_mark_phase(pic_state *pic)
{
struct context *cxt;
size_t j;
assert(pic->heap->weaks == NULL);
/* context */
for (cxt = pic->cxt; cxt != NULL; cxt = cxt->prev) {
if (cxt->fp) gc_mark_object(pic, (struct object *)cxt->fp);
if (cxt->sp) gc_mark_object(pic, (struct object *)cxt->sp);
if (cxt->irep) gc_mark_object(pic, (struct object *)cxt->irep);
}
/* arena */
for (j = 0; j < pic->ai; ++j) {
gc_mark_object(pic, (struct object *)pic->arena[j]);
}
/* global variables */
gc_mark(pic, pic->globals);
/* dynamic environment */
gc_mark(pic, pic->dyn_env);
/* top continuation */
gc_mark(pic, pic->halt);
/* features */
gc_mark(pic, pic->features);
/* weak maps */
do {
struct object *key;
pic_value val;
int it;
khash_t(weak) *h;
struct weak *weak;
j = 0;
weak = pic->heap->weaks;
while (weak != NULL) {
h = &weak->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
key = kh_key(h, it);
val = kh_val(h, it);
if (is_alive(key)) {
if (obj_p(pic, val) && ! is_alive(obj_ptr(pic, val))) {
gc_mark(pic, val);
++j;
}
}
}
weak = weak->prev;
}
} while (j > 0);
}
/* SWEEP */
static void
gc_finalize_object(pic_state *pic, struct object *obj)
{
switch (obj_type(pic, obj)) {
case PIC_TYPE_VECTOR: {
pic_free(pic, obj->u.vec.data);
break;
}
case PIC_TYPE_BLOB: {
pic_free(pic, obj->u.blob.data);
break;
}
case PIC_TYPE_STRING: {
pic_rope_decref(pic, obj->u.str.rope);
break;
}
case PIC_TYPE_DATA: {
if (obj->u.data.type->dtor) {
obj->u.data.type->dtor(pic, obj->u.data.data);
}
break;
}
case PIC_TYPE_DICT: {
kh_destroy(dict, &obj->u.dict.hash);
break;
}
case PIC_TYPE_SYMBOL: {
/* TODO: remove this symbol's entry from pic->syms immediately */
break;
}
case PIC_TYPE_WEAK: {
kh_destroy(weak, &obj->u.weak.hash);
break;
}
case PIC_TYPE_IREP: {
struct irep *irep = &obj->u.irep;
if ((irep->flags & IREP_CODE_STATIC) == 0) {
pic_free(pic, (code_t *) irep->code);
}
pic_free(pic, irep->obj);
pic_free(pic, irep->irep);
break;
}
case PIC_TYPE_PORT: {
pic_fclose(pic, obj_value(pic, obj)); /* FIXME */
break;
}
case PIC_TYPE_FRAME: {
pic_free(pic, obj->u.frame.regs);
break;
}
case PIC_TYPE_PAIR:
case PIC_TYPE_ERROR:
case PIC_TYPE_RECORD:
case PIC_TYPE_PROC_FUNC:
case PIC_TYPE_PROC_IREP:
break;
default:
PIC_UNREACHABLE();
}
}
static size_t
type2size(int type)
{
switch (type) {
case PIC_TYPE_VECTOR: return sizeof(struct vector);
case PIC_TYPE_BLOB: return sizeof(struct blob);
case PIC_TYPE_STRING: return sizeof(struct string);
case PIC_TYPE_DATA: return sizeof(struct data);
case PIC_TYPE_DICT: return sizeof(struct dict);
case PIC_TYPE_SYMBOL: return sizeof(struct symbol);
case PIC_TYPE_WEAK: return sizeof(struct weak);
case PIC_TYPE_IREP: return sizeof(struct irep);
case PIC_TYPE_PORT: return sizeof(struct port);
case PIC_TYPE_PAIR: return sizeof(struct pair);
case PIC_TYPE_FRAME: return sizeof(struct frame);
case PIC_TYPE_ERROR: return sizeof(struct error);
case PIC_TYPE_RECORD: return sizeof(struct record);
case PIC_TYPE_PROC_FUNC: return sizeof(struct proc);
case PIC_TYPE_PROC_IREP: return sizeof(struct proc);
default: PIC_UNREACHABLE();
}
}
static struct object *
obj_alloc(pic_state *pic, int type)
{
union header *p, *prevp;
struct object *obj;
size_t nunits;
nunits = unitsof(type);
prevp = pic->heap->freep;
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if (p->s.size >= nunits)
break;
if (p == pic->heap->freep) {
return NULL;
}
}
if (p->s.size == nunits) {
prevp->s.ptr = p->s.ptr;
}
else {
p->s.size -= nunits;
p += p->s.size;
p->s.size = nunits;
}
pic->heap->freep = prevp;
obj = (struct object *) p;
obj->u.basic.tt = type;
return obj;
}
static void
free_chunk(pic_state *pic, union header *bp)
{
union header *p;
assert(bp != NULL);
for (p = pic->heap->freep; ! (bp > p && bp < p->s.ptr); p = p->s.ptr) {
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr)) {
break;
}
}
if (bp + bp->s.size == p->s.ptr && p->s.ptr->s.size > 0) { /* don't melt base header */
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;
} else {
bp->s.ptr = p->s.ptr;
}
if (p + p->s.size == bp && bp->s.size > 0) { /* don't melt base header */
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
} else {
p->s.ptr = bp;
}
pic->heap->freep = p;
}
static void
heap_morecore(pic_state *pic)
{
union header *bp, *np;
struct heap_page *page;
assert(PAGE_UNITS >= 2);
page = pic_malloc(pic, PIC_HEAP_PAGE_SIZE);
page->next = pic->heap->pages;
bp = page->u.basep;
bp->s.size = 0; /* bp is never used for allocation */
free_chunk(pic, bp);
np = page->u.basep + 1;
np->s.size = PAGE_UNITS - 1;
free_chunk(pic, np);
pic->heap->pages = page;
}
static size_t
gc_sweep_page(pic_state *pic, struct heap_page *page)
{
union header *bp, *p, *head = NULL, *tail = NULL;
struct object *obj;
size_t alive = 0, nunits;
for (bp = page->u.basep; ; bp = bp->s.ptr) {
p = bp + (bp->s.size ? bp->s.size : 1); /* first bp's size is 0, so force advnce */
while (p != bp->s.ptr) {
if (p < page->u.basep || page->u.basep + PAGE_UNITS <= p) {
goto escape;
}
obj = (struct object *) p;
nunits = unitsof(obj_type(pic, obj));
if (obj->u.basic.tt & GC_MARK) {
obj->u.basic.tt &= ~GC_MARK;
alive += nunits;
} else {
gc_finalize_object(pic, obj);
if (head == NULL) {
head = p;
}
if (tail != NULL) {
tail->s.ptr = p;
}
tail = p;
tail->s.size = nunits;
tail->s.ptr = NULL; /* We can safely reuse ptr field of dead object */
}
p += nunits;
}
}
escape:
/* free! */
while (head != NULL) {
p = head;
head = head->s.ptr;
free_chunk(pic, p);
}
return alive;
}
static void
gc_sweep_phase(pic_state *pic)
{
struct heap_page *page;
int it;
khash_t(weak) *h;
khash_t(oblist) *s = &pic->oblist;
struct symbol *sym;
struct object *obj;
size_t total = 0, inuse = 0;
/* weak maps */
while (pic->heap->weaks != NULL) {
h = &pic->heap->weaks->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
obj = kh_key(h, it);
if (! is_alive(obj)) {
kh_del(weak, h, it);
}
}
pic->heap->weaks = pic->heap->weaks->prev;
}
/* symbol table */
for (it = kh_begin(s); it != kh_end(s); ++it) {
if (! kh_exist(s, it))
continue;
sym = kh_val(s, it);
if (sym && ! is_alive((struct object *)sym)) {
kh_del(oblist, s, it);
}
}
page = pic->heap->pages;
while (page) {
inuse += gc_sweep_page(pic, page);
total += PAGE_UNITS;
page = page->next;
}
if (PIC_PAGE_REQUEST_THRESHOLD(total) <= inuse) {
heap_morecore(pic);
}
}
void
pic_gc(pic_state *pic)
{
if (! pic->gc_enable) {
return;
}
gc_mark_phase(pic);
gc_sweep_phase(pic);
}
struct object *
pic_obj_alloc_unsafe(pic_state *pic, int type)
{
struct object *obj;
if (pic->heap->pages == NULL) {
heap_morecore(pic);
}
#if GC_STRESS
pic_gc(pic);
#endif
obj = obj_alloc(pic, type);
if (obj == NULL) {
pic_gc(pic);
obj = obj_alloc(pic, type);
if (obj == NULL)
pic_panic(pic, "GC memory exhausted");
}
return obj;
}
struct object *
pic_obj_alloc(pic_state *pic, int type)
{
struct object *obj;
obj = pic_obj_alloc_unsafe(pic, type);
gc_protect(pic, obj);
return obj;
}