From a6f561d66ed2e791df1084a982a50494c98e02b5 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 13 Oct 2013 17:25:36 +0900 Subject: [PATCH] change the type of argument pic_gc_protect accepts --- include/picrin.h | 4 +++ include/picrin/value.h | 1 + src/gc.c | 57 ++++++++++++++++++++++++++---------------- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/include/picrin.h b/include/picrin.h index 76241b67..626a52a4 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -29,6 +29,10 @@ void *pic_alloc(pic_state *, size_t); struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); void pic_free(pic_state *, void *); +void pic_gc_protect(pic_state *, pic_value); +int pic_gc_arena_preserve(pic_state *); +void pic_gc_arena_restore(pic_state *, int); + pic_state *pic_open(); void pic_close(pic_state *); diff --git a/include/picrin/value.h b/include/picrin/value.h index b2acba8b..6334b5b7 100644 --- a/include/picrin/value.h +++ b/include/picrin/value.h @@ -52,6 +52,7 @@ struct pic_proc { } u; }; +#define pic_object_ptr(o) ((struct pic_object *)o.u.data) #define pic_pair_ptr(o) ((struct pic_pair *)o.u.data) #define pic_symbol_ptr(o) ((struct pic_symbol *)o.u.data) #define pic_proc_ptr(o) ((struct pic_proc *)o.u.data) diff --git a/src/gc.c b/src/gc.c index c51a2e8c..482b95f8 100644 --- a/src/gc.c +++ b/src/gc.c @@ -39,6 +39,40 @@ pic_free(pic_state *pic, void *ptr) free(ptr); } +static void +gc_protect(pic_state *pic, struct pic_object *obj) +{ + if (pic->arena_idx >= PIC_ARENA_SIZE) { + pic_raise(pic, "arena overflow"); + } + pic->arena[pic->arena_idx++] = obj; +} + +void +pic_gc_protect(pic_state *pic, pic_value v) +{ + struct pic_object *obj; + + if (v.type != PIC_VTYPE_HEAP) { + return; + } + obj = pic_object_ptr(v); + + gc_protect(pic, obj); +} + +int +pic_gc_arena_preserve(pic_state *pic) +{ + return pic->arena_idx; +} + +void +pic_gc_arena_restore(pic_state *pic, int state) +{ + pic->arena_idx = state; +} + static void * pic_gc_alloc(pic_state *pic, size_t size) { @@ -104,27 +138,6 @@ pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt) obj = (struct pic_object *)malloc(size); obj->tt = tt; - pic_gc_protect(pic, obj); + gc_protect(pic, obj); return obj; } - -void -pic_gc_protect(pic_state *pic, struct pic_object *obj) -{ - if (pic->arena_idx >= PIC_ARENA_SIZE) { - pic_raise(pic, "arena overflow"); - } - pic->arena[pic->arena_idx++] = obj; -} - -int -pic_gc_arena_preserve(pic_state *pic) -{ - return pic->arena_idx; -} - -void -pic_gc_arena_restore(pic_state *pic, int state) -{ - pic->arena_idx = state; -}