change the type of argument pic_gc_protect accepts

This commit is contained in:
Yuichi Nishiwaki 2013-10-13 17:25:36 +09:00
parent ee985a31cd
commit a6f561d66e
3 changed files with 40 additions and 22 deletions

View File

@ -29,6 +29,10 @@ void *pic_alloc(pic_state *, size_t);
struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt); struct pic_object *pic_obj_alloc(pic_state *, size_t, enum pic_tt);
void pic_free(pic_state *, void *); 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(); pic_state *pic_open();
void pic_close(pic_state *); void pic_close(pic_state *);

View File

@ -52,6 +52,7 @@ struct pic_proc {
} u; } 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_pair_ptr(o) ((struct pic_pair *)o.u.data)
#define pic_symbol_ptr(o) ((struct pic_symbol *)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) #define pic_proc_ptr(o) ((struct pic_proc *)o.u.data)

View File

@ -39,6 +39,40 @@ pic_free(pic_state *pic, void *ptr)
free(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 * static void *
pic_gc_alloc(pic_state *pic, size_t size) 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 = (struct pic_object *)malloc(size);
obj->tt = tt; obj->tt = tt;
pic_gc_protect(pic, obj); 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;
}