From 3cb46b9b79ac71bf14005790fd1e4b4869b9b9a9 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 29 Jun 2014 17:22:02 +0900 Subject: [PATCH] allocate jmpbuf array in heap --- include/picrin.h | 1 + include/picrin/cont.h | 3 +++ include/picrin/error.h | 1 - src/cont.c | 11 +++++++++++ src/error.c | 21 ++++++++------------- src/gc.c | 1 + src/state.c | 6 +++++- 7 files changed, 29 insertions(+), 15 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index fd3b4ca2..ca043c09 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -109,6 +109,7 @@ typedef struct { jmp_buf *jmp; struct pic_error *err; struct pic_jmpbuf *try_jmps; + size_t try_jmp_size, try_jmp_idx; struct pic_heap *heap; struct pic_object **arena; diff --git a/include/picrin/cont.h b/include/picrin/cont.h index 496454fd..b33b6755 100644 --- a/include/picrin/cont.h +++ b/include/picrin/cont.h @@ -30,6 +30,9 @@ struct pic_cont { size_t arena_size; int arena_idx; + struct pic_jmpbuf *try_jmps; + size_t try_jmp_idx, try_jmp_size; + pic_value results; }; diff --git a/include/picrin/error.h b/include/picrin/error.h index 75361c1a..41444dc6 100644 --- a/include/picrin/error.h +++ b/include/picrin/error.h @@ -15,7 +15,6 @@ struct pic_jmpbuf { pic_value *sp; pic_code *ip; jmp_buf *prev_jmp; - struct pic_jmpbuf *prev; }; /* do not return from try block! */ diff --git a/src/cont.c b/src/cont.c index 11b5a3f6..df2d8a6b 100644 --- a/src/cont.c +++ b/src/cont.c @@ -10,6 +10,7 @@ #include "picrin/proc.h" #include "picrin/cont.h" #include "picrin/pair.h" +#include "picrin/error.h" pic_value pic_values0(pic_state *pic) @@ -143,6 +144,11 @@ save_cont(pic_state *pic, struct pic_cont **c) 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->try_jmp_idx = pic->try_jmp_idx; + cont->try_jmp_size = pic->try_jmp_size; + cont->try_jmps = pic_alloc(pic, sizeof(struct pic_jmpbuf) * pic->try_jmp_size); + memcpy(cont->try_jmps, pic->try_jmps, sizeof(struct pic_jmpbuf) * pic->try_jmp_size); + cont->results = pic_undef_value(); } @@ -189,6 +195,11 @@ restore_cont(pic_state *pic, struct pic_cont *cont) pic->arena_size = cont->arena_size; pic->arena_idx = cont->arena_idx; + pic->try_jmps = pic_realloc(pic, pic->try_jmps, sizeof(struct pic_jmpbuf) * cont->try_jmp_size); + memcpy(pic->try_jmps, cont->try_jmps, sizeof(struct pic_jmpbuf) * cont->try_jmp_size); + pic->try_jmp_size = cont->try_jmp_size; + pic->try_jmp_idx = cont->try_jmp_idx; + memcpy(cont->stk_pos, cont->stk_ptr, cont->stk_len); longjmp(tmp->jmp, 1); diff --git a/src/error.c b/src/error.c index 21f6d487..517677b4 100644 --- a/src/error.c +++ b/src/error.c @@ -38,7 +38,7 @@ pic_push_try(pic_state *pic) { struct pic_jmpbuf *try_jmp; - try_jmp = pic_alloc(pic, sizeof(struct pic_jmpbuf)); + try_jmp = pic->try_jmps + pic->try_jmp_idx++; try_jmp->ci = pic->ci; try_jmp->sp = pic->sp; @@ -46,27 +46,22 @@ pic_push_try(pic_state *pic) try_jmp->prev_jmp = pic->jmp; pic->jmp = &try_jmp->here; - - try_jmp->prev = pic->try_jmps; - pic->try_jmps = try_jmp; } void pic_pop_try(pic_state *pic) { - struct pic_jmpbuf *prev; + struct pic_jmpbuf *try_jmp; - assert(pic->jmp == &pic->try_jmps->here); + try_jmp = pic->try_jmps + --pic->try_jmp_idx; - pic->ci = pic->try_jmps->ci; - pic->sp = pic->try_jmps->sp; - pic->ip = pic->try_jmps->ip; + assert(pic->jmp == &try_jmp->here); - pic->jmp = pic->try_jmps->prev_jmp; + pic->ci = try_jmp->ci; + pic->sp = try_jmp->sp; + pic->ip = try_jmp->ip; - prev = pic->try_jmps->prev; - pic_free(pic, pic->try_jmps); - pic->try_jmps = prev; + pic->jmp = try_jmp->prev_jmp; } static struct pic_error * diff --git a/src/gc.c b/src/gc.c index 3d28aa96..1ea3986e 100644 --- a/src/gc.c +++ b/src/gc.c @@ -621,6 +621,7 @@ gc_finalize_object(pic_state *pic, struct pic_object *obj) pic_free(pic, cont->st_ptr); pic_free(pic, cont->ci_ptr); pic_free(pic, cont->arena); + pic_free(pic, cont->try_jmps); PIC_BLK_DECREF(pic, cont->blk); break; } diff --git a/src/state.c b/src/state.c index 758bae9c..cdae3901 100644 --- a/src/state.c +++ b/src/state.c @@ -9,6 +9,7 @@ #include "picrin/proc.h" #include "picrin/macro.h" #include "picrin/cont.h" +#include "picrin/error.h" void pic_init_core(pic_state *); @@ -70,7 +71,9 @@ pic_open(int argc, char *argv[], char **envp) /* error handling */ pic->jmp = NULL; pic->err = NULL; - pic->try_jmps = NULL; + pic->try_jmps = calloc(PIC_RESCUE_SIZE, sizeof(struct pic_jmpbuf)); + pic->try_jmp_idx = 0; + pic->try_jmp_size = PIC_RESCUE_SIZE; /* GC arena */ pic->arena = (struct pic_object **)calloc(PIC_ARENA_SIZE, sizeof(struct pic_object **)); @@ -170,6 +173,7 @@ pic_close(pic_state *pic) /* free global stacks */ free(pic->globals); + free(pic->try_jmps); xh_destroy(&pic->syms); xh_destroy(&pic->global_tbl); xh_destroy(&pic->macros);