summaryrefslogtreecommitdiff
path: root/src/std/mem.c
diff options
context:
space:
mode:
authorOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 13:31:17 +1100
committerOmniscient <17525998+omnisci3nce@users.noreply.github.com>2024-02-25 13:31:17 +1100
commitd919e8cb785c165ee700907b74672eb586c55d29 (patch)
tree7c43ccd269acaa1a2b384e951d8bb1ebd361743a /src/std/mem.c
parent4b9ab2ec5acf2ecf2e96c38733a835b964664171 (diff)
add an arena reset to free everything
Diffstat (limited to 'src/std/mem.c')
-rw-r--r--src/std/mem.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/src/std/mem.c b/src/std/mem.c
index c30f76d..419a712 100644
--- a/src/std/mem.c
+++ b/src/std/mem.c
@@ -9,8 +9,8 @@
#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;
+ 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");
@@ -22,5 +22,9 @@ void* arena_alloc_align(arena* a, size_t size, size_t align) {
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 };
+ 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