diff --git a/arena.h b/arena.h index 01a910a..be6b766 100644 --- a/arena.h +++ b/arena.h @@ -1,4 +1,4 @@ -// arena.h - v1.2.0 - MIT License +// arena.h - v1.2.1 - MIT License // single header library for region-based memory management. // // [License and changelog] @@ -25,6 +25,11 @@ // // This macro defines the default capacity for arena regions. // +// #define ARENA_REGION_GROWTH_FACTOR new_region_growth_factor (1.5f) +// +// This macro defines the growth factor of the capacity for every +// subsequent region in all arenas. +// // #define ARENA_ALIGNMENT alignment (8) // // This macro defines the memory alignment used for allocations. @@ -153,6 +158,10 @@ int main(void) # define ARENA_REGION_CAPACITY (8*1024) #endif // ARENA_REGION_CAPACITY +#ifndef ARENA_REGION_GROWTH_FACTOR +# define ARENA_REGION_GROWTH_FACTOR 1.5f +#endif // ARENA_REGION_GROWTH_FACTOR + #ifndef ARENA_ALIGNMENT # define ARENA_ALIGNMENT alignof(max_align_t) #endif // ARENA_ALIGNMENT @@ -300,8 +309,16 @@ void *arena_alloc(Arena *a, size_t size) return NULL; } + // Get the next region capacity multiplied by the growth factor size_t region_capacity = (a->region_capacity == 0 ? ARENA_REGION_CAPACITY : a->region_capacity); + if (a->tail != NULL) { + region_capacity = a->tail->capacity * ARENA_REGION_GROWTH_FACTOR; + // Edge case for small growth factors + if (region_capacity <= a->tail->capacity) { + region_capacity = a->tail->capacity + 1; + } + } // Calculate and check region allocation size size_t alloc_size = (size > region_capacity ? size : region_capacity); @@ -491,6 +508,8 @@ size_t arena__align(size_t n) /* * Revision history: * + * 1.2.1 (2026-04-08) Region growth factor handling with new + * ARENA_REGION_GROWTH_FACTOR macro * 1.2.0 (2026-04-08) New functions: arena_sprintf(), arena_snapshot(), * arena_rewind() and arena_trim() * 1.1.3 (2026-01-14) Align Arena_Region data flexible array member diff --git a/test.c b/test.c index b2c0f1f..f9a8849 100644 --- a/test.c +++ b/test.c @@ -9,6 +9,7 @@ #endif // DEBUG #define ARENA_IMPLEMENTATION +#define ARENA_REGION_GROWTH_FACTOR 2 #include "arena.h" void arena_print(Arena arena) @@ -50,8 +51,14 @@ int main(void) arena_print(a); char *str = arena_sprintf(&a, "Formatted string %x", 0xCAFE); + + for (size_t i = 0; i < 1000; ++i) { + str = arena_sprintf(&a, "Formatted string %d", i); + } + printf("%s\n", str); + arena_print(a); arena_free(&a); return 0; }