From e02e1403b432f8cd6d07ebbdd235627f20f01cfb Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sat, 6 Jun 2020 12:30:48 -0700 Subject: small changes to exposure of allocation functions --- Makefile | 7 ++++--- include/libn.h | 14 +++----------- sys/cmd/cc/cc.c | 18 ++++++++++++++++++ sys/cmd/cc/lex.c | 18 ++++++++++++++++++ sys/cmd/rules.mk | 4 ++-- sys/libn/memory.c | 10 ++++++++++ 6 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Makefile b/Makefile index dd18cc6..c518063 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,8 @@ # Compiler, Linker, and Assembler -CC := gcc -AR := ar -AS := nasm +CC := gcc +AR := ar +AS := nasm +PKG := pkg-config # All needed build directories INC_DIR := include diff --git a/include/libn.h b/include/libn.h index 38f0a3a..d47e7c5 100644 --- a/include/libn.h +++ b/include/libn.h @@ -59,6 +59,7 @@ typedef struct Iface { // ----------------------------------------------------------------------------- // memory allocation +// TODO(nnoll): Allow for nil iterfaces? /* allocator interface */ typedef struct mem·Allocator { void *(*alloc)(void *iface, uint n, ulong size); @@ -72,17 +73,8 @@ typedef struct mem·Reallocator { } mem·Reallocator; /* system implementation */ -extern void ·free(void* _, void *ptr); -extern void *·alloc(void* _, uint n, ulong size); -extern void *·calloc(void* _, uint n, ulong size); -extern void *·realloc(void* _, void *ptr, uint n, ulong size); - -// TODO(nnoll): Allow for nil iterfaces? -static -mem·Allocator mem·sys = { - .alloc = ·alloc, - .free = ·free -}; +extern mem·Allocator mem·sys; +extern mem·Reallocator mem·rsys; /* simple memory arena */ typedef struct mem·Arena mem·Arena; diff --git a/sys/cmd/cc/cc.c b/sys/cmd/cc/cc.c index 3246292..3602643 100644 --- a/sys/cmd/cc/cc.c +++ b/sys/cmd/cc/cc.c @@ -54,6 +54,24 @@ getstr(string key, int *ok) return idx; } +static +void +·free(void* _, void* ptr) { + return free(ptr); +} + +static +void * +·alloc(void* _, uint n, ulong size) { + return malloc(n*size); +} + +static +void * +·calloc(void* _, uint n, ulong size) { + return calloc(n, size); +} + static int morestrtab(StrTab *tab, int n) diff --git a/sys/cmd/cc/lex.c b/sys/cmd/cc/lex.c index 1396358..0963fb9 100644 --- a/sys/cmd/cc/lex.c +++ b/sys/cmd/cc/lex.c @@ -791,6 +791,24 @@ Nospace: #define PTR_HASH(p) (uintptr)(p) #define PTR_EQUAL(p1, p2) ((uintptr)(p1) == (uintptr)(p2)) +static +void +·free(void* _, void* ptr) { + return free(ptr); +} + +static +void * +·alloc(void* _, uint n, ulong size) { + return malloc(n*size); +} + +static +void * +·calloc(void* _, uint n, ulong size) { + return calloc(n, size); +} + static int moresymtab(SymTab *tab, int n) diff --git a/sys/cmd/rules.mk b/sys/cmd/rules.mk index 1a6bd88..d23396c 100644 --- a/sys/cmd/rules.mk +++ b/sys/cmd/rules.mk @@ -14,8 +14,8 @@ include $(DIR)/rules.mk # DIR := $(d)/rc # include $(DIR)/rules.mk -DIR := $(d)/dwm -include $(DIR)/rules.mk +# DIR := $(d)/dwm +# include $(DIR)/rules.mk DIR := $(d)/term include $(DIR)/rules.mk diff --git a/sys/libn/memory.c b/sys/libn/memory.c index 46f2478..31f910e 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -1,26 +1,36 @@ #include #include +static void ·free(void* _, void* ptr) { return free(ptr); } +static void * ·alloc(void* _, uint n, ulong size) { return malloc(n*size); } +static void * ·calloc(void* _, uint n, ulong size) { return calloc(n, size); } +static void * ·realloc(void* _, void *ptr, uint n, ulong size) { return realloc(ptr, n*size); } +mem·Allocator mem·sys = { + .alloc = ·alloc, + .free = ·free +}; + + // ------------------------------------------------------------------------- // Dynamic buffer. -- cgit v1.2.1