From 4b9ab2ec5acf2ecf2e96c38733a835b964664171 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 11:02:46 +1100 Subject: add an arena allocator first impl --- src/std/mem.c | 26 ++++++++++++++++++++++++++ src/std/mem.h | 12 ++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 src/std/mem.c (limited to 'src/std') 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 +#include +#include +#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 -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); -- cgit v1.2.3-70-g09d2 From d919e8cb785c165ee700907b74672eb586c55d29 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:31:17 +1100 Subject: add an arena reset to free everything --- src/std/mem.c | 10 +++++++--- src/std/mem.h | 6 ++++-- 2 files changed, 11 insertions(+), 5 deletions(-) (limited to 'src/std') 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 diff --git a/src/std/mem.h b/src/std/mem.h index 75b6d2a..36d30bd 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -14,9 +14,11 @@ // 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_alloc(arena* a, size_t size); +void* arena_alloc_align(arena* a, size_t size, size_t align); +void arena_free_all(arena* a); \ No newline at end of file -- cgit v1.2.3-70-g09d2 From 1d6be7975928e644e56422f3244aae5571f48ee0 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:31:44 +1100 Subject: chore: format --- src/std/mem.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src/std') diff --git a/src/std/mem.c b/src/std/mem.c index 419a712..f5b92d4 100644 --- a/src/std/mem.c +++ b/src/std/mem.c @@ -22,9 +22,11 @@ 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, .curr = 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. +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 -- cgit v1.2.3-70-g09d2 From a1c3e27c53558fde411e63d8b3e3809c79789ea4 Mon Sep 17 00:00:00 2001 From: Omniscient <17525998+omnisci3nce@users.noreply.github.com> Date: Sun, 25 Feb 2024 13:32:46 +1100 Subject: add TODO for future arena resize fn --- src/std/mem.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/std') diff --git a/src/std/mem.h b/src/std/mem.h index 36d30bd..c3ec61d 100644 --- a/src/std/mem.h +++ b/src/std/mem.h @@ -21,4 +21,5 @@ typedef struct 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); \ No newline at end of file +void arena_free_all(arena* a); +// TODO: arena_resize \ No newline at end of file -- cgit v1.2.3-70-g09d2