diff options
author | Nicholas Noll <nbnoll@eml.cc> | 2021-12-05 11:55:10 -0800 |
---|---|---|
committer | Nicholas Noll <nbnoll@eml.cc> | 2021-12-05 12:02:10 -0800 |
commit | c200dd832789afa298ba45e0b9efdec96c0e92cc (patch) | |
tree | 4dc5622468a23acfd747e7778eeb9132266d7c9b /include/base/memory.h | |
parent | 8eedf5bdb05c2df0cd339f8742dc4f1752e8aaae (diff) |
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.
Diffstat (limited to 'include/base/memory.h')
-rw-r--r-- | include/base/memory.h | 24 |
1 files changed, 13 insertions, 11 deletions
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; |