diff options
author | Imogen Thoms <[email protected]> | 2025-01-30 21:35:11 +0000 |
---|---|---|
committer | Ren Kararou <[email protected]> | 2025-01-30 15:40:27 -0600 |
commit | f6dda8af9998eba76ff377c91cb69593c086b8c0 (patch) | |
tree | e180556894730e4f82b79a8b345718af261d2faa | |
parent | 11ff7f4f76d6deb5afab072ded3e115b130069c8 (diff) | |
download | libspicy-f6dda8af9998eba76ff377c91cb69593c086b8c0.tar.gz libspicy-f6dda8af9998eba76ff377c91cb69593c086b8c0.tar.bz2 libspicy-f6dda8af9998eba76ff377c91cb69593c086b8c0.zip |
added mogi_arena allocator
Signed-off-by: Imogen Thoms <[email protected]>
-rw-r--r-- | inc/arena.h | 24 | ||||
-rw-r--r-- | makefile | 2 | ||||
-rw-r--r-- | src/arena.c | 38 |
3 files changed, 63 insertions, 1 deletions
diff --git a/inc/arena.h b/inc/arena.h new file mode 100644 index 0000000..dda105b --- /dev/null +++ b/inc/arena.h @@ -0,0 +1,24 @@ +/* + * arena.h + * arena allocator i wrote during a meeting in 30 mins + * imogen sorindeia thoms 2025 + * */ + +#ifndef MOGI_ARENA_H +#define MOGI_ARENA_H + +#include <stdlib.h> +#include <stdint.h> + +struct _mogi_arena_s { + char *buffer; + size_t posn; + size_t capacity; +}; +typedef struct _mogi_arena_s mogi_arena_t; + +mogi_arena_t mogi_arena_init(size_t size); +void mogi_arena_dispose(mogi_arena_t *a); +uintptr_t mogi_arena_allocate(mogi_arena_t *a, size_t size); + +#endif /* MOGI_ARENA_H */ diff --git a/makefile b/makefile index 26595fd..8ac6378 100644 --- a/makefile +++ b/makefile @@ -13,7 +13,7 @@ endif INCLUDES=-Iinc/ -OBJECTS=obj/salloc.o +OBJECTS=obj/salloc.o obj/arena.o .PHONY: all all: build/lib/libspicy.so build/lib/static/libspicy.a includes diff --git a/src/arena.c b/src/arena.c new file mode 100644 index 0000000..e38ad20 --- /dev/null +++ b/src/arena.c @@ -0,0 +1,38 @@ +/* + * arena.c + * arena allocator i wrote during a meeting in 30 mins + * imogen sorindeia thoms 2025 + * */ + +#include <stdlib.h> +#include <stdint.h> + +#include "arena.h" + +/* initialize an arena with given capacity */ +mogi_arena_t mogi_arena_init(size_t size) { + mogi_arena_t a = { + .buffer = calloc(size, 1), + .capacity = size + }; + return a; +} + +/* get rid of entire arena */ +void mogi_arena_dispose(mogi_arena_t *a) { + if(NULL != a->buffer) { + free(a->buffer); + } +} + +/* allocate space on arena + * TODO: alignment l m f a o + */ +uintptr_t mogi_arena_allocate(mogi_arena_t *a, size_t size) { + if((NULL == a->buffer) || ((a->posn + size) > a->capacity)) { + return (uintptr_t)0; + } + uintptr_t p = (uintptr_t)(a->buffer + a->posn); + a->posn += (uintptr_t)size; + return p; +} |