add GC arena

This commit is contained in:
Yuichi Nishiwaki 2013-10-13 17:02:29 +09:00
parent 012b387c89
commit ee985a31cd
3 changed files with 29 additions and 0 deletions

View File

@ -11,6 +11,7 @@ struct pic_env {
struct pic_env *parent; struct pic_env *parent;
}; };
#define PIC_ARENA_SIZE 1024
#define PIC_HEAP_SIZE 1024 #define PIC_HEAP_SIZE 1024
typedef struct { typedef struct {
@ -20,6 +21,8 @@ typedef struct {
struct pic_env *global_env; struct pic_env *global_env;
struct heap_page *heap; struct heap_page *heap;
struct pic_object *arena[PIC_ARENA_SIZE];
int arena_idx;
} pic_state; } pic_state;
void *pic_alloc(pic_state *, size_t); void *pic_alloc(pic_state *, size_t);

View File

@ -104,5 +104,27 @@ pic_obj_alloc(pic_state *pic, size_t size, enum pic_tt tt)
obj = (struct pic_object *)malloc(size); obj = (struct pic_object *)malloc(size);
obj->tt = tt; obj->tt = tt;
pic_gc_protect(pic, obj);
return 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;
}

View File

@ -30,6 +30,10 @@ pic_open()
pic->heap = (struct heap_page *)malloc(sizeof(struct heap_page)); pic->heap = (struct heap_page *)malloc(sizeof(struct heap_page));
init_heap_page(pic->heap); 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(); pic->global_env = pic_new_empty_env();
return pic; return pic;