diff options
author | quou <quou@disroot.org> | 2025-02-07 00:27:11 +1100 |
---|---|---|
committer | quou <quou@disroot.org> | 2025-02-07 00:27:11 +1100 |
commit | 7686aae2667fffd798ee2f0dc7ca1252a36612c5 (patch) | |
tree | 2dc667d63e8bf8834a5c61e8c7ad24b440371227 /qstd | |
parent | 8e5210e987405bb5c79d02a96b9c183c6faad489 (diff) |
arena pop
Diffstat (limited to 'qstd')
-rw-r--r-- | qstd/memory.c | 15 | ||||
-rw-r--r-- | qstd/memory.h | 3 |
2 files changed, 18 insertions, 0 deletions
diff --git a/qstd/memory.c b/qstd/memory.c index 7d669d2..87618f9 100644 --- a/qstd/memory.c +++ b/qstd/memory.c @@ -34,10 +34,12 @@ void init_arena( a->buf = mem; a->size = size; a->ptr = 0; + a->last_push = 0; } void clear_arena(Arena* a) { a->ptr = 0; + a->last_push = 0; } void* imp_arena_alloc( @@ -75,6 +77,19 @@ void* arena_alloc_aligned( return (void*)align_address((uintptr_t)p, align); } +void arena_push(Arena* a) { + int last = a->last_push, * prev; + a->last_push = a->ptr; + prev = arena_alloc(a, sizeof *prev); + *prev = last; +} + +void arena_pop(Arena* a) { + assert(a->last_push); + a->ptr -= a->last_push; + a->last_push = *(int*)&a->buf[a->ptr]; +} + void init_heap( Heap* h, void* mem, diff --git a/qstd/memory.h b/qstd/memory.h index a0b819a..d87dce6 100644 --- a/qstd/memory.h +++ b/qstd/memory.h @@ -13,6 +13,7 @@ void zero(void* buf, int size); typedef struct Arena { char* buf; int size, ptr; + int last_push; } Arena; void init_arena( @@ -30,6 +31,8 @@ void* arena_alloc_aligned( int size, int align ); +void arena_push(Arena* a); +void arena_pop(Arena* a); typedef struct Heap { char* buf; |