aboutsummaryrefslogtreecommitdiff
path: root/sys/libn/memory.c
diff options
context:
space:
mode:
authorNicholas Noll <nbnoll@eml.cc>2020-04-23 21:09:30 -0700
committerNicholas Noll <nbnoll@eml.cc>2020-04-23 21:09:30 -0700
commit4c7870c21b9e645b349ddb77b091543b72c46bf5 (patch)
treef53a77257b62c7ef0729f176daa4c49c5bf57587 /sys/libn/memory.c
parent1a6c99600617f069d6d167fb3d33142a07fe0936 (diff)
feat: made calling signature of interface accepting functions more reliable
Diffstat (limited to 'sys/libn/memory.c')
-rw-r--r--sys/libn/memory.c22
1 files changed, 16 insertions, 6 deletions
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);
}