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/io.c | 4 ++-- sys/libn/memory.c | 22 ++++++++++++++++------ sys/libn/rules.mk | 8 ++++---- 3 files changed, 22 insertions(+), 12 deletions(-) (limited to 'sys/libn') diff --git a/sys/libn/io.c b/sys/libn/io.c index 7eec74e..ff64ff0 100644 --- a/sys/libn/io.c +++ b/sys/libn/io.c @@ -31,7 +31,7 @@ io·ungetbyte(Stream *s, byte c) return ungetc(c, s); } -vlong +int io·read(Stream *s, int sz, int n, void *buf) { return fread(buf, sz, n, s); @@ -61,7 +61,7 @@ io·putstring(Stream *s, string str) return fputs(str, s); } -vlong +int io·write(Stream *s, int sz, int n, void *buf) { return fwrite(buf, sz, n, s); 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); } diff --git a/sys/libn/rules.mk b/sys/libn/rules.mk index 15f2f2d..19e4c50 100644 --- a/sys/libn/rules.mk +++ b/sys/libn/rules.mk @@ -29,14 +29,14 @@ BINS_$(d) := $(patsubst $(SRC_DIR)/%, $(OBJ_DIR)/%, $(BINS_$(d))) BINS := $(BINS) $(BINS_$(d)) # Local rules -# $(LIBS_$(d)) := TGTFLAGS := -# $(LIBS_$(d)) := TGTINCS := -# $(LIBS_$(d)) := TGTLIBS := +# $(LIBS_$(d)) := TCFLAGS := +# $(LIBS_$(d)) := TCINCS := +# $(LIBS_$(d)) := TCLIBS := $(LIBS_$(d)): $(OBJS_$(d)) $(ARCHIVE) -$(BINS_$(d)): TCLIBS := $(LIBS_$(d)) +$(BINS_$(d)): TCLIBS := $(LIBS_$(d)) $(LIB_DIR)/vendor/libz.a $(BINS_$(d)): $(OBJ_DIR)/libn/test.o $(LINK) -- cgit v1.2.1