diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 17:36:36 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 17:36:36 +1100 |
commit | 06ed6edcedf30fd8f7e036fffe8f81e8ca89d4b0 (patch) | |
tree | f5ae75e431817a53ee5725b4c0d6f3e61b15a664 /src/std/mem.c | |
parent | 109b6dd1881d90915e972f0d263a032bd262adb5 (diff) | |
parent | b5b7cea24c46d28a4b72bc18ca8ccb2d532007d3 (diff) |
Merge branch 'master' of github.com:omnisci3nce/celeritas-core
Diffstat (limited to 'src/std/mem.c')
-rw-r--r-- | src/std/mem.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/std/mem.c b/src/std/mem.c new file mode 100644 index 0000000..f5b92d4 --- /dev/null +++ b/src/std/mem.c @@ -0,0 +1,32 @@ +#include "mem.h" +#include <stddef.h> +#include <stdint.h> +#include <string.h> +#include "log.h" + +#ifndef DEFAULT_ALIGNMENT +#define DEFAULT_ALIGNMENT (2 * sizeof(void*)) +#endif + +void* arena_alloc_align(arena* a, size_t size, size_t align) { + ptrdiff_t padding = -(uintptr_t)a->curr & (align - 1); + ptrdiff_t available = a->end - a->curr - padding; + TRACE("Padding %td available %td", padding, available); + if (available < 0 || (ptrdiff_t)size > available) { + ERROR_EXIT("Arena ran out of memory\n"); + } + void* p = a->begin + padding; + a->begin += padding + size; + return memset(p, 0, size); +} +void* arena_alloc(arena* a, size_t size) { return arena_alloc_align(a, size, DEFAULT_ALIGNMENT); } + +arena arena_create(void* backing_buffer, size_t capacity) { + return (arena){ .begin = backing_buffer, + .curr = backing_buffer, + .end = backing_buffer + (ptrdiff_t)capacity }; +} + +void arena_free_all(arena* a) { + a->curr = a->begin; // pop everything at once and reset to the start. +}
\ No newline at end of file |