picrin/extlib/benz/gc.c

771 lines
15 KiB
C
Raw Normal View History

2014-08-25 00:38:09 -04:00
/**
* See Copyright Notice in picrin.h
*/
#include "picrin.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/object.h"
#include "picrin/private/khash.h"
2016-02-20 11:13:16 -05:00
#include "picrin/private/state.h"
2014-08-25 00:38:09 -04:00
2016-02-20 09:01:12 -05:00
enum {
WHITE = 0,
BLACK = 1
};
2014-08-25 00:38:09 -04:00
union header {
struct {
union header *ptr;
size_t size;
} s;
};
2015-07-24 02:42:30 -04:00
#define BITMAP_SIZE \
(PIC_HEAP_PAGE_SIZE / sizeof(union header)) / (sizeof(uint32_t) * CHAR_BIT)
2014-08-25 00:38:09 -04:00
struct heap_page {
union header *basep, *endp;
2015-07-24 02:42:30 -04:00
uint32_t bitmap[BITMAP_SIZE];
2014-08-25 00:38:09 -04:00
struct heap_page *next;
};
2016-02-21 06:32:00 -05:00
struct object {
union {
2016-02-21 06:32:00 -05:00
struct basic basic;
struct identifier id;
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 env env;
struct proc proc;
struct context cxt;
struct port port;
struct error err;
struct checkpoint cp;
} u;
2015-07-20 02:02:16 -04:00
};
2016-02-21 06:32:00 -05:00
struct heap {
2014-08-25 00:38:09 -04:00
union header base, *freep;
struct heap_page *pages;
2016-02-21 06:32:00 -05:00
struct weak *weaks; /* weak map chain */
2014-08-25 00:38:09 -04:00
};
2016-02-21 06:32:00 -05:00
struct heap *
2015-07-15 10:32:31 -04:00
pic_heap_open(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct heap *heap;
2015-07-15 10:32:31 -04:00
2016-02-21 06:32:00 -05:00
heap = pic_malloc(pic, sizeof(struct heap));
2015-07-15 10:32:31 -04:00
2014-08-25 00:38:09 -04:00
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;
2016-02-10 07:50:39 -05:00
heap->weaks = NULL;
2014-08-25 00:38:09 -04:00
return heap;
}
void
2016-02-21 06:32:00 -05:00
pic_heap_close(pic_state *pic, struct heap *heap)
2014-08-25 00:38:09 -04:00
{
struct heap_page *page;
while (heap->pages) {
page = heap->pages;
heap->pages = heap->pages->next;
2015-07-16 02:28:30 -04:00
pic_free(pic, page->basep);
pic_free(pic, page);
2014-08-25 00:38:09 -04:00
}
2015-07-16 02:28:30 -04:00
pic_free(pic, heap);
2014-08-25 00:38:09 -04:00
}
2016-02-22 14:37:58 -05:00
#if PIC_USE_LIBC
2015-05-27 13:12:26 -04:00
void *
2016-02-21 05:19:35 -05:00
pic_default_allocf(void *PIC_UNUSED(userdata), void *ptr, size_t size)
2014-08-25 00:38:09 -04:00
{
2015-08-10 08:17:26 -04:00
if (size != 0) {
2014-08-25 00:38:09 -04:00
return realloc(ptr, size);
}
2015-08-10 08:17:26 -04:00
free(ptr);
return NULL;
2014-08-25 00:38:09 -04:00
}
2015-05-27 13:12:26 -04:00
#endif
2014-08-25 00:38:09 -04:00
void *
2015-05-28 03:42:16 -04:00
pic_malloc(pic_state *pic, size_t size)
2014-08-25 00:38:09 -04:00
{
void *ptr;
2015-08-10 08:13:23 -04:00
ptr = pic->allocf(pic->userdata, NULL, size);
2014-08-25 00:38:09 -04:00
if (ptr == NULL && size > 0) {
2014-09-16 12:02:27 -04:00
pic_panic(pic, "memory exhausted");
2014-08-25 00:38:09 -04:00
}
return ptr;
}
void *
pic_realloc(pic_state *pic, void *ptr, size_t size)
{
2015-08-10 08:13:23 -04:00
ptr = pic->allocf(pic->userdata, ptr, size);
2014-08-25 00:38:09 -04:00
if (ptr == NULL && size > 0) {
2014-09-16 12:02:27 -04:00
pic_panic(pic, "memory exhausted");
2014-08-25 00:38:09 -04:00
}
return ptr;
}
void *
pic_calloc(pic_state *pic, size_t count, size_t size)
{
void *ptr;
size *= count;
2015-08-10 08:13:23 -04:00
ptr = pic->allocf(pic->userdata, NULL, size);
2014-08-25 00:38:09 -04:00
if (ptr == NULL && size > 0) {
2014-09-16 12:02:27 -04:00
pic_panic(pic, "memory exhausted");
2014-08-25 00:38:09 -04:00
}
memset(ptr, 0, size);
return ptr;
}
void
pic_free(pic_state *pic, void *ptr)
{
2015-08-10 08:13:23 -04:00
pic->allocf(pic->userdata, ptr, 0);
2014-08-25 00:38:09 -04:00
}
static void
2016-02-21 06:32:00 -05:00
gc_protect(pic_state *pic, struct object *obj)
2014-08-25 00:38:09 -04:00
{
if (pic->arena_idx >= pic->arena_size) {
pic->arena_size = pic->arena_size * 2 + 1;
2016-02-21 06:32:00 -05:00
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct object *) * pic->arena_size);
2014-08-25 00:38:09 -04:00
}
pic->arena[pic->arena_idx++] = obj;
}
pic_value
2016-02-19 02:17:13 -05:00
pic_protect(pic_state *pic, pic_value v)
2014-08-25 00:38:09 -04:00
{
if (! pic_obj_p(pic, v))
2014-08-25 00:38:09 -04:00
return v;
gc_protect(pic, pic_obj_ptr(v));
2014-08-25 00:38:09 -04:00
return v;
}
size_t
2016-02-19 02:17:13 -05:00
pic_enter(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
return pic->arena_idx;
}
void
2016-02-19 02:17:13 -05:00
pic_leave(pic_state *pic, size_t state)
2014-08-25 00:38:09 -04:00
{
pic->arena_idx = state;
}
static void *
heap_alloc(pic_state *pic, size_t size)
2014-08-25 00:38:09 -04:00
{
2015-07-16 02:42:44 -04:00
union header *p, *prevp;
2014-08-25 00:38:09 -04:00
size_t nunits;
assert(size > 0);
nunits = (size + sizeof(union header) - 1) / sizeof(union header) + 1;
2015-07-16 02:42:44 -04:00
prevp = pic->heap->freep;
2014-08-25 00:38:09 -04:00
for (p = prevp->s.ptr; ; prevp = p, p = p->s.ptr) {
if (p->s.size >= nunits)
break;
2015-07-16 02:42:44 -04:00
if (p == pic->heap->freep) {
2014-08-25 00:38:09 -04:00
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;
return (void *)(p + 1);
}
static void
2015-07-16 02:41:43 -04:00
heap_free(pic_state *pic, void *ap)
2014-08-25 00:38:09 -04:00
{
2015-07-16 02:42:44 -04:00
union header *bp, *p;
2015-07-16 02:41:43 -04:00
assert(ap != NULL);
bp = (union header *)ap - 1;
2014-08-25 00:38:09 -04:00
2015-07-16 02:42:44 -04:00
for (p = pic->heap->freep; ! (bp > p && bp < p->s.ptr); p = p->s.ptr) {
2014-08-25 00:38:09 -04:00
if (p >= p->s.ptr && (bp > p || bp < p->s.ptr)) {
break;
}
}
2015-07-16 03:13:30 -04:00
if (bp + bp->s.size == p->s.ptr && p->s.ptr->s.size > 0) { /* don't melt base header */
2014-08-25 00:38:09 -04:00
bp->s.size += p->s.ptr->s.size;
bp->s.ptr = p->s.ptr->s.ptr;
2015-07-16 02:45:01 -04:00
} else {
2014-08-25 00:38:09 -04:00
bp->s.ptr = p->s.ptr;
}
if (p + p->s.size == bp && bp->s.size > 0) { /* don't melt base header */
2014-08-25 00:38:09 -04:00
p->s.size += bp->s.size;
p->s.ptr = bp->s.ptr;
2015-07-16 02:45:01 -04:00
} else {
2014-08-25 00:38:09 -04:00
p->s.ptr = bp;
}
pic->heap->freep = p;
}
2015-07-16 02:33:31 -04:00
static void
heap_morecore(pic_state *pic)
2015-07-16 02:33:31 -04:00
{
2015-07-16 05:44:23 -04:00
union header *bp, *np;
2015-07-16 02:33:31 -04:00
struct heap_page *page;
2015-07-16 05:44:23 -04:00
size_t nunits;
2015-07-16 02:33:31 -04:00
2015-07-16 05:44:23 -04:00
nunits = PIC_HEAP_PAGE_SIZE / sizeof(union header);
2015-07-16 03:13:30 -04:00
2015-07-16 05:44:23 -04:00
assert(nunits >= 2);
2015-07-16 02:33:31 -04:00
2015-07-16 05:44:23 -04:00
bp = pic_malloc(pic, PIC_HEAP_PAGE_SIZE);
bp->s.size = 0; /* bp is never used for allocation */
heap_free(pic, bp + 1);
2015-07-16 02:33:31 -04:00
2015-07-16 05:44:23 -04:00
np = bp + 1;
np->s.size = nunits - 1;
2015-07-16 03:13:30 -04:00
heap_free(pic, np + 1);
2015-07-16 02:33:31 -04:00
page = pic_malloc(pic, sizeof(struct heap_page));
2015-07-16 05:44:23 -04:00
page->basep = bp;
page->endp = bp + nunits;
2015-07-16 02:33:31 -04:00
page->next = pic->heap->pages;
pic->heap->pages = page;
}
2015-07-24 02:42:30 -04:00
/* bitmap */
static struct heap_page *
obj2page(pic_state *pic, union header *h)
{
struct heap_page *page;
page = pic->heap->pages;
while (page) {
if (page->basep <= h && h < page->endp)
return page;
page = page->next;
}
PIC_UNREACHABLE();
}
static bool
is_marked(pic_state *pic, struct object *obj)
2015-07-24 02:42:30 -04:00
{
union header *h = ((union header *)obj) - 1;
struct heap_page *page;
size_t i, unit_size = sizeof(uint32_t) * CHAR_BIT;
page = obj2page(pic, h);
i = h - page->basep;
return page->bitmap[i / unit_size] & (1 << (i % unit_size));
}
static void
mark(pic_state *pic, struct object *obj)
2015-07-24 02:42:30 -04:00
{
union header *h = ((union header *)obj) - 1;
struct heap_page *page;
size_t i, unit_size = sizeof(uint32_t) * CHAR_BIT;
page = obj2page(pic, h);
i = h - page->basep;
page->bitmap[i / unit_size] |= (1 << (i % unit_size));
}
2015-07-20 02:18:12 -04:00
/* MARK */
2014-08-25 00:38:09 -04:00
2016-02-21 06:32:00 -05:00
static void gc_mark_object(pic_state *, struct object *);
2014-08-25 00:38:09 -04:00
static void
gc_mark(pic_state *pic, pic_value v)
{
if (! pic_obj_p(pic, v))
return;
gc_mark_object(pic, pic_obj_ptr(v));
}
2014-08-25 00:38:09 -04:00
static void
2016-02-21 06:32:00 -05:00
gc_mark_object(pic_state *pic, struct object *obj)
2014-08-25 00:38:09 -04:00
{
2015-07-20 01:24:58 -04:00
loop:
if (is_marked(pic, obj))
2014-08-25 00:38:09 -04:00
return;
2015-07-16 04:23:29 -04:00
mark(pic, obj);
2014-08-25 00:38:09 -04:00
2016-02-21 06:32:00 -05:00
#define LOOP(o) obj = (struct object *)(o); goto loop
2015-07-20 01:24:58 -04:00
switch (obj->u.basic.tt) {
case PIC_TYPE_PAIR: {
gc_mark(pic, obj->u.pair.car);
if (pic_obj_p(pic, obj->u.pair.cdr)) {
LOOP(pic_obj_ptr(obj->u.pair.cdr));
2015-07-20 01:24:58 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_CXT: {
2014-08-25 00:38:09 -04:00
int i;
for (i = 0; i < obj->u.cxt.regc; ++i) {
gc_mark(pic, obj->u.cxt.regs[i]);
2014-08-25 00:38:09 -04:00
}
if (obj->u.cxt.up) {
LOOP(obj->u.cxt.up);
2014-08-25 00:38:09 -04:00
}
break;
}
2016-02-21 06:42:41 -05:00
case PIC_TYPE_FUNC: {
int i;
for (i = 0; i < obj->u.proc.u.f.localc; ++i) {
gc_mark(pic, obj->u.proc.locals[i]);
}
break;
}
case PIC_TYPE_IREP: {
if (obj->u.proc.u.i.cxt) {
LOOP(obj->u.proc.u.i.cxt);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TYPE_PORT: {
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_ERROR: {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)obj->u.err.type);
gc_mark_object(pic, (struct object *)obj->u.err.msg);
gc_mark(pic, obj->u.err.irrs);
LOOP(obj->u.err.stack);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_STRING: {
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_VECTOR: {
2015-08-26 06:04:27 -04:00
int i;
for (i = 0; i < obj->u.vec.len; ++i) {
gc_mark(pic, obj->u.vec.data[i]);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TYPE_BLOB: {
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_ID: {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)obj->u.id.u.id);
2016-02-19 15:18:13 -05:00
LOOP(obj->u.id.env);
break;
}
case PIC_TYPE_ENV: {
khash_t(env) *h = &obj->u.env.map;
2016-02-20 11:52:34 -05:00
int it;
2014-08-25 00:38:09 -04:00
2015-06-24 18:26:48 -04:00
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (kh_exist(h, it)) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)kh_key(h, it));
gc_mark_object(pic, (struct object *)kh_val(h, it));
2015-06-24 18:26:48 -04:00
}
}
if (obj->u.env.up) {
LOOP(obj->u.env.up);
2015-07-20 01:24:58 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_DATA: {
if (obj->u.data.type->mark) {
obj->u.data.type->mark(pic, obj->u.data.data, gc_mark);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TYPE_DICT: {
2016-02-20 01:31:14 -05:00
pic_value key, val;
2016-02-18 10:39:13 -05:00
int it = 0;
2014-08-25 00:38:09 -04:00
2016-02-20 01:31:14 -05:00
while (pic_dict_next(pic, pic_obj_value(&obj->u.dict), &it, &key, &val)) {
gc_mark(pic, key);
2016-02-18 10:39:13 -05:00
gc_mark(pic, val);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TYPE_RECORD: {
2016-02-06 12:58:18 -05:00
gc_mark(pic, obj->u.rec.type);
if (pic_obj_p(pic, obj->u.rec.datum)) {
2016-02-06 12:58:18 -05:00
LOOP(pic_obj_ptr(obj->u.rec.datum));
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_SYMBOL: {
2016-02-19 15:18:13 -05:00
LOOP(obj->u.id.u.str);
break;
}
case PIC_TYPE_WEAK: {
2016-02-21 06:32:00 -05:00
struct weak *weak = (struct weak *)obj;
2015-06-09 05:31:46 -04:00
2016-02-10 07:50:39 -05:00
weak->prev = pic->heap->weaks;
pic->heap->weaks = weak;
2015-06-09 05:31:46 -04:00
break;
}
case PIC_TYPE_CP: {
if (obj->u.cp.prev) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)obj->u.cp.prev);
2015-06-22 04:06:13 -04:00
}
if (obj->u.cp.in) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)obj->u.cp.in);
2015-06-22 04:06:13 -04:00
}
if (obj->u.cp.out) {
2016-02-21 06:32:00 -05:00
LOOP((struct object *)obj->u.cp.out);
2015-06-22 04:06:13 -04:00
}
break;
}
default:
PIC_UNREACHABLE();
2014-08-25 00:38:09 -04:00
}
}
static void
gc_mark_phase(pic_state *pic)
{
pic_value *stack;
2016-02-21 06:32:00 -05:00
struct callinfo *ci;
struct list_head *list;
2016-02-20 11:52:34 -05:00
int it;
2014-09-18 01:50:01 -04:00
size_t j;
2015-06-09 05:31:46 -04:00
2016-02-10 07:50:39 -05:00
assert(pic->heap->weaks == NULL);
2014-08-25 00:38:09 -04:00
2015-06-08 08:04:04 -04:00
/* checkpoint */
if (pic->cp) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)pic->cp);
2014-08-25 00:38:09 -04:00
}
/* stack */
for (stack = pic->stbase; stack != pic->sp; ++stack) {
gc_mark(pic, *stack);
}
/* callinfo */
for (ci = pic->ci; ci != pic->cibase; --ci) {
2015-05-30 09:34:51 -04:00
if (ci->cxt) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)ci->cxt);
2014-08-25 00:38:09 -04:00
}
}
/* arena */
for (j = 0; j < pic->arena_idx; ++j) {
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)pic->arena[j]);
2014-08-25 00:38:09 -04:00
}
2016-02-04 10:30:11 -05:00
/* ireps */
for (list = pic->ireps.next; list != &pic->ireps; list = list->next) {
2016-02-21 06:32:00 -05:00
struct irep *irep = (struct irep *)list;
2016-02-06 04:15:15 -05:00
for (j = 0; j < irep->npool; ++j) {
2016-02-06 07:11:22 -05:00
gc_mark_object(pic, irep->pool[j]);
2016-02-04 10:30:11 -05:00
}
}
2014-08-25 00:38:09 -04:00
/* global variables */
2016-02-19 14:35:15 -05:00
gc_mark(pic, pic->globals);
2014-08-25 00:38:09 -04:00
/* macro objects */
2016-02-19 14:35:15 -05:00
gc_mark(pic, pic->macros);
2015-06-09 05:31:46 -04:00
2014-09-18 01:50:01 -04:00
/* error object */
gc_mark(pic, pic->err);
2014-08-25 00:38:09 -04:00
2014-09-18 01:50:01 -04:00
/* features */
2014-09-09 12:41:10 -04:00
gc_mark(pic, pic->features);
2014-08-25 00:38:09 -04:00
/* library table */
for (it = kh_begin(&pic->ltable); it != kh_end(&pic->ltable); ++it) {
if (! kh_exist(&pic->ltable, it)) {
continue;
}
2016-02-21 06:32:00 -05:00
gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).name);
gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).env);
gc_mark_object(pic, (struct object *)kh_val(&pic->ltable, it).exports);
}
2015-06-08 09:28:17 -04:00
2016-02-10 07:50:39 -05:00
/* weak maps */
2014-09-24 23:12:52 -04:00
do {
2016-02-21 06:32:00 -05:00
struct object *key;
2015-06-09 05:31:46 -04:00
pic_value val;
2016-02-20 11:52:34 -05:00
int it;
2016-02-10 07:50:39 -05:00
khash_t(weak) *h;
2016-02-21 06:32:00 -05:00
struct weak *weak;
2014-09-24 23:12:52 -04:00
2015-06-09 05:31:46 -04:00
j = 0;
2016-02-10 07:50:39 -05:00
weak = pic->heap->weaks;
2015-06-09 05:31:46 -04:00
2016-02-10 07:50:39 -05:00
while (weak != NULL) {
h = &weak->hash;
2015-06-24 18:14:29 -04:00
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);
2015-07-24 02:42:30 -04:00
if (is_marked(pic, key)) {
if (pic_obj_p(pic, val) && ! is_marked(pic, pic_obj_ptr(val))) {
gc_mark(pic, val);
++j;
}
2014-09-24 23:12:52 -04:00
}
}
2016-02-10 07:50:39 -05:00
weak = weak->prev;
2014-09-24 23:12:52 -04:00
}
} while (j > 0);
2014-08-25 00:38:09 -04:00
}
2015-07-20 02:18:12 -04:00
/* SWEEP */
2014-08-25 00:38:09 -04:00
static void
2016-02-21 06:32:00 -05:00
gc_finalize_object(pic_state *pic, struct object *obj)
2014-08-25 00:38:09 -04:00
{
switch (obj->u.basic.tt) {
case PIC_TYPE_VECTOR: {
pic_free(pic, obj->u.vec.data);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_BLOB: {
pic_free(pic, obj->u.blob.data);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_STRING: {
pic_rope_decref(pic, obj->u.str.rope);
break;
}
case PIC_TYPE_ENV: {
kh_destroy(env, &obj->u.env.map);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_DATA: {
if (obj->u.data.type->dtor) {
obj->u.data.type->dtor(pic, obj->u.data.data);
2015-06-22 14:02:12 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_DICT: {
kh_destroy(dict, &obj->u.dict.hash);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_SYMBOL: {
2016-02-07 13:36:20 -05:00
/* TODO: remove this symbol's entry from pic->syms immediately */
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TYPE_WEAK: {
2016-02-10 07:50:39 -05:00
kh_destroy(weak, &obj->u.weak.hash);
break;
}
2016-02-21 06:42:41 -05:00
case PIC_TYPE_IREP: {
pic_irep_decref(pic, obj->u.proc.u.i.irep);
2015-06-09 05:31:46 -04:00
break;
}
2015-07-20 02:02:16 -04:00
case PIC_TYPE_PAIR:
case PIC_TYPE_CXT:
case PIC_TYPE_PORT:
case PIC_TYPE_ERROR:
case PIC_TYPE_ID:
case PIC_TYPE_RECORD:
case PIC_TYPE_CP:
2016-02-21 06:42:41 -05:00
case PIC_TYPE_FUNC:
2015-06-22 04:06:13 -04:00
break;
2015-07-20 02:02:16 -04:00
default:
PIC_UNREACHABLE();
2014-08-25 00:38:09 -04:00
}
}
2015-07-16 03:34:59 -04:00
static size_t
2014-08-25 00:38:09 -04:00
gc_sweep_page(pic_state *pic, struct heap_page *page)
{
2015-07-16 02:45:01 -04:00
union header *bp, *p, *head = NULL, *tail = NULL;
2016-02-21 06:32:00 -05:00
struct object *obj;
2015-07-16 03:34:59 -04:00
size_t alive = 0;
2014-08-25 00:38:09 -04:00
for (bp = page->basep; ; bp = bp->s.ptr) {
2015-07-16 03:13:30 -04:00
p = bp + (bp->s.size ? bp->s.size : 1); /* first bp's size is 0, so force advnce */
for (; p != bp->s.ptr; p += p->s.size) {
if (p < page->basep || page->endp <= p) {
2015-07-16 02:45:01 -04:00
goto escape;
2014-08-25 00:38:09 -04:00
}
2016-02-21 06:32:00 -05:00
obj = (struct object *)(p + 1);
if (is_marked(pic, obj)) {
2015-07-16 03:34:59 -04:00
alive += p->s.size;
2015-07-16 02:45:01 -04:00
} else {
if (head == NULL) {
head = p;
2015-07-19 05:12:50 -04:00
}
if (tail != NULL) {
2015-07-16 02:45:01 -04:00
tail->s.ptr = p;
}
tail = p;
tail->s.ptr = NULL; /* We can safely reuse ptr field of dead object */
2014-08-25 00:38:09 -04:00
}
}
}
escape:
/* free! */
2015-07-16 02:45:01 -04:00
while (head != NULL) {
p = head;
head = head->s.ptr;
2016-02-21 06:32:00 -05:00
gc_finalize_object(pic, (struct object *)(p + 1));
2015-07-16 02:45:01 -04:00
heap_free(pic, p + 1);
2014-08-25 00:38:09 -04:00
}
2015-07-16 03:34:59 -04:00
return alive;
2014-08-25 00:38:09 -04:00
}
static void
gc_sweep_phase(pic_state *pic)
{
2015-07-16 03:34:59 -04:00
struct heap_page *page;
2016-02-20 11:52:34 -05:00
int it;
2016-02-10 07:50:39 -05:00
khash_t(weak) *h;
2016-02-14 22:59:58 -05:00
khash_t(oblist) *s = &pic->oblist;
symbol *sym;
2016-02-21 06:32:00 -05:00
struct object *obj;
2015-07-16 03:34:59 -04:00
size_t total = 0, inuse = 0;
2014-09-24 23:12:52 -04:00
2016-02-10 07:50:39 -05:00
/* weak maps */
while (pic->heap->weaks != NULL) {
h = &pic->heap->weaks->hash;
2015-06-24 18:14:29 -04:00
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
2015-07-16 04:23:29 -04:00
obj = kh_key(h, it);
2015-07-24 02:42:30 -04:00
if (! is_marked(pic, obj)) {
2016-02-10 07:50:39 -05:00
kh_del(weak, h, it);
2014-09-24 23:12:52 -04:00
}
}
2016-02-10 07:50:39 -05:00
pic->heap->weaks = pic->heap->weaks->prev;
2015-06-09 05:31:46 -04:00
}
2014-08-25 00:38:09 -04:00
2015-07-20 02:18:12 -04:00
/* symbol table */
for (it = kh_begin(s); it != kh_end(s); ++it) {
if (! kh_exist(s, it))
continue;
sym = kh_val(s, it);
if (! is_marked(pic, (struct object *)sym)) {
2016-02-14 22:59:58 -05:00
kh_del(oblist, s, it);
2015-07-20 02:18:12 -04:00
}
}
2015-01-20 03:38:34 -05:00
2015-07-16 03:34:59 -04:00
page = pic->heap->pages;
2014-08-25 00:38:09 -04:00
while (page) {
2015-07-16 03:34:59 -04:00
inuse += gc_sweep_page(pic, page);
total += page->endp - page->basep;
2014-08-25 00:38:09 -04:00
page = page->next;
}
2015-07-16 03:34:59 -04:00
if (PIC_PAGE_REQUEST_THRESHOLD(total) <= inuse) {
heap_morecore(pic);
}
2014-08-25 00:38:09 -04:00
}
2015-07-24 02:42:30 -04:00
void
gc_init(pic_state *pic)
{
struct heap_page *page;
page = pic->heap->pages;
while (page) {
/* clear mark bits */
memset(page->bitmap, 0, sizeof(page->bitmap));
page = page->next;
}
}
2014-08-25 00:38:09 -04:00
void
2016-02-13 23:17:12 -05:00
pic_gc(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2015-01-18 22:14:29 -05:00
if (! pic->gc_enable) {
return;
}
2015-07-24 02:42:30 -04:00
gc_init(pic);
2014-08-25 00:38:09 -04:00
gc_mark_phase(pic);
gc_sweep_phase(pic);
}
2016-02-13 23:19:54 -05:00
void *
pic_alloca(pic_state *pic, size_t n)
{
static const pic_data_type t = { "pic_alloca", pic_free, 0 };
/* TODO: optimize */
2016-02-19 09:30:47 -05:00
return pic_data(pic, pic_data_value(pic, pic_malloc(pic, n), &t));
2016-02-13 23:19:54 -05:00
}
2016-02-21 06:32:00 -05:00
struct object *
pic_obj_alloc_unsafe(pic_state *pic, size_t size, int type)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct object *obj;
2014-08-25 00:38:09 -04:00
#if GC_STRESS
2016-02-13 23:17:12 -05:00
pic_gc(pic);
2014-08-25 00:38:09 -04:00
#endif
2016-02-21 06:32:00 -05:00
obj = (struct object *)heap_alloc(pic, size);
2014-08-25 00:38:09 -04:00
if (obj == NULL) {
2016-02-13 23:17:12 -05:00
pic_gc(pic);
2016-02-21 06:32:00 -05:00
obj = (struct object *)heap_alloc(pic, size);
2014-08-25 00:38:09 -04:00
if (obj == NULL) {
heap_morecore(pic);
2016-02-21 06:32:00 -05:00
obj = (struct object *)heap_alloc(pic, size);
2014-08-25 00:38:09 -04:00
if (obj == NULL)
2014-09-16 12:02:27 -04:00
pic_panic(pic, "GC memory exhausted");
2014-08-25 00:38:09 -04:00
}
}
obj->u.basic.tt = type;
2014-08-25 00:38:09 -04:00
return obj;
}
2016-02-21 06:32:00 -05:00
struct object *
pic_obj_alloc(pic_state *pic, size_t size, int type)
2014-08-25 00:38:09 -04:00
{
2016-02-21 06:32:00 -05:00
struct object *obj;
2014-08-25 00:38:09 -04:00
obj = pic_obj_alloc_unsafe(pic, size, type);
2014-08-25 00:38:09 -04:00
gc_protect(pic, obj);
return obj;
}