diff --git a/include/picrin.h b/include/picrin.h index fa84f59b..d194de1f 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -105,8 +105,8 @@ typedef struct { struct pic_jmpbuf *try_jmps; struct pic_heap *heap; - struct pic_object *arena[PIC_ARENA_SIZE]; - int arena_idx; + struct pic_object **arena; + size_t arena_size, arena_idx; char *native_stack_start; } pic_state; @@ -123,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/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/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/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/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 fd48833e..2821be35 100644 --- a/src/gc.c +++ b/src/gc.c @@ -179,8 +179,9 @@ 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) { - pic_abort(pic, "arena overflow"); + 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; } @@ -200,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; } @@ -618,6 +619,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/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 37fab568..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)); @@ -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)) { 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];