From 4c7870c21b9e645b349ddb77b091543b72c46bf5 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Thu, 23 Apr 2020 21:09:30 -0700 Subject: feat: made calling signature of interface accepting functions more reliable --- sys/libn/memory.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'sys/libn/memory.c') diff --git a/sys/libn/memory.c b/sys/libn/memory.c index ca0c819..8081569 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -68,6 +68,8 @@ struct Block struct mem·Arena { mem·Allocator heap; + void *impl; + byte *off; byte *end; struct Block *curr; @@ -75,9 +77,10 @@ struct mem·Arena }; mem·Arena* -mem·newarena(mem·Allocator from) +mem·newarena(mem·Allocator from, void *impl) { - mem·Arena *a = from.alloc(sizeof(*a) + ARENA_BLOCK_SIZE); + mem·Arena *a = from.alloc(impl, 1, sizeof(*a) + ARENA_BLOCK_SIZE); + a->impl = impl; a->heap = from; a->off = a->first.buf; a->end = a->first.buf + ARENA_BLOCK_SIZE; @@ -94,7 +97,7 @@ grow(mem·Arena *a, vlong min) struct Block *blk; size = ALIGN_UP(MAX(min, ARENA_BLOCK_SIZE), ARENA_ALIGN); - blk = a->heap.alloc(sizeof(*blk) + size); + blk = a->heap.alloc(a->impl, 1, sizeof(*blk) + size); a->off = blk->buf; a->end = a->off + size; @@ -106,9 +109,16 @@ grow(mem·Arena *a, vlong min) } void* -mem·arenaalloc(mem·Arena *a, ulong size) +mem·arenaalloc(mem·Arena *a, uint n, ulong size) { + if (!n) { + return nil; + } + void *ptr; + // TODO(nnoll): check for overflow + size = n * size; + if (size > (ulong)(a->end - a->off)) { grow(a, size); Assert(size <= (uintptr)(a->end - a->off)); @@ -131,9 +141,9 @@ mem·freearena(mem·Arena *a) it = a->first.next; while (it != nil) { next = it->next; - a->heap.free(it); + a->heap.free(a->impl, it); it = next; } - a->heap.free(a); + a->heap.free(a->impl, a); } -- cgit v1.2.1