From bc53100f1ef063e09d77e8670e1796bc67017411 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Tue, 28 Sep 2021 13:39:24 -0700 Subject: Checkin: various small changes --- sys/libn/memory.c | 45 ++++++++++++++++++++++++++++++++------------- 1 file changed, 32 insertions(+), 13 deletions(-) (limited to 'sys/libn/memory.c') diff --git a/sys/libn/memory.c b/sys/libn/memory.c index 795fe1f..a749e4c 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -3,25 +3,25 @@ static void -·free(void* _, void* ptr) { +·free(void *_, void *ptr) { return free(ptr); } static void * -·alloc(void* _, uint n, ulong size) { +·alloc(void *_, uint n, ulong size) { return malloc(n*size); } static void * -·calloc(void* _, uint n, ulong size) { +·calloc(void *_, uint n, ulong size) { return calloc(n, size); } static void * -·realloc(void* _, void *ptr, uint n, ulong size) { +·realloc(void *_, void *ptr, uint n, ulong size) { return realloc(ptr, n*size); } @@ -89,7 +89,7 @@ void #define ALIGN_DOWN_PTR(p, a) ((void*)ALIGN_DOWN((uintptr)(p), (a))) #define ALIGN_UP_PTR(p, a) ((void*)ALIGN_UP((uintptr)(p), (a))) -struct Block +struct Block { struct Block *next; byte buf[]; @@ -97,8 +97,8 @@ struct Block struct mem·Arena { - mem·Allocator heap; - void *impl; + void *heap; + mem·Allocator mem; byte *off; byte *end; @@ -110,8 +110,8 @@ mem·Arena* mem·makearena(mem·Allocator from, void *impl) { mem·Arena *a = from.alloc(impl, 1, sizeof(*a) + ARENA_BLOCK_SIZE); - a->impl = impl; - a->heap = from; + a->mem = from; + a->heap = impl; a->off = a->first.buf; a->end = a->first.buf + ARENA_BLOCK_SIZE; a->curr = &a->first; @@ -128,7 +128,7 @@ grow(mem·Arena *a, vlong min) struct Block *blk; size = ALIGN_UP(MAX(min, ARENA_BLOCK_SIZE), ARENA_ALIGN); - blk = a->heap.alloc(a->impl, 1, sizeof(*blk) + size); + blk = a->mem.alloc(a->heap, 1, sizeof(*blk) + size); a->off = blk->buf; a->end = a->off + size; @@ -142,7 +142,7 @@ grow(mem·Arena *a, vlong min) void* mem·arenaalloc(mem·Arena *a, uint n, ulong size) { - if (!n) { + if(!n) { return nil; } @@ -172,13 +172,32 @@ mem·freearena(mem·Arena *a) it = a->first.next; while (it != nil) { next = it->next; - a->heap.free(a->impl, it); + a->mem.free(a->heap, it); it = next; } - a->heap.free(a->impl, a); + a->mem.free(a->heap, a); } +static +void* +·arenaalloc(void *heap, uint n, ulong size) +{ + return mem·arenaalloc(heap, n, size); +} + +static +void +·arenafree(void *heap, void *ptr) +{ + /* no-op */ +} + +mem·Allocator mem·ArenaAllocator = { + .alloc = ·arenaalloc, + .free = ·arenafree, +}; + // ------------------------------------------------------------------------- // Generalized memory helpers -- cgit v1.2.1