turn on GC

This commit is contained in:
Yuichi Nishiwaki 2015-01-19 12:14:46 +09:00
parent ccc7a2beed
commit ca69048513
2 changed files with 44 additions and 7 deletions

View File

@ -390,6 +390,8 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
}
if (pic_proc_irep_p(proc)) {
gc_mark_object(pic, (struct pic_object *)proc->u.irep);
} else {
gc_mark_object(pic, (struct pic_object *)proc->u.func.name);
}
break;
}
@ -398,6 +400,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
}
case PIC_TT_ERROR: {
struct pic_error *err = (struct pic_error *)obj;
gc_mark_object(pic, (struct pic_object *)err->type);
gc_mark_object(pic, (struct pic_object *)err->msg);
gc_mark(pic, err->irrs);
gc_mark_object(pic, (struct pic_object *)err->stack);
@ -443,6 +446,9 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
for (i = 0; i < irep->plen; ++i) {
gc_mark(pic, irep->pool[i]);
}
for (i = 0; i < irep->slen; ++i) {
gc_mark_object(pic, (struct pic_object *)irep->syms[i]);
}
break;
}
case PIC_TT_DATA: {
@ -462,6 +468,7 @@ gc_mark_object(pic_state *pic, struct pic_object *obj)
xh_entry *it;
for (it = xh_begin(&dict->hash); it != NULL; it = xh_next(it)) {
gc_mark_object(pic, (struct pic_object *)xh_key(it, pic_sym));
gc_mark(pic, xh_val(it, pic_value));
}
break;
@ -516,6 +523,28 @@ gc_mark_trie(pic_state *pic, struct pic_trie *trie)
}
}
#define M(x) gc_mark_object(pic, (struct pic_object *)pic->x)
static void
gc_mark_global_symbols(pic_state *pic)
{
M(sDEFINE); M(sLAMBDA); M(sIF); M(sBEGIN); M(sQUOTE); M(sSETBANG);
M(sQUASIQUOTE); M(sUNQUOTE); M(sUNQUOTE_SPLICING);
M(sDEFINE_SYNTAX); M(sIMPORT); M(sEXPORT);
M(sDEFINE_LIBRARY); M(sIN_LIBRARY);
M(sCOND_EXPAND); M(sAND); M(sOR); M(sELSE); M(sLIBRARY);
M(sONLY); M(sRENAME); M(sPREFIX); M(sEXCEPT);
M(sCONS); M(sCAR); M(sCDR); M(sNILP);
M(sSYMBOL_P); M(sPAIR_P);
M(sADD); M(sSUB); M(sMUL); M(sDIV); M(sMINUS);
M(sEQ); M(sLT); M(sLE); M(sGT); M(sGE); M(sNOT);
M(sREAD); M(sFILE);
M(rDEFINE); M(rLAMBDA); M(rIF); M(rBEGIN); M(rQUOTE); M(rSETBANG);
M(rDEFINE_SYNTAX); M(rIMPORT); M(rEXPORT);
M(rDEFINE_LIBRARY); M(rIN_LIBRARY);
M(rCOND_EXPAND);
}
static void
gc_mark_phase(pic_state *pic)
{
@ -553,6 +582,13 @@ gc_mark_phase(pic_state *pic)
gc_mark_object(pic, pic->arena[j]);
}
gc_mark_global_symbols(pic);
/* symbol table */
for (it = xh_begin(&pic->syms); it != NULL; it = xh_next(it)) {
gc_mark_object(pic, (struct pic_object *)xh_val(it, pic_sym));
}
/* global variables */
if (pic->globals) {
gc_mark_object(pic, (struct pic_object *)pic->globals);
@ -814,7 +850,7 @@ pic_obj_alloc_unsafe(pic_state *pic, size_t size, enum pic_tt tt)
obj = (struct pic_object *)gc_alloc(pic, size);
if (obj == NULL) {
/* pic_gc_run(pic); */
pic_gc_run(pic);
obj = (struct pic_object *)gc_alloc(pic, size);
if (obj == NULL) {
add_heap_page(pic);

View File

@ -202,6 +202,11 @@ pic_close(pic_state *pic)
pic->wind = pic->wind->prev;
}
/* free symbol names */
for (it = xh_begin(&pic->syms); it != NULL; it = xh_next(it)) {
free(xh_key(it, char *));
}
/* clear out root objects */
pic->sp = pic->stbase;
pic->ci = pic->cibase;
@ -210,6 +215,7 @@ pic_close(pic_state *pic)
pic->err = pic_undef_value();
pic->globals = NULL;
pic->macros = NULL;
xh_clear(&pic->syms);
xh_clear(&pic->attrs);
pic->features = pic_nil_value();
pic->libs = pic_nil_value();
@ -230,11 +236,6 @@ pic_close(pic_state *pic)
pic_trie_delete(pic, pic->reader->trie);
free(pic->reader);
/* free symbol names */
for (it = xh_begin(&pic->syms); it != NULL; it = xh_next(it)) {
free(xh_key(it, char *));
}
/* free global stacks */
xh_destroy(&pic->syms);
xh_destroy(&pic->attrs);