stateful allocator
This commit is contained in:
parent
50dce97bac
commit
c573f2c56e
|
@ -81,7 +81,7 @@ pic_heap_close(pic_state *pic, struct pic_heap *heap)
|
|||
|
||||
#if PIC_ENABLE_LIBC
|
||||
void *
|
||||
pic_default_allocf(void *ptr, size_t size)
|
||||
pic_default_allocf(void PIC_UNUSED(*userdata), void *ptr, size_t size)
|
||||
{
|
||||
if (size == 0) {
|
||||
if (ptr) {
|
||||
|
@ -102,7 +102,7 @@ pic_malloc(pic_state *pic, size_t size)
|
|||
{
|
||||
void *ptr;
|
||||
|
||||
ptr = pic->allocf(NULL, size);
|
||||
ptr = pic->allocf(pic->userdata, NULL, size);
|
||||
if (ptr == NULL && size > 0) {
|
||||
pic_panic(pic, "memory exhausted");
|
||||
}
|
||||
|
@ -112,7 +112,7 @@ pic_malloc(pic_state *pic, size_t size)
|
|||
void *
|
||||
pic_realloc(pic_state *pic, void *ptr, size_t size)
|
||||
{
|
||||
ptr = pic->allocf(ptr, size);
|
||||
ptr = pic->allocf(pic->userdata, ptr, size);
|
||||
if (ptr == NULL && size > 0) {
|
||||
pic_panic(pic, "memory exhausted");
|
||||
}
|
||||
|
@ -125,7 +125,7 @@ pic_calloc(pic_state *pic, size_t count, size_t size)
|
|||
void *ptr;
|
||||
|
||||
size *= count;
|
||||
ptr = pic->allocf(NULL, size);
|
||||
ptr = pic->allocf(pic->userdata, NULL, size);
|
||||
if (ptr == NULL && size > 0) {
|
||||
pic_panic(pic, "memory exhausted");
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ pic_calloc(pic_state *pic, size_t count, size_t size)
|
|||
void
|
||||
pic_free(pic_state *pic, void *ptr)
|
||||
{
|
||||
pic->allocf(ptr, 0);
|
||||
pic->allocf(pic->userdata, ptr, 0);
|
||||
}
|
||||
|
||||
static void
|
||||
|
|
|
@ -67,7 +67,7 @@ typedef struct {
|
|||
struct pic_context *up;
|
||||
} pic_callinfo;
|
||||
|
||||
typedef void *(*pic_allocf)(void *, size_t);
|
||||
typedef void *(*pic_allocf)(void *, void *, size_t);
|
||||
|
||||
struct pic_state {
|
||||
int argc;
|
||||
|
@ -158,7 +158,7 @@ void pic_gc_arena_restore(pic_state *, size_t);
|
|||
pic_gc_arena_restore(pic, ai); \
|
||||
} while (0)
|
||||
|
||||
void *pic_default_allocf(void *, size_t);
|
||||
void *pic_default_allocf(void *, void *, size_t);
|
||||
pic_state *pic_open(pic_allocf, void *);
|
||||
void pic_close(pic_state *);
|
||||
void pic_set_argv(pic_state *, int argc, char *argv[], char **envp);
|
||||
|
|
|
@ -237,7 +237,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
pic_state *pic;
|
||||
size_t ai;
|
||||
|
||||
pic = allocf(NULL, sizeof(pic_state));
|
||||
pic = allocf(userdata, NULL, sizeof(pic_state));
|
||||
|
||||
if (! pic) {
|
||||
goto EXIT_PIC;
|
||||
|
@ -265,7 +265,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
pic->envp = NULL;
|
||||
|
||||
/* prepare VM stack */
|
||||
pic->stbase = pic->sp = allocf(NULL, PIC_STACK_SIZE * sizeof(pic_value));
|
||||
pic->stbase = pic->sp = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(pic_value));
|
||||
pic->stend = pic->stbase + PIC_STACK_SIZE;
|
||||
|
||||
if (! pic->sp) {
|
||||
|
@ -273,7 +273,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
}
|
||||
|
||||
/* callinfo */
|
||||
pic->cibase = pic->ci = allocf(NULL, PIC_STACK_SIZE * sizeof(pic_callinfo));
|
||||
pic->cibase = pic->ci = allocf(userdata, NULL, PIC_STACK_SIZE * sizeof(pic_callinfo));
|
||||
pic->ciend = pic->cibase + PIC_STACK_SIZE;
|
||||
|
||||
if (! pic->ci) {
|
||||
|
@ -281,7 +281,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
}
|
||||
|
||||
/* exception handler */
|
||||
pic->xpbase = pic->xp = allocf(NULL, PIC_RESCUE_SIZE * sizeof(struct pic_proc *));
|
||||
pic->xpbase = pic->xp = allocf(userdata, NULL, PIC_RESCUE_SIZE * sizeof(struct pic_proc *));
|
||||
pic->xpend = pic->xpbase + PIC_RESCUE_SIZE;
|
||||
|
||||
if (! pic->xp) {
|
||||
|
@ -289,7 +289,7 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
}
|
||||
|
||||
/* GC arena */
|
||||
pic->arena = allocf(NULL, PIC_ARENA_SIZE * sizeof(struct pic_object *));
|
||||
pic->arena = allocf(userdata, NULL, PIC_ARENA_SIZE * sizeof(struct pic_object *));
|
||||
pic->arena_size = PIC_ARENA_SIZE;
|
||||
pic->arena_idx = 0;
|
||||
|
||||
|
@ -458,13 +458,13 @@ pic_open(pic_allocf allocf, void *userdata)
|
|||
return pic;
|
||||
|
||||
EXIT_ARENA:
|
||||
allocf(pic->xp, 0);
|
||||
allocf(userdata, pic->xp, 0);
|
||||
EXIT_XP:
|
||||
allocf(pic->ci, 0);
|
||||
allocf(userdata, pic->ci, 0);
|
||||
EXIT_CI:
|
||||
allocf(pic->sp, 0);
|
||||
allocf(userdata, pic->sp, 0);
|
||||
EXIT_SP:
|
||||
allocf(pic, 0);
|
||||
allocf(userdata, pic, 0);
|
||||
EXIT_PIC:
|
||||
return NULL;
|
||||
}
|
||||
|
@ -500,15 +500,15 @@ pic_close(pic_state *pic)
|
|||
pic_reader_destroy(pic);
|
||||
|
||||
/* free runtime context */
|
||||
allocf(pic->stbase, 0);
|
||||
allocf(pic->cibase, 0);
|
||||
allocf(pic->xpbase, 0);
|
||||
allocf(pic->userdata, pic->stbase, 0);
|
||||
allocf(pic->userdata, pic->cibase, 0);
|
||||
allocf(pic->userdata, pic->xpbase, 0);
|
||||
|
||||
/* free global stacks */
|
||||
kh_destroy(s, h);
|
||||
|
||||
/* free GC arena */
|
||||
allocf(pic->arena, 0);
|
||||
allocf(pic->userdata, pic->arena, 0);
|
||||
|
||||
allocf(pic, 0);
|
||||
allocf(pic->userdata, pic, 0);
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue