From a003b42435df7809ac6543edb38097adbdc5c45c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 26 May 2014 15:51:18 +0900 Subject: [PATCH] dynamically allocate GC arena --- include/picrin.h | 3 ++- include/picrin/cont.h | 3 ++- src/cont.c | 8 ++++++-- src/gc.c | 3 ++- src/state.c | 5 +++++ 5 files changed, 17 insertions(+), 5 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index fa84f59b..334da138 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -105,7 +105,8 @@ typedef struct { struct pic_jmpbuf *try_jmps; struct pic_heap *heap; - struct pic_object *arena[PIC_ARENA_SIZE]; + struct pic_object **arena; + size_t arena_size; int arena_idx; char *native_stack_start; diff --git a/include/picrin/cont.h b/include/picrin/cont.h index a4b8f32a..496454fd 100644 --- a/include/picrin/cont.h +++ b/include/picrin/cont.h @@ -26,7 +26,8 @@ struct pic_cont { pic_code *ip; - struct pic_object *arena[PIC_ARENA_SIZE]; + struct pic_object **arena; + size_t arena_size; int arena_idx; pic_value results; diff --git a/src/cont.c b/src/cont.c index e22bcfef..f84e55c7 100644 --- a/src/cont.c +++ b/src/cont.c @@ -139,7 +139,9 @@ save_cont(pic_state *pic, struct pic_cont **c) cont->ip = pic->ip; cont->arena_idx = pic->arena_idx; - memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * PIC_ARENA_SIZE); + cont->arena_size = pic->arena_size; + cont->arena = (struct pic_object **)pic_alloc(pic, sizeof(struct pic_object *) * pic->arena_size); + memcpy(cont->arena, pic->arena, sizeof(struct pic_object *) * pic->arena_size); cont->results = pic_undef_value(); } @@ -182,7 +184,9 @@ restore_cont(pic_state *pic, struct pic_cont *cont) pic->ip = cont->ip; - memcpy(pic->arena, cont->arena, sizeof(struct pic_object *) * PIC_ARENA_SIZE); + pic->arena = (struct pic_object **)pic_realloc(pic, pic->arena, sizeof(struct pic_object *) * cont->arena_size); + memcpy(pic->arena, cont->arena, sizeof(struct pic_object *) * cont->arena_size); + pic->arena_size = cont->arena_size; pic->arena_idx = cont->arena_idx; memcpy(cont->stk_pos, cont->stk_ptr, cont->stk_len); diff --git a/src/gc.c b/src/gc.c index fd48833e..d5159055 100644 --- a/src/gc.c +++ b/src/gc.c @@ -179,7 +179,7 @@ pic_free(pic_state *pic, void *ptr) static void gc_protect(pic_state *pic, struct pic_object *obj) { - if (pic->arena_idx >= PIC_ARENA_SIZE) { + if (pic->arena_idx >= pic->arena_size) { pic_abort(pic, "arena overflow"); } pic->arena[pic->arena_idx++] = obj; @@ -618,6 +618,7 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) pic_free(pic, cont->stk_ptr); pic_free(pic, cont->st_ptr); pic_free(pic, cont->ci_ptr); + pic_free(pic, cont->arena); PIC_BLK_DECREF(pic, cont->blk); break; } diff --git a/src/state.c b/src/state.c index 37fab568..81c18004 100644 --- a/src/state.c +++ b/src/state.c @@ -70,6 +70,8 @@ pic_open(int argc, char *argv[], char **envp) pic->try_jmps = NULL; /* GC arena */ + pic->arena = (struct pic_object **)calloc(PIC_ARENA_SIZE, sizeof(struct pic_object **)); + pic->arena_size = PIC_ARENA_SIZE; pic->arena_idx = 0; /* native stack marker */ @@ -153,6 +155,9 @@ pic_close(pic_state *pic) xh_destroy(&pic->global_tbl); xh_destroy(&pic->macros); + /* free GC arena */ + free(pic->arena); + /* free symbol names */ xh_begin(&it, &pic->sym_names); while (xh_next(&it)) {