From a003b42435df7809ac6543edb38097adbdc5c45c Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 26 May 2014 15:51:18 +0900 Subject: [PATCH 1/3] 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)) { From 826eb4415d3b2f4089750f5650bb6c206d7b1577 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 26 May 2014 15:56:51 +0900 Subject: [PATCH 2/3] automatic arena extend --- src/gc.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/gc.c b/src/gc.c index d5159055..3b408e08 100644 --- a/src/gc.c +++ b/src/gc.c @@ -180,7 +180,8 @@ static void gc_protect(pic_state *pic, struct pic_object *obj) { if (pic->arena_idx >= pic->arena_size) { - pic_abort(pic, "arena overflow"); + 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; } From 2155a0c8f19b446fa6f10758270258e6a048fe8e Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 26 May 2014 16:06:41 +0900 Subject: [PATCH 3/3] declare arena_idx as a size_t --- include/picrin.h | 7 +++---- src/codegen.c | 4 ++-- src/debug.c | 4 ++-- src/gc.c | 4 ++-- src/init.c | 2 +- src/load.c | 4 ++-- src/macro.c | 6 +++--- src/number.c | 2 +- src/pair.c | 16 ++++++++-------- src/read.c | 2 +- src/state.c | 2 +- src/system.c | 4 ++-- src/vm.c | 2 +- 13 files changed, 29 insertions(+), 30 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 334da138..d194de1f 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -106,8 +106,7 @@ typedef struct { struct pic_heap *heap; struct pic_object **arena; - size_t arena_size; - int arena_idx; + size_t arena_size, arena_idx; char *native_stack_start; } pic_state; @@ -124,8 +123,8 @@ void pic_free(pic_state *, void *); void pic_gc_run(pic_state *); pic_value pic_gc_protect(pic_state *, pic_value); -int pic_gc_arena_preserve(pic_state *); -void pic_gc_arena_restore(pic_state *, int); +size_t pic_gc_arena_preserve(pic_state *); +void pic_gc_arena_restore(pic_state *, size_t); pic_state *pic_open(int argc, char *argv[], char **envp); void pic_close(pic_state *); diff --git a/src/codegen.c b/src/codegen.c index ef9bd216..2d052739 100644 --- a/src/codegen.c +++ b/src/codegen.c @@ -266,7 +266,7 @@ static pic_value analyze(analyze_state *state, pic_value obj, bool tailpos) { pic_state *pic = state->pic; - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value res; pic_sym tag; @@ -1438,7 +1438,7 @@ struct pic_proc * pic_compile(pic_state *pic, pic_value obj) { struct pic_irep *irep; - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); #if DEBUG fprintf(stdout, "ai = %d\n", pic_gc_arena_preserve(pic)); diff --git a/src/debug.c b/src/debug.c index 7ce89fc0..f59a4125 100644 --- a/src/debug.c +++ b/src/debug.c @@ -10,7 +10,7 @@ pic_str * pic_get_backtrace(pic_state *pic) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_callinfo *ci; pic_str *trace; @@ -38,7 +38,7 @@ pic_get_backtrace(pic_state *pic) void pic_print_backtrace(pic_state *pic, struct pic_error *e) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_str *trace; assert(pic->err != NULL); diff --git a/src/gc.c b/src/gc.c index 3b408e08..2821be35 100644 --- a/src/gc.c +++ b/src/gc.c @@ -201,14 +201,14 @@ pic_gc_protect(pic_state *pic, pic_value v) return v; } -int +size_t pic_gc_arena_preserve(pic_state *pic) { return pic->arena_idx; } void -pic_gc_arena_restore(pic_state *pic, int state) +pic_gc_arena_restore(pic_state *pic, size_t state) { pic->arena_idx = state; } diff --git a/src/init.c b/src/init.c index 8bbc3229..26050194 100644 --- a/src/init.c +++ b/src/init.c @@ -60,7 +60,7 @@ pic_features(pic_state *pic) void pic_init_core(pic_state *pic) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_deflibrary ("(scheme base)") { diff --git a/src/load.c b/src/load.c index ec95b4dc..b1fcf39a 100644 --- a/src/load.c +++ b/src/load.c @@ -8,7 +8,7 @@ pic_value pic_load_cstr(pic_state *pic, const char *src) { - int ai; + size_t ai; pic_value v, exprs; struct pic_proc *proc; @@ -37,7 +37,7 @@ pic_value pic_load(pic_state *pic, const char *fn) { FILE *file; - int ai; + size_t ai; pic_value v, exprs; struct pic_proc *proc; diff --git a/src/macro.c b/src/macro.c index b4a33a5a..2790efb0 100644 --- a/src/macro.c +++ b/src/macro.c @@ -150,7 +150,7 @@ static pic_value macroexpand_node(pic_state *, pic_value, struct pic_senv *, pic static pic_value macroexpand(pic_state *pic, pic_value expr, struct pic_senv *senv, pic_value assoc_box) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value v; v = macroexpand_node(pic, expr, senv, assoc_box); @@ -196,7 +196,7 @@ push_scope(pic_state *pic, pic_value formals, struct pic_senv *up, pic_value ass static pic_value macroexpand_list(pic_state *pic, pic_value list, struct pic_senv *senv, pic_value assoc_box) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value v, vs; /* macroexpand in order */ @@ -271,7 +271,7 @@ macroexpand_deflibrary(pic_state *pic, pic_value expr) pic_in_library(pic, pic_cadr(pic, expr)); pic_for_each (v, pic_cddr(pic, expr)) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_eval(pic, v); diff --git a/src/number.c b/src/number.c index a73eb785..fb9a4c1f 100644 --- a/src/number.c +++ b/src/number.c @@ -734,7 +734,7 @@ pic_number_string_to_number(pic_state *pic) void pic_init_number(pic_state *pic) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_defun(pic, "number?", pic_number_real_p); pic_defun(pic, "complex?", pic_number_real_p); diff --git a/src/pair.c b/src/pair.c index 37d5ff7f..6fe316c1 100644 --- a/src/pair.c +++ b/src/pair.c @@ -84,7 +84,7 @@ pic_list1(pic_state *pic, pic_value obj1) pic_value pic_list2(pic_state *pic, pic_value obj1, pic_value obj2) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list1(pic, obj2)); @@ -97,7 +97,7 @@ pic_list2(pic_state *pic, pic_value obj1, pic_value obj2) pic_value pic_list3(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list2(pic, obj2, obj3)); @@ -110,7 +110,7 @@ pic_list3(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3) pic_value pic_list4(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list3(pic, obj2, obj3, obj4)); @@ -123,7 +123,7 @@ pic_list4(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_va pic_value pic_list5(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list4(pic, obj2, obj3, obj4, obj5)); @@ -136,7 +136,7 @@ pic_list5(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_va pic_value pic_list6(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5, pic_value obj6) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list5(pic, obj2, obj3, obj4, obj5, obj6)); @@ -149,7 +149,7 @@ pic_list6(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_va pic_value pic_list7(pic_state *pic, pic_value obj1, pic_value obj2, pic_value obj3, pic_value obj4, pic_value obj5, pic_value obj6, pic_value obj7) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = pic_cons(pic, obj1, pic_list6(pic, obj2, obj3, obj4, obj5, obj6, obj7)); @@ -205,7 +205,7 @@ pic_length(pic_state *pic, pic_value obj) pic_value pic_reverse(pic_state *pic, pic_value list) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value v, acc; acc = pic_nil_value(); @@ -221,7 +221,7 @@ pic_reverse(pic_state *pic, pic_value list) pic_value pic_append(pic_state *pic, pic_value xs, pic_value ys) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value x; xs = pic_reverse(pic, xs); diff --git a/src/read.c b/src/read.c index e768c10e..030ee0e3 100644 --- a/src/read.c +++ b/src/read.c @@ -217,7 +217,7 @@ read_datum(int tok, yyscan_t scanner) static pic_value read(int tok, yyscan_t scanner) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_value val; val = read_datum(tok, scanner); diff --git a/src/state.c b/src/state.c index 81c18004..a9a13ba8 100644 --- a/src/state.c +++ b/src/state.c @@ -18,7 +18,7 @@ pic_open(int argc, char *argv[], char **envp) char t; pic_state *pic; - int ai; + size_t ai; pic = (pic_state *)malloc(sizeof(pic_state)); diff --git a/src/system.c b/src/system.c index b5108af1..efd53f48 100644 --- a/src/system.c +++ b/src/system.c @@ -18,7 +18,7 @@ pic_system_cmdline(pic_state *pic) pic_get_args(pic, ""); for (i = 0; i < pic->argc; ++i) { - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); v = pic_cons(pic, pic_obj_value(pic_str_new_cstr(pic, pic->argv[i])), v); pic_gc_arena_restore(pic, ai); @@ -95,7 +95,7 @@ pic_system_getenvs(pic_state *pic) { char **envp; pic_value data = pic_nil_value(); - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); pic_get_args(pic, ""); diff --git a/src/vm.c b/src/vm.c index 760a63eb..6c89d8a8 100644 --- a/src/vm.c +++ b/src/vm.c @@ -514,7 +514,7 @@ pic_value pic_apply(pic_state *pic, struct pic_proc *proc, pic_value argv) { pic_code c; - int ai = pic_gc_arena_preserve(pic); + size_t ai = pic_gc_arena_preserve(pic); size_t argc, i; pic_code boot[2];