aboutsummaryrefslogtreecommitdiff
path: root/teensy
diff options
context:
space:
mode:
authoriamcheeseman <[email protected]>2026-05-11 09:16:23 -0400
committeriamcheeseman <[email protected]>2026-05-11 09:16:23 -0400
commit4ab4f2a64a3cd2b3c9a49a2a41040af96ed28b14 (patch)
treefee75ebd72c2a23b5c32b003597bf0f08319a4ea /teensy
parent1300ce4c9cdfa2e6553b32b42bbe2f087a9e816f (diff)
change the temp allocator to be an arena
Diffstat (limited to 'teensy')
-rw-r--r--teensy/teensy_mem.c32
1 files changed, 15 insertions, 17 deletions
diff --git a/teensy/teensy_mem.c b/teensy/teensy_mem.c
index 2a6d992..735cf55 100644
--- a/teensy/teensy_mem.c
+++ b/teensy/teensy_mem.c
@@ -3,8 +3,10 @@
#include "teensy_common.h"
#include "teensy_list.h"
-// Array to track temp allocations.
-void **temp_allocations;
+#define TEMP_ALLOC_ARENA_SIZE (1024*1024)
+
+uint8_t temp_arena[TEMP_ALLOC_ARENA_SIZE];
+uint8_t *next_temp_alloc = temp_arena;
#ifdef TEENSY_DEBUG
@@ -34,13 +36,10 @@ void ty_init_mem(void)
if (!allocations)
ty_log_fatal(TY_ERR_MEM, "(%s) (track) ran out of memory", __func__);
#endif // TEENSY_DEBUG
-
- temp_allocations = ty_list_create();
}
void ty_deinit_mem(void)
{
- ty_list_free(temp_allocations);
#ifdef TEENSY_DEBUG
fprintf(stderr, "%d allocations leaked\n", allocations_len);
for (int i = 0; i < allocations_len; i++) {
@@ -65,13 +64,19 @@ void ty_deinit_mem(void)
#endif // TEENSY_DEBUG
}
+void *ty_talloc(size_t size)
+{
+ if (next_temp_alloc > temp_arena + TEMP_ALLOC_ARENA_SIZE)
+ ty_log_fatal(TY_ERR_MEM, "bump up the temp alloc arena size");
+
+ void *ptr = next_temp_alloc;
+ next_temp_alloc += size;
+ return ptr;
+}
+
void ty_free_temp_allocs(void)
{
- for (size_t i = 0; i < ty_list_len(temp_allocations); i++) {
- void *temp = temp_allocations[i];
- ty_free(temp);
- }
- ty_list_clear(temp_allocations);
+ next_temp_alloc = temp_arena;
}
void *ty_alloc(size_t size)
@@ -137,13 +142,6 @@ void *ty_realloc(void *ptr, size_t new_size)
return new_ptr;
}
-void *ty_talloc(size_t size)
-{
- void *ptr = ty_alloc(size);
- ty_list_append(temp_allocations, ptr);
- return ptr;
-}
-
void ty_free(void *ptr)
{
free(ptr);