summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoshua Rowe <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 13:49:24 +1100
committerGitHub <noreply@github.com>2024-02-25 13:49:24 +1100
commitfc287c6e4f958242a4272da5cb1704e3e789fe07 (patch)
tree47c8d1fdf271c41f930aaa5d0373a834b1c9e665 /src
parent64f23d9726c55d5c965f123ffc5b6ca47ec1f475 (diff)
parenta1c3e27c53558fde411e63d8b3e3809c79789ea4 (diff)
Merge pull request #1 from omnisci3nce/cel-18-arena-allocator
CEL-18 arena allocator
Diffstat (limited to 'src')
-rw-r--r--src/log.h2
-rw-r--r--src/std/mem.c32
-rw-r--r--src/std/mem.h15
3 files changed, 47 insertions, 2 deletions
diff --git a/src/log.h b/src/log.h
index e7a90ea..b0c355b 100644
--- a/src/log.h
+++ b/src/log.h
@@ -1,6 +1,8 @@
#pragma once
#include <stdbool.h>
+#include <stdio.h>
+#include <stdlib.h>
#define ERROR_EXIT(...) \
{ \
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
diff --git a/src/std/mem.h b/src/std/mem.h
index 74222a7..c3ec61d 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -9,6 +9,17 @@
*/
#pragma once
-#include "defines.h"
+#include <stddef.h>
-typedef void* (*alloc)(size_t amount); \ No newline at end of file
+// Inspired by https://nullprogram.com/blog/2023/09/27/
+typedef struct arena {
+ char* begin;
+ char* curr;
+ char* end;
+} arena;
+
+arena arena_create(void* backing_buffer, size_t capacity);
+void* arena_alloc(arena* a, size_t size);
+void* arena_alloc_align(arena* a, size_t size, size_t align);
+void arena_free_all(arena* a);
+// TODO: arena_resize \ No newline at end of file