From c573f2c56e616e0f3ed74ca8e9212442217c545d Mon Sep 17 00:00:00 2001 From: Yuichi Nishiwaki Date: Mon, 10 Aug 2015 21:13:23 +0900 Subject: [PATCH] stateful allocator --- extlib/benz/gc.c | 10 +++++----- extlib/benz/include/picrin.h | 4 ++-- extlib/benz/state.c | 28 ++++++++++++++-------------- 3 files changed, 21 insertions(+), 21 deletions(-) diff --git a/extlib/benz/gc.c b/extlib/benz/gc.c index 27d3d0dc..913cf817 100644 --- a/extlib/benz/gc.c +++ b/extlib/benz/gc.c @@ -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 diff --git a/extlib/benz/include/picrin.h b/extlib/benz/include/picrin.h index 62fa27bd..7a6ad217 100644 --- a/extlib/benz/include/picrin.h +++ b/extlib/benz/include/picrin.h @@ -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); diff --git a/extlib/benz/state.c b/extlib/benz/state.c index 8da412cf..9c9d621a 100644 --- a/extlib/benz/state.c +++ b/extlib/benz/state.c @@ -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); }