summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/log.h2
-rw-r--r--src/maths/geometry.h2
-rw-r--r--src/std/mem.c26
-rw-r--r--src/std/mem.h12
-rw-r--r--tests/arena_tests.c12
5 files changed, 51 insertions, 3 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/maths/geometry.h b/src/maths/geometry.h
index 6f4797b..937c38a 100644
--- a/src/maths/geometry.h
+++ b/src/maths/geometry.h
@@ -4,7 +4,7 @@
* @brief Shapes and intersections between them
* @version 0.1
* @date 2024-02-24
- *
+ *
* @copyright Copyright (c) 2024
*/
#pragma once
diff --git a/src/std/mem.c b/src/std/mem.c
new file mode 100644
index 0000000..c30f76d
--- /dev/null
+++ b/src/std/mem.c
@@ -0,0 +1,26 @@
+#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->begin & (align - 1);
+ ptrdiff_t available = a->end - a->begin - 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, .end = backing_buffer + (ptrdiff_t)capacity };
+} \ No newline at end of file
diff --git a/src/std/mem.h b/src/std/mem.h
index 74222a7..75b6d2a 100644
--- a/src/std/mem.h
+++ b/src/std/mem.h
@@ -9,6 +9,14 @@
*/
#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* 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);
diff --git a/tests/arena_tests.c b/tests/arena_tests.c
new file mode 100644
index 0000000..88fc21b
--- /dev/null
+++ b/tests/arena_tests.c
@@ -0,0 +1,12 @@
+/*
+size_t arena_size = 16;
+ arena scratch = arena_create(malloc(arena_size), arena_size);
+
+ i32* int_ptr = arena_alloc(&scratch, sizeof(i32));
+ i32* b = arena_alloc(&scratch, sizeof(i32));
+ i32* c = arena_alloc(&scratch, sizeof(i32));
+ *int_ptr = 55;
+ printf("Int pointer %d %p\n", (*int_ptr), int_ptr);
+
+ // will abort on second arena alloc
+*/ \ No newline at end of file