Skip to content
This repository has been archived by the owner on Mar 26, 2024. It is now read-only.

Commit

Permalink
custom allocator and custom abort
Browse files Browse the repository at this point in the history
  • Loading branch information
nyuichi committed May 27, 2015
1 parent 271898a commit 2780759
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 58 deletions.
1 change: 1 addition & 0 deletions extlib/benz/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,7 @@ emit_r(codegen_state *state, enum pic_opcode insn, int d, int i)
static void
create_activation(codegen_state *state)
{
pic_state *pic = state->pic;
codegen_context *cxt = state->cxt;
size_t i, n;
xhash regs;
Expand Down
8 changes: 7 additions & 1 deletion extlib/benz/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,14 @@ pic_panic(pic_state *pic, const char *msg)
{
PIC_UNUSED(pic);

#if DEBUG
fprintf(stderr, "abort: %s\n", msg);
abort();
#else
PIC_UNUSED(msg);
#endif
PIC_ABORT(pic);

PIC_UNREACHABLE();
}

void
Expand Down
14 changes: 8 additions & 6 deletions extlib/benz/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ add_heap_page(pic_state *pic)
pic->heap->pages = page;
}

static void *
alloc(void *ptr, size_t size)
#if PIC_ENABLE_LIBC
void *
pic_default_allocf(void *ptr, size_t size)
{
if (size == 0) {
if (ptr) {
Expand All @@ -127,13 +128,14 @@ alloc(void *ptr, size_t size)
return malloc(size);
}
}
#endif

void *
pic_alloc(pic_state *pic, size_t size)
{
void *ptr;

ptr = alloc(NULL, size);
ptr = pic->allocf(NULL, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
Expand All @@ -143,7 +145,7 @@ pic_alloc(pic_state *pic, size_t size)
void *
pic_realloc(pic_state *pic, void *ptr, size_t size)
{
ptr = alloc(ptr, size);
ptr = pic->allocf(ptr, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
Expand All @@ -156,7 +158,7 @@ pic_calloc(pic_state *pic, size_t count, size_t size)
void *ptr;

size *= count;
ptr = alloc(NULL, size);
ptr = pic->allocf(NULL, size);
if (ptr == NULL && size > 0) {
pic_panic(pic, "memory exhausted");
}
Expand All @@ -169,7 +171,7 @@ pic_free(pic_state *pic, void *ptr)
{
PIC_UNUSED(pic);

free(ptr);
pic->allocf(ptr, 0);
}

static void
Expand Down
7 changes: 6 additions & 1 deletion extlib/benz/include/picrin.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,14 @@ typedef struct {
struct pic_env *up;
} pic_callinfo;

typedef void *(*pic_allocf)(void *, size_t);

typedef struct {
int argc;
char **argv, **envp;

pic_allocf allocf;

struct pic_winder *wind;

pic_value *sp;
Expand Down Expand Up @@ -167,7 +171,8 @@ void pic_gc_arena_restore(pic_state *, size_t);
pic_gc_arena_restore(pic, ai); \
} while (0)

pic_state *pic_open(int argc, char *argv[], char **envp);
pic_state *pic_open(int argc, char *argv[], char **envp, pic_allocf);
void *pic_default_allocf(void *, size_t);
void pic_close(pic_state *);

void pic_add_feature(pic_state *, const char *);
Expand Down
7 changes: 7 additions & 0 deletions extlib/benz/include/picrin/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
/* #define PIC_SETJMP(pic, buf) setjmp(buf) */
/* #define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val)) */

/** custom abort */
/* #define PIC_ABORT(pic) abort() */

/** initial memory size (to be dynamically extended if necessary) */
/* #define PIC_ARENA_SIZE 1000 */

Expand Down Expand Up @@ -112,6 +115,10 @@
# define PIC_LONGJMP(pic, buf, val) longjmp((buf), (val))
#endif

#ifndef PIC_ABORT
# define PIC_ABORT(pic) abort()
#endif

#ifndef PIC_ARENA_SIZE
# define PIC_ARENA_SIZE (8 * 1024)
#endif
Expand Down
58 changes: 28 additions & 30 deletions extlib/benz/include/picrin/xhash.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
extern "C" {
#endif

#define XHASH_ALLOCATOR pic->allocf

/* simple object to object hash table */

#define XHASH_INIT_SIZE 11
#define XHASH_RESIZE_RATIO 0.75
#define XHASH_RESIZE_RATIO(x) ((x) * 3 / 4)

#define XHASH_ALIGNMENT 3 /* quad word alignment */
#define XHASH_MASK (~(size_t)((1 << XHASH_ALIGNMENT) - 1))
Expand All @@ -31,8 +33,10 @@ typedef struct xh_entry {

typedef int (*xh_hashf)(const void *, void *);
typedef int (*xh_equalf)(const void *, const void *, void *);
typedef void *(*xh_allocf)(void *, size_t);

typedef struct xhash {
xh_allocf allocf;
xh_entry **buckets;
size_t size, count, kwidth, vwidth;
size_t koffset, voffset;
Expand Down Expand Up @@ -76,16 +80,17 @@ PIC_INLINE xh_entry *xh_next(xh_entry *e);


PIC_INLINE void
xh_bucket_realloc(xhash *x, size_t newsize)
xh_bucket_alloc(xhash *x, size_t newsize)
{
x->size = newsize;
x->buckets = realloc(x->buckets, (x->size + 1) * sizeof(xh_entry *));
x->buckets = x->allocf(NULL, (x->size + 1) * sizeof(xh_entry *));
memset(x->buckets, 0, (x->size + 1) * sizeof(xh_entry *));
}

PIC_INLINE void
xh_init_(xhash *x, size_t kwidth, size_t vwidth, xh_hashf hashf, xh_equalf equalf, void *data)
xh_init_(xhash *x, xh_allocf allocf, size_t kwidth, size_t vwidth, xh_hashf hashf, xh_equalf equalf, void *data)
{
x->allocf = allocf;
x->size = 0;
x->buckets = NULL;
x->count = 0;
Expand All @@ -99,7 +104,7 @@ xh_init_(xhash *x, size_t kwidth, size_t vwidth, xh_hashf hashf, xh_equalf equal
x->tail = NULL;
x->data = data;

xh_bucket_realloc(x, XHASH_INIT_SIZE);
xh_bucket_alloc(x, XHASH_INIT_SIZE);
}

PIC_INLINE xh_entry *
Expand All @@ -125,8 +130,8 @@ xh_resize_(xhash *x, size_t newsize)
xh_entry *it;
size_t idx;

xh_init_(&y, x->kwidth, x->vwidth, x->hashf, x->equalf, x->data);
xh_bucket_realloc(&y, newsize);
xh_init_(&y, x->allocf, x->kwidth, x->vwidth, x->hashf, x->equalf, x->data);
xh_bucket_alloc(&y, newsize);

for (it = xh_begin(x); it != NULL; it = xh_next(it)) {
idx = ((unsigned)it->hash) % y.size;
Expand All @@ -139,7 +144,7 @@ xh_resize_(xhash *x, size_t newsize)
y.head = x->head;
y.tail = x->tail;

free(x->buckets);
x->allocf(x->buckets, 0);

/* copy all members from y to x */
memcpy(x, &y, sizeof(xhash));
Expand All @@ -157,13 +162,13 @@ xh_put_(xhash *x, const void *key, void *val)
return e;
}

if (x->count + 1 > x->size * XHASH_RESIZE_RATIO) {
if (x->count + 1 > XHASH_RESIZE_RATIO(x->size)) {
xh_resize_(x, x->size * 2 + 1);
}

hash = x->hashf(key, x->data);
idx = ((unsigned)hash) % x->size;
e = malloc(x->voffset + x->vwidth);
e = x->allocf(NULL, x->voffset + x->vwidth);
e->next = x->buckets[idx];
e->hash = hash;
e->key = ((char *)e) + x->koffset;
Expand Down Expand Up @@ -208,7 +213,7 @@ xh_del_(xhash *x, const void *key)
q->bw->fw = q->fw;
}
r = q->next;
free(q);
x->allocf(q, 0);
x->buckets[idx] = r;
}
else {
Expand All @@ -228,7 +233,7 @@ xh_del_(xhash *x, const void *key)
q->bw->fw = q->fw;
}
r = q->next;
free(q);
x->allocf(q, 0);
p->next = r;
}

Expand All @@ -251,7 +256,7 @@ xh_clear(xhash *x)
e = x->buckets[i];
while (e) {
d = e->next;
free(e);
x->allocf(e, 0);
e = d;
}
x->buckets[i] = NULL;
Expand All @@ -265,7 +270,7 @@ PIC_INLINE void
xh_destroy(xhash *x)
{
xh_clear(x);
free(x->buckets);
x->allocf(x->buckets, 0);
}

/* string map */
Expand All @@ -287,16 +292,15 @@ xh_str_hash(const void *key, void *data)
PIC_INLINE int
xh_str_equal(const void *key1, const void *key2, void *data)
{
const char *s1 = *(const char **)key1, *s2 = *(const char **)key2;

(void)data;

return strcmp(*(const char **)key1, *(const char **)key2) == 0;
return strcmp(s1, s2) == 0;
}

PIC_INLINE void
xh_init_str(xhash *x, size_t width)
{
xh_init_(x, sizeof(const char *), width, xh_str_hash, xh_str_equal, NULL);
}
#define xh_init_str(x, width) \
xh_init_(x, XHASH_ALLOCATOR, sizeof(const char *), width, xh_str_hash, xh_str_equal, NULL);

PIC_INLINE xh_entry *
xh_get_str(xhash *x, const char *key)
Expand Down Expand Up @@ -334,11 +338,8 @@ xh_ptr_equal(const void *key1, const void *key2, void *data)
return *(const void **)key1 == *(const void **)key2;
}

PIC_INLINE void
xh_init_ptr(xhash *x, size_t width)
{
xh_init_(x, sizeof(const void *), width, xh_ptr_hash, xh_ptr_equal, NULL);
}
#define xh_init_ptr(x, width) \
xh_init_(x, XHASH_ALLOCATOR, sizeof(const void *), width, xh_ptr_hash, xh_ptr_equal, NULL);

PIC_INLINE xh_entry *
xh_get_ptr(xhash *x, const void *key)
Expand Down Expand Up @@ -376,11 +377,8 @@ xh_int_equal(const void *key1, const void *key2, void *data)
return *(int *)key1 == *(int *)key2;
}

PIC_INLINE void
xh_init_int(xhash *x, size_t width)
{
xh_init_(x, sizeof(int), width, xh_int_hash, xh_int_equal, NULL);
}
#define xh_init_int(x, width) \
xh_init_(x, XHASH_ALLOCATOR, sizeof(int), width, xh_int_hash, xh_int_equal, NULL);

PIC_INLINE xh_entry *
xh_get_int(xhash *x, int key)
Expand Down
6 changes: 3 additions & 3 deletions extlib/benz/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ strfile_seek(void *cookie, long pos, int whence)
struct strfile *m = cookie;

switch (whence) {
case XF_SEEK_SET:
case SEEK_SET:
m->pos = pos;
break;
case XF_SEEK_CUR:
case SEEK_CUR:
m->pos += pos;
break;
case XF_SEEK_END:
case SEEK_END:
m->pos = m->end + pos;
break;
}
Expand Down

0 comments on commit 2780759

Please sign in to comment.