From c200dd832789afa298ba45e0b9efdec96c0e92cc Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Sun, 5 Dec 2021 11:55:10 -0800 Subject: Chore: simplified allocator interfaces. I was never happy with the allocator/reallocator split. It was originally designed to accomodate things like arenas that don't free. But the majority of the time you don't care about this. --- include/base/fmt.h | 4 ++-- include/base/memory.h | 24 +++++++++++++----------- 2 files changed, 15 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/base/fmt.h b/include/base/fmt.h index 92e3261..d4176e8 100644 --- a/include/base/fmt.h +++ b/include/base/fmt.h @@ -22,7 +22,7 @@ struct fmt·State int (*flush)(fmt·State *); struct { void *heap; - mem·Reallocator mem; + mem·Allocator mem; }; }; @@ -64,7 +64,7 @@ char *fmt·vesprint(char *buf, char *end, char *fmt, va_list args); /* low-level interface: used for custom printing verbs */ int fmt·open(int fd, int len, char *buf, fmt·State *); // creates a buffer on a file -int fmt·make(mem·Reallocator mem, void *heap, fmt·State *); // creates an in-memory buffer +int fmt·make(mem·Allocator mem, void *heap, fmt·State *); // creates an in-memory buffer void fmt·free(fmt·State *); // releases an in-memory buffer int fmt·do(fmt·State *, char *fmt); diff --git a/include/base/memory.h b/include/base/memory.h index dd00992..767e948 100644 --- a/include/base/memory.h +++ b/include/base/memory.h @@ -21,6 +21,7 @@ typedef struct mem·BufHead byte buf[]; } mem·BufHead; +#define mem·buffer(x) x* #define mem·bufhdr(b) ((mem·BufHead*)((uint8*)(b)-offsetof(mem·BufHead, buf))) #define mem·buflen(b) ((b) ? (mem·bufhdr(b)->len) : 0) #define mem·bufcap(b) ((b) ? (mem·bufhdr(b)->cap) : 0) @@ -44,26 +45,27 @@ void* mem·bufgrow(void*, vlong, vlong); // memory allocation /* interfaces */ +typedef struct mem·Pool mem·Pool; +typedef struct mem·Arena mem·Arena; + typedef struct mem·Allocator { void *(*alloc)(void *heap, uint n, ulong size); + void *(*realloc)(void *iface, void *ptr, uint n, ulong size); void (*free)(void *heap, void *ptr); } mem·Allocator; -extern mem·Allocator sys·Memory; +extern mem·Allocator base·Memory; /* standard allocator */ -typedef struct mem·Reallocator { - void *(*alloc)(void *iface, uint n, ulong size); - void *(*realloc)(void *iface, void *ptr, uint n, ulong size); - void (*free)(void *iface, void *ptr); -} mem·Reallocator; +/* pool allocator (manages free list) */ +mem·Pool *mem·makepool(mem·Allocator from, void*); +void mem·freepool(mem·Pool); -extern mem·Reallocator sys·FullMemory; - -/* simple memory arena */ -typedef struct mem·Arena mem·Arena; +void *mem·poolalloc(mem·Pool *, uintptr n, uintptr size); +void *mem·poolrealloc(mem·Pool *, void *, uintptr n, uintptr size); +void mem·poolfree(mem·Pool *, void *); +/* simple arena allocator (heterogeneous bump) */ mem·Arena *mem·makearena(mem·Allocator from, void*); void *mem·arenaalloc(mem·Arena *A, uint n, ulong size); void mem·freearena(mem·Arena *A); - extern mem·Allocator mem·ArenaAllocator; -- cgit v1.2.1