picrin/extlib/benz/gc.c

769 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"
union header {
struct {
union header *ptr;
size_t size;
2015-05-27 09:49:29 -04:00
char mark;
2014-08-25 00:38:09 -04:00
} s;
};
struct heap_page {
union header *basep, *endp;
struct heap_page *next;
};
2015-07-20 02:02:16 -04:00
union object {
struct pic_object obj;
struct pic_symbol sym;
struct pic_string str;
struct pic_blob blob;
struct pic_pair pair;
struct pic_vector vec;
struct pic_dict dict;
struct pic_reg reg;
struct pic_data data;
struct pic_record rec;
struct pic_id id;
struct pic_env env;
struct pic_proc proc;
struct pic_context cxt;
struct pic_irep irep;
struct pic_port port;
struct pic_error err;
struct pic_lib lib;
struct pic_checkpoint cp;
};
2014-08-25 00:38:09 -04:00
struct pic_heap {
union header base, *freep;
struct heap_page *pages;
};
2015-07-15 10:32:31 -04:00
struct pic_heap *
pic_heap_open(pic_state *pic)
2014-08-25 00:38:09 -04:00
{
2015-07-15 10:32:31 -04:00
struct pic_heap *heap;
2015-07-16 02:31:11 -04:00
heap = pic_malloc(pic, sizeof(struct pic_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;
return heap;
}
void
pic_heap_close(pic_state *pic, struct pic_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
}
2015-05-27 13:12:26 -04:00
#if PIC_ENABLE_LIBC
void *
pic_default_allocf(void *ptr, size_t size)
2014-08-25 00:38:09 -04:00
{
if (size == 0) {
if (ptr) {
free(ptr);
}
return NULL;
}
if (ptr) {
return realloc(ptr, size);
} else {
return malloc(size);
}
}
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-05-27 13:12:26 -04:00
ptr = pic->allocf(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-05-27 13:12:26 -04:00
ptr = pic->allocf(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-05-27 13:12:26 -04:00
ptr = pic->allocf(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-05-27 13:12:26 -04:00
pic->allocf(ptr, 0);
2014-08-25 00:38:09 -04:00
}
static void
gc_protect(pic_state *pic, struct pic_object *obj)
{
if (pic->arena_idx >= pic->arena_size) {
pic->arena_size = pic->arena_size * 2 + 1;
pic->arena = pic_realloc(pic, pic->arena, sizeof(struct pic_object *) * pic->arena_size);
}
pic->arena[pic->arena_idx++] = obj;
}
pic_value
pic_gc_protect(pic_state *pic, pic_value v)
{
if (! pic_obj_p(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
pic_gc_arena_preserve(pic_state *pic)
{
return pic->arena_idx;
}
void
pic_gc_arena_restore(pic_state *pic, size_t state)
{
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-20 02:18:12 -04:00
/* MARK */
2014-08-25 00:38:09 -04:00
2015-07-20 02:18:12 -04:00
static void gc_mark_object(pic_state *, union object *);
2014-08-25 00:38:09 -04:00
2014-09-24 23:12:52 -04:00
static bool
gc_obj_is_marked(struct pic_object *obj)
{
union header *p;
p = ((union header *)obj) - 1;
2015-07-20 02:18:12 -04:00
return p->s.mark == PIC_GC_MARK;
2014-09-24 23:12:52 -04:00
}
static void
gc_unmark_object(struct pic_object *obj)
{
union header *p;
p = ((union header *)obj) - 1;
p->s.mark = PIC_GC_UNMARK;
}
static void
gc_mark(pic_state *pic, pic_value v)
{
if (! pic_obj_p(v))
return;
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)pic_obj_ptr(v));
}
2014-08-25 00:38:09 -04:00
static void
2015-07-20 02:02:16 -04:00
gc_mark_object(pic_state *pic, union object *obj)
2014-08-25 00:38:09 -04:00
{
union header *p;
2015-07-20 01:24:58 -04:00
loop:
2014-08-25 00:38:09 -04:00
p = ((union header *)obj) - 1;
2015-07-20 02:18:12 -04:00
if (p->s.mark == PIC_GC_MARK)
2014-08-25 00:38:09 -04:00
return;
p->s.mark = PIC_GC_MARK;
2015-07-20 02:02:16 -04:00
#define LOOP(o) obj = (union object *)(o); goto loop
2015-07-20 01:24:58 -04:00
2015-07-20 02:02:16 -04:00
switch (obj->obj.tt) {
2014-08-25 00:38:09 -04:00
case PIC_TT_PAIR: {
2015-07-20 02:02:16 -04:00
gc_mark(pic, obj->pair.car);
if (pic_obj_p(obj->pair.cdr)) {
LOOP(pic_obj_ptr(obj->pair.cdr));
2015-07-20 01:24:58 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
2015-05-30 09:34:51 -04:00
case PIC_TT_CXT: {
2014-08-25 00:38:09 -04:00
int i;
2015-07-20 02:02:16 -04:00
for (i = 0; i < obj->cxt.regc; ++i) {
gc_mark(pic, obj->cxt.regs[i]);
2014-08-25 00:38:09 -04:00
}
2015-07-20 02:02:16 -04:00
if (obj->cxt.up) {
LOOP(obj->cxt.up);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TT_PROC: {
2015-07-20 02:02:16 -04:00
if (pic_proc_irep_p(&obj->proc)) {
gc_mark_object(pic, (union object *)obj->proc.u.i.irep);
if (obj->proc.u.i.cxt) {
LOOP(obj->proc.u.i.cxt);
2015-05-31 07:19:07 -04:00
}
2015-01-18 22:14:46 -05:00
} else {
2015-07-20 02:02:16 -04:00
if (obj->proc.u.f.env) {
LOOP(obj->proc.u.f.env);
}
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TT_PORT: {
break;
}
case PIC_TT_ERROR: {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)obj->err.type);
gc_mark_object(pic, (union object *)obj->err.msg);
gc_mark(pic, obj->err.irrs);
LOOP(obj->err.stack);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_STRING: {
break;
}
case PIC_TT_VECTOR: {
size_t i;
2015-07-20 02:02:16 -04:00
for (i = 0; i < obj->vec.len; ++i) {
gc_mark(pic, obj->vec.data[i]);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TT_BLOB: {
break;
}
case PIC_TT_ID: {
2015-07-20 02:02:16 -04:00
gc_mark(pic, obj->id.var);
LOOP(obj->id.env);
break;
}
2015-05-30 09:39:03 -04:00
case PIC_TT_ENV: {
2015-07-20 02:02:16 -04:00
khash_t(env) *h = &obj->env.map;
2015-06-24 18:26:48 -04:00
khiter_t 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)) {
gc_mark_object(pic, kh_key(h, it));
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)kh_val(h, it));
2015-06-24 18:26:48 -04:00
}
}
2015-07-20 02:02:16 -04:00
if (obj->env.up) {
LOOP(obj->env.up);
2015-07-20 01:24:58 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_LIB: {
2015-07-20 02:02:16 -04:00
gc_mark(pic, obj->lib.name);
gc_mark_object(pic, (union object *)obj->lib.env);
LOOP(obj->lib.exports);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_IREP: {
size_t i;
2015-07-20 02:02:16 -04:00
for (i = 0; i < obj->irep.ilen; ++i) {
gc_mark_object(pic, (union object *)obj->irep.irep[i]);
2014-08-25 00:38:09 -04:00
}
2015-07-20 02:02:16 -04:00
for (i = 0; i < obj->irep.plen; ++i) {
gc_mark(pic, obj->irep.pool[i]);
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TT_DATA: {
2015-07-20 02:02:16 -04:00
if (obj->data.type->mark) {
obj->data.type->mark(pic, obj->data.data, gc_mark);
2014-09-16 03:18:19 -04:00
}
2015-07-20 02:02:16 -04:00
LOOP(obj->data.storage);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_DICT: {
2015-06-23 12:13:18 -04:00
pic_sym *sym;
2015-06-24 16:56:15 -04:00
khiter_t it;
2014-08-25 00:38:09 -04:00
2015-07-20 02:02:16 -04:00
pic_dict_for_each (sym, &obj->dict, it) {
gc_mark_object(pic, (union object *)sym);
gc_mark(pic, pic_dict_ref(pic, &obj->dict, sym));
2014-08-25 00:38:09 -04:00
}
break;
}
case PIC_TT_RECORD: {
struct pic_record *rec = (struct pic_record *)obj;
2015-07-20 01:24:58 -04:00
LOOP((struct pic_object *)rec->data);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_SYMBOL: {
break;
}
2015-06-09 05:31:46 -04:00
case PIC_TT_REG: {
struct pic_reg *reg = (struct pic_reg *)obj;
reg->prev = pic->regs;
pic->regs = reg;
break;
}
2015-06-22 04:06:13 -04:00
case PIC_TT_CP: {
2015-07-20 02:02:16 -04:00
if (obj->cp.prev) {
gc_mark_object(pic, (union object *)obj->cp.prev);
2015-06-22 04:06:13 -04:00
}
2015-07-20 02:02:16 -04:00
if (obj->cp.in) {
gc_mark_object(pic, (union object *)obj->cp.in);
2015-06-22 04:06:13 -04:00
}
2015-07-20 02:02:16 -04:00
if (obj->cp.out) {
LOOP((struct pic_object *)obj->cp.out);
2015-06-22 04:06:13 -04:00
}
break;
}
2014-08-25 00:38:09 -04:00
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
case PIC_TT_INT:
case PIC_TT_CHAR:
case PIC_TT_EOF:
case PIC_TT_UNDEF:
2015-06-09 03:02:23 -04:00
case PIC_TT_INVALID:
2014-09-16 12:02:27 -04:00
pic_panic(pic, "logic flaw");
2014-08-25 00:38:09 -04:00
}
}
2015-07-20 02:02:16 -04:00
#define M(x) gc_mark_object(pic, (union object *)pic->x)
#define P(x) gc_mark(pic, pic->x)
2014-08-25 00:38:09 -04:00
static void
gc_mark_phase(pic_state *pic)
{
pic_value *stack;
pic_callinfo *ci;
2014-09-18 01:50:01 -04:00
struct pic_proc **xhandler;
size_t j;
2015-06-09 05:31:46 -04:00
assert(pic->regs == NULL);
2014-08-25 00:38:09 -04:00
2015-06-08 08:04:04 -04:00
/* checkpoint */
if (pic->cp) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union 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) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)ci->cxt);
2014-08-25 00:38:09 -04:00
}
}
2014-09-18 01:50:01 -04:00
/* exception handlers */
for (xhandler = pic->xpbase; xhandler != pic->xp; ++xhandler) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)*xhandler);
2014-09-18 01:50:01 -04:00
}
2014-08-25 00:38:09 -04:00
/* arena */
for (j = 0; j < pic->arena_idx; ++j) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)pic->arena[j]);
2014-08-25 00:38:09 -04:00
}
2015-01-20 03:38:34 -05:00
/* mark reserved symbols */
2015-07-20 02:18:12 -04:00
M(sQUOTE); M(sQUASIQUOTE); M(sUNQUOTE); M(sUNQUOTE_SPLICING);
M(sSYNTAX_QUOTE); M(sSYNTAX_QUASIQUOTE); M(sSYNTAX_UNQUOTE); M(sSYNTAX_UNQUOTE_SPLICING);
M(sDEFINE_LIBRARY); M(sIMPORT); M(sEXPORT); M(sCOND_EXPAND);
M(sCALL); M(sGREF); M(sLREF); M(sCREF);
M(uDEFINE); M(uLAMBDA); M(uIF); M(uBEGIN); M(uQUOTE); M(uSETBANG); M(uDEFINE_MACRO);
M(uDEFINE_LIBRARY); M(uIMPORT); M(uEXPORT); M(uCOND_EXPAND);
M(uCONS); M(uCAR); M(uCDR); M(uNILP); M(uSYMBOLP); M(uPAIRP);
M(uADD); M(uSUB); M(uMUL); M(uDIV); M(uEQ); M(uLT); M(uLE); M(uGT); M(uGE); M(uNOT);
2015-01-18 22:14:46 -05:00
/* mark system procedures */
2015-07-20 02:18:12 -04:00
P(pCONS); P(pCAR); P(pCDR); P(pNILP); P(pSYMBOLP); P(pPAIRP); P(pNOT);
P(pADD); P(pSUB); P(pMUL); P(pDIV); P(pEQ); P(pLT); P(pLE); P(pGT); P(pGE);
2014-08-25 00:38:09 -04:00
/* global variables */
if (pic->globals) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)pic->globals);
2014-08-25 00:38:09 -04:00
}
/* macro objects */
if (pic->macros) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)pic->macros);
2014-08-25 00:38:09 -04:00
}
2015-06-09 05:31:46 -04:00
/* attribute table */
if (pic->attrs) {
2015-07-20 02:02:16 -04:00
gc_mark_object(pic, (union object *)pic->attrs);
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 */
gc_mark(pic, pic->libs);
2014-09-01 00:43:54 -04:00
2015-06-08 09:28:17 -04:00
/* parameter table */
gc_mark(pic, pic->ptable);
2015-06-09 05:31:46 -04:00
/* registries */
2014-09-24 23:12:52 -04:00
do {
2015-06-09 05:31:46 -04:00
struct pic_object *key;
pic_value val;
2015-06-24 18:14:29 -04:00
khiter_t it;
khash_t(reg) *h;
2015-06-09 05:31:46 -04:00
struct pic_reg *reg;
2014-09-24 23:12:52 -04:00
2015-06-09 05:31:46 -04:00
j = 0;
reg = pic->regs;
while (reg != NULL) {
2015-06-24 18:14:29 -04:00
h = &reg->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 (gc_obj_is_marked(key)) {
if (pic_obj_p(val) && ! gc_obj_is_marked(pic_obj_ptr(val))) {
gc_mark(pic, val);
++j;
}
2014-09-24 23:12:52 -04:00
}
}
2015-06-09 05:31:46 -04:00
reg = reg->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
2015-07-20 02:02:16 -04:00
gc_finalize_object(pic_state *pic, union object *obj)
2014-08-25 00:38:09 -04:00
{
2015-07-20 02:02:16 -04:00
switch (obj->obj.tt) {
2014-08-25 00:38:09 -04:00
case PIC_TT_VECTOR: {
2015-07-20 02:02:16 -04:00
pic_free(pic, obj->vec.data);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_BLOB: {
2015-07-20 02:02:16 -04:00
pic_free(pic, obj->blob.data);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_STRING: {
2015-07-20 02:02:16 -04:00
pic_rope_decref(pic, obj->str.rope);
break;
}
2015-05-30 09:39:03 -04:00
case PIC_TT_ENV: {
2015-07-20 02:02:16 -04:00
kh_destroy(env, &obj->env.map);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_IREP: {
2015-07-20 02:02:16 -04:00
pic_free(pic, obj->irep.code);
pic_free(pic, obj->irep.irep);
pic_free(pic, obj->irep.pool);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_DATA: {
2015-07-20 02:02:16 -04:00
if (obj->data.type->dtor) {
obj->data.type->dtor(pic, obj->data.data);
2015-06-22 14:02:12 -04:00
}
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_DICT: {
2015-07-20 02:02:16 -04:00
kh_destroy(dict, &obj->dict.hash);
2014-08-25 00:38:09 -04:00
break;
}
case PIC_TT_SYMBOL: {
2015-07-20 02:02:16 -04:00
pic_free(pic, (void *)obj->sym.cstr);
break;
}
2015-06-09 05:31:46 -04:00
case PIC_TT_REG: {
2015-07-20 02:02:16 -04:00
kh_destroy(reg, &obj->reg.hash);
2015-06-09 05:31:46 -04:00
break;
}
2015-07-20 02:02:16 -04:00
case PIC_TT_PAIR:
case PIC_TT_CXT:
case PIC_TT_PROC:
case PIC_TT_PORT:
case PIC_TT_ERROR:
case PIC_TT_ID:
case PIC_TT_LIB:
case PIC_TT_RECORD:
case PIC_TT_CP:
2015-06-22 04:06:13 -04:00
break;
2015-07-20 02:02:16 -04:00
2014-08-25 00:38:09 -04:00
case PIC_TT_NIL:
case PIC_TT_BOOL:
case PIC_TT_FLOAT:
case PIC_TT_INT:
case PIC_TT_CHAR:
case PIC_TT_EOF:
case PIC_TT_UNDEF:
2015-06-09 03:02:23 -04:00
case PIC_TT_INVALID:
2014-09-16 12:02:27 -04:00
pic_panic(pic, "logic flaw");
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;
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
}
2015-07-20 02:18:12 -04:00
if (p->s.mark == PIC_GC_MARK) {
p->s.mark = PIC_GC_UNMARK;
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;
2015-07-20 02:02:16 -04:00
gc_finalize_object(pic, (union 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;
2015-06-24 18:14:29 -04:00
khiter_t it;
khash_t(reg) *h;
2015-07-20 02:18:12 -04:00
khash_t(s) *s = &pic->syms;
pic_sym *sym;
2015-07-16 03:34:59 -04:00
size_t total = 0, inuse = 0;
2014-09-24 23:12:52 -04:00
2015-06-09 05:31:46 -04:00
/* registries */
while (pic->regs != NULL) {
2015-06-24 18:14:29 -04:00
h = &pic->regs->hash;
for (it = kh_begin(h); it != kh_end(h); ++it) {
if (! kh_exist(h, it))
continue;
if (! gc_obj_is_marked(kh_key(h, it))) {
kh_del(reg, h, it);
2014-09-24 23:12:52 -04:00
}
}
2015-06-09 05:31:46 -04:00
pic->regs = pic->regs->prev;
}
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 (! gc_obj_is_marked((struct pic_object *)sym)) {
kh_del(s, s, it);
}
}
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
}
void
pic_gc_run(pic_state *pic)
{
2015-01-18 22:14:29 -05:00
if (! pic->gc_enable) {
return;
}
2014-08-25 00:38:09 -04:00
gc_mark_phase(pic);
gc_sweep_phase(pic);
}
struct pic_object *
pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt)
{
struct pic_object *obj;
#if GC_STRESS
pic_gc_run(pic);
#endif
obj = (struct pic_object *)heap_alloc(pic, size);
2014-08-25 00:38:09 -04:00
if (obj == NULL) {
2015-01-18 22:14:46 -05:00
pic_gc_run(pic);
obj = (struct pic_object *)heap_alloc(pic, size);
2014-08-25 00:38:09 -04:00
if (obj == NULL) {
heap_morecore(pic);
obj = (struct pic_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
}
}
gc_unmark_object(obj);
2014-08-25 00:38:09 -04:00
obj->tt = tt;
return obj;
}
struct pic_object *
pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt)
{
struct pic_object *obj;
obj = pic_obj_alloc_unsafe(pic, size, tt);
gc_protect(pic, obj);
return obj;
}