diff options
-rw-r--r-- | include/base/fmt.h | 4 | ||||
-rw-r--r-- | include/base/memory.h | 24 | ||||
-rw-r--r-- | src/base/fmt/buffer.c | 6 | ||||
-rw-r--r-- | src/base/fmt/open.c | 2 | ||||
-rw-r--r-- | src/base/fs/walk.c | 2 | ||||
-rw-r--r-- | src/base/fs/walker.c | 2 | ||||
-rw-r--r-- | src/base/mem/arena.c | 12 | ||||
-rw-r--r-- | src/base/mem/interface.c | 7 |
8 files changed, 32 insertions, 27 deletions
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; diff --git a/src/base/fmt/buffer.c b/src/base/fmt/buffer.c index 0099e72..e039577 100644 --- a/src/base/fmt/buffer.c +++ b/src/base/fmt/buffer.c @@ -7,7 +7,7 @@ flush(fmt·State *io) char *s; void *heap = io->heap; - mem·Reallocator mem = io->mem; + mem·Allocator mem = io->mem; if(!io->buffer.beg) return 0; @@ -29,7 +29,7 @@ flush(fmt·State *io) } int -fmt·make(mem·Reallocator mem, void *heap, fmt·State *io) +fmt·make(mem·Allocator mem, void *heap, fmt·State *io) { int n; @@ -53,7 +53,7 @@ void fmt·free(fmt·State *io) { void *heap = io->heap; - mem·Reallocator mem = io->mem; + mem·Allocator mem = io->mem; mem.free(heap, io->buffer.beg); io->buffer.beg = io->buffer.cur = io->buffer.end = nil; diff --git a/src/base/fmt/open.c b/src/base/fmt/open.c index b6829bb..2020a2a 100644 --- a/src/base/fmt/open.c +++ b/src/base/fmt/open.c @@ -27,7 +27,7 @@ fmt·open(int fd, int len, char *buf, fmt·State *io) io->n = 0; /* no heap needed */ io->heap = nil; - io->mem = (mem·Reallocator){ 0 }; + io->mem = (mem·Allocator){ 0 }; fmt·setlocale(io, nil, nil, nil); diff --git a/src/base/fs/walk.c b/src/base/fs/walk.c index 4b0d0aa..612aca8 100644 --- a/src/base/fs/walk.c +++ b/src/base/fs/walk.c @@ -6,7 +6,7 @@ static int morehistory(fs·History *h, int n) { - SET_GROW(h, struct Key, n, hash, sys·Memory, nil); + SET_GROW(h, struct Key, n, hash, base·Memory, nil); } static int diff --git a/src/base/fs/walker.c b/src/base/fs/walker.c index d988d13..0a0f61e 100644 --- a/src/base/fs/walker.c +++ b/src/base/fs/walker.c @@ -3,7 +3,7 @@ static void delete(fs·History *h) { - SET_FREE(h, sys·Memory, nil); + SET_FREE(h, base·Memory, nil); } int diff --git a/src/base/mem/arena.c b/src/base/mem/arena.c index 37e7b56..7fe036a 100644 --- a/src/base/mem/arena.c +++ b/src/base/mem/arena.c @@ -31,6 +31,13 @@ static void* return mem·arenaalloc(heap, n, size); } +static void* +·arenarealloc(void *heap, void *old, uint n, ulong size) +{ + /* does not free */ + return mem·arenaalloc(heap, n, size); +} + static void ·arenafree(void *heap, void *ptr) { @@ -38,8 +45,9 @@ static void } mem·Allocator mem·ArenaAllocator = { - .alloc = ·arenaalloc, - .free = ·arenafree, + .alloc = ·arenaalloc, + .realloc = ·arenarealloc, + .free = ·arenafree, }; diff --git a/src/base/mem/interface.c b/src/base/mem/interface.c index e128bb9..99cb774 100644 --- a/src/base/mem/interface.c +++ b/src/base/mem/interface.c @@ -20,12 +20,7 @@ static void * return realloc(ptr, n*size); } -mem·Allocator sys·Memory = { - .alloc = ·calloc, - .free = ·free -}; - -mem·Reallocator sys·FullMemory = { +mem·Allocator base·Memory = { .alloc = ·calloc, .realloc = ·realloc, .free = ·free |