From ee985a31cdf8579bd178e0aaa67d308cb4501363 Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Sun, 13 Oct 2013 17:02:29 +0900 Subject: [PATCH] add GC arena --- include/picrin.h | 3 +++ src/gc.c | 22 ++++++++++++++++++++++ src/state.c | 4 ++++ 3 files changed, 29 insertions(+) diff --git a/include/picrin.h b/include/picrin.h index 6eafba52..76241b67 100644 --- a/include/picrin.h +++ b/include/picrin.h @@ -11,6 +11,7 @@ struct pic_env { struct pic_env *parent; }; +#define PIC_ARENA_SIZE 1024 #define PIC_HEAP_SIZE 1024 typedef struct { @@ -20,6 +21,8 @@ typedef struct { struct pic_env *global_env; struct heap_page *heap; + struct pic_object *arena[PIC_ARENA_SIZE]; + int arena_idx; } pic_state; void *pic_alloc(pic_state *, size_t); diff --git a/src/gc.c b/src/gc.c index 1c5f4ded..c51a2e8c 100644 --- a/src/gc.c +++ b/src/gc.c @@ -104,5 +104,27 @@ 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); 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; +} diff --git a/src/state.c b/src/state.c index 812c0299..17ad6368 100644 --- a/src/state.c +++ b/src/state.c @@ -30,6 +30,10 @@ pic_open() pic->heap = (struct heap_page *)malloc(sizeof(struct heap_page)); init_heap_page(pic->heap); + /* GC arena */ + pic->arena = (struct pic_object *)calloc(PIC_ARENA_SIZE, sizeof(struct pic_object *)); + pic->arena_idx = 0; + pic->global_env = pic_new_empty_env(); return pic;