aboutsummaryrefslogtreecommitdiff
path: root/teensy/teensy_mem.h
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-29 22:28:43 -0400
committeriamcheeseman <[email protected]>2026-05-29 22:28:43 -0400
commit648504907c9f62adc72628ae73a8c668ec1276c8 (patch)
treed95ba176f4677581e2b3971e8cea4179ba908023 /teensy/teensy_mem.h
parent840b5c151fcd207e904fb2a6d3bb3fd658ef6209 (diff)
Generic arena allocator
Also replaces the temp allocator behind the scenes
Diffstat (limited to 'teensy/teensy_mem.h')
-rw-r--r--teensy/teensy_mem.h28
1 files changed, 27 insertions, 1 deletions
diff --git a/teensy/teensy_mem.h b/teensy/teensy_mem.h
index 133869f..b4cdc62 100644
--- a/teensy/teensy_mem.h
+++ b/teensy/teensy_mem.h
@@ -1,11 +1,37 @@
#ifndef TEENSY_MEM_H_
#define TEENSY_MEM_H_
+#include <stdint.h>
#include <stdlib.h>
#define ty_new(T) (ty_alloc(sizeof(T)))
-void ty_free_temp_allocs(void);
+// A specific section of memory allocated for an arena.
+typedef struct Arena_Hunk Arena_Hunk;
+struct Arena_Hunk {
+ Arena_Hunk *next;
+ uint8_t *start;
+ uint8_t *end;
+ uint8_t *head;
+};
+
+// Memory arena. It allows the user of this API to allocate lots of memory and
+// free it all at once. Arenas do not support reallocation.
+typedef struct {
+ Arena_Hunk *hunks;
+ size_t hunk_size;
+ size_t alloced_size;
+} Arena;
+
+// Makes an allocation with the speicified arena.
+void *ty_arena_alloc(Arena *arena, size_t size);
+// Frees all allocations made with this arena.
+void ty_arena_clear(Arena *arena);
+// Frees the arena itself. Do NOT call to free allocations made with the arena.
+// Use `ty_arena_clear()` for that.
+void ty_arena_free(Arena *arena);
+
+void ty_clear_temp_allocs(void);
// Temp allocation. Freed at the end of every frame. Do NOT realloc.
void *ty_talloc(size_t size);