From 1b7e73d3cbf552007a015923daf9ffc21c06479c Mon Sep 17 00:00:00 2001 From: seajee Date: Tue, 18 Nov 2025 21:27:33 +0100 Subject: [PATCH] v1.1.1 Implement memory alignment; improve docs --- arena.h | 154 ++++++++++++++++++++++++++++++++------------------------ test.c | 30 +++-------- 2 files changed, 94 insertions(+), 90 deletions(-) diff --git a/arena.h b/arena.h index 2c1f937..a69f9b4 100644 --- a/arena.h +++ b/arena.h @@ -1,4 +1,4 @@ -// arena.h - v1.1.0 - MIT License +// arena.h - v1.1.1 - MIT License // single header library for region-based memory management. // // [License and changelog] @@ -21,6 +21,14 @@ // Note: every compile-time option listed here should be configured before // the #include "arena.h" // +// #define ARENA_REGION_CAPACITY new_region_capacity_in_bytes (4096) +// +// This macro defines the default capacity for arena regions. +// +// #define ARENA_ALIGNMENT alignment (8) +// +// This macro defines the memory alignment used for allocations. +// // #define ARENA_NO_ASSERT // // This macro disables assertions that happen when allocations fail. @@ -28,21 +36,18 @@ // that returns NULL when it fails. This macro affects every function // that returns a pointer. // -// #define ARENA_REGION_CAPACITY new_region_capacity_in_bytes (4096) -// -// This macro defines the default capacity for arena regions. -// // #define ARENA_ASSERT my_assert // // This macro defines an alternative function for assertions. This has // no effect when ARENA_NO_ASSERT is defined. // -// #define ARENA_REALLOC my_realloc +// #define ARENA_MALLOC my_malloc // #define ARENA_FREE my_free // // These macros define alternative functions for dynamic allocation -// and deallocation on the heap. They are only used for managing -// Arena_Region structures. +// and deallocation. They are only used for managing Arena_Region +// structures. If redefined ensure that your functions consider memory +// alignment. // // [Function documentation] // @@ -54,7 +59,7 @@ // // This function is optional (see [Example] section): initializes an arena // with a specified region capacity. It will override the default -// ARENA_REGION_CAPACITY. +// ARENA_REGION_CAPACITY. // // void *arena_alloc(Arena *a, size_t size) // @@ -119,6 +124,10 @@ int main(void) # define ARENA_REGION_CAPACITY (8*1024) #endif // ARENA_REGION_CAPACITY +#ifndef ARENA_ALIGNMENT +# define ARENA_ALIGNMENT sizeof(max_align_t) +#endif // ARENA_ALIGNMENT + #ifndef ARENA_NO_ASSERT # ifndef ARENA_ASSERT # include @@ -128,10 +137,10 @@ int main(void) # define ARENA_ASSERT(...) ((void)0) #endif // ARENA_NO_ASSERT -#ifndef ARENA_REALLOC +#ifndef ARENA_MALLOC # include -# define ARENA_REALLOC realloc -#endif // ARENA_REALLOC +# define ARENA_MALLOC malloc +#endif // ARENA_MALLOC #ifndef ARENA_FREE # include @@ -158,10 +167,11 @@ typedef struct Arena { } Arena; Arena arena_create(size_t region_capacity); +void arena_free(Arena *a); + void *arena_alloc(Arena *a, size_t size); void *arena_copy(Arena *a, const void *src, size_t size); char *arena_strdup(Arena *a, const char *s); -void arena_free(Arena *a); void arena_reset(Arena *a); #ifdef __cplusplus @@ -184,6 +194,24 @@ Arena arena_create(size_t region_capacity) return a; } +void arena_free(Arena *a) +{ + if (a == NULL) { + return; + } + + Arena_Region *cur = a->head; + while (cur != NULL) { + Arena_Region *next = cur->next; + ARENA_FREE(cur); + cur = next; + } + + a->head = NULL; + a->tail = NULL; + // a->region_capacity = 0; +} + void *arena_alloc(Arena *a, size_t size) { if (a == NULL || size == 0) { @@ -191,15 +219,28 @@ void *arena_alloc(Arena *a, size_t size) return NULL; } + // Ensure memory alignment of requested size + size = (size + (ARENA_ALIGNMENT - 1)) & ~(ARENA_ALIGNMENT - 1); + + // Check for overflow + if (size > SIZE_MAX - sizeof(Arena_Region)) { + ARENA_ASSERT(false && "Requested size too large"); + return NULL; + } + size_t region_capacity = (a->region_capacity == 0 - ? ARENA_REGION_CAPACITY : a->region_capacity); + ? ARENA_REGION_CAPACITY : a->region_capacity); - // Empty arena + // Calculate and check region allocation size + size_t alloc_size = (size > region_capacity ? size : region_capacity); + if (alloc_size > SIZE_MAX - sizeof(Arena_Region)) { + ARENA_ASSERT(false && "Region size overflow"); + return NULL; + } + + // Empty arena: allocate first region if (a->head == NULL) { - size_t alloc_size = (size > region_capacity ? size : region_capacity); - a->head = (Arena_Region*)ARENA_REALLOC( - NULL, sizeof(*a->head) + alloc_size); - + a->head = (Arena_Region*)ARENA_MALLOC(sizeof(*a->head) + alloc_size); if (a->head == NULL) { ARENA_ASSERT(false && "Reallocation failed"); return NULL; @@ -212,37 +253,34 @@ void *arena_alloc(Arena *a, size_t size) return a->head->data; } - // Not enough capacity - if (size > a->tail->capacity - a->tail->count) { - // Find first suitable region - while (a->tail->next != NULL - && size > a->tail->capacity - a->tail->count) { - a->tail = a->tail->next; - } - - // If not found create a new region - if (size > a->tail->capacity - a->tail->count) { - size_t alloc_size = - (size > region_capacity ? size : region_capacity); - - a->tail->next = (Arena_Region*)ARENA_REALLOC( - NULL, sizeof(*a->tail) + alloc_size); - - if (a->tail->next == NULL) { - ARENA_ASSERT(false && "Reallocation failed"); - return NULL; - } - - a->tail = a->tail->next; - a->tail->next = NULL; - a->tail->count = size; - a->tail->capacity = alloc_size; - return a->tail->data; - } + // Find first suitable region + Arena_Region *r = a->tail; + while (r != NULL && size > r->capacity - r->count) { + r = r->next; } - a->tail->count += size; - return a->tail->data + a->tail->count - size; + // If found allocate on it + if (r != NULL) { + if (r->count > SIZE_MAX - size) { + ARENA_ASSERT(false && "Region count overflow"); + return NULL; + } + r->count += size; + return r->data + r->count - size; + } + + // If not found append a new region + a->tail->next = (Arena_Region*)ARENA_MALLOC(sizeof(*a->tail) + alloc_size); + if (a->tail->next == NULL) { + ARENA_ASSERT(false && "Reallocation failed"); + return NULL; + } + + a->tail = a->tail->next; + a->tail->next = NULL; + a->tail->count = size; + a->tail->capacity = alloc_size; + return a->tail->data; } void *arena_copy(Arena *a, const void *src, size_t size) @@ -268,25 +306,6 @@ char *arena_strdup(Arena *a, const char *s) return (char*)arena_copy(a, s, strlen(s) + 1); } - -void arena_free(Arena *a) -{ - if (a == NULL) { - return; - } - - Arena_Region *cur = a->head; - while (cur != NULL) { - Arena_Region *next = cur->next; - ARENA_FREE(cur); - cur = next; - } - - a->head = NULL; - a->tail = NULL; - // a->region_capacity = 0; -} - void arena_reset(Arena *a) { if (a == NULL) { @@ -309,6 +328,7 @@ void arena_reset(Arena *a) /* * Revision history: * + * 1.1.1 (2025-11-18) Implement memory alignment; improve docs * 1.1.0 (2025-11-18) New helper functions: arena_copy(), arena_strdup() * 1.0.3 (2025-09-10) Renamed parameter bytes to size in arena_alloc() * 1.0.2 (2025-09-06) Bug fixes; new ARENA_NO_ASSERT macro diff --git a/test.c b/test.c index aac5676..8eaf989 100644 --- a/test.c +++ b/test.c @@ -2,8 +2,8 @@ #include #ifdef DEBUG -#define realloc(p, s) (printf("%s:%d:%s: realloc(%p, %lu)\n",\ - __FILE__, __LINE__, __func__, (p), (s)), realloc((p), (s))); +#define malloc(s) (printf("%s:%d:%s: malloc(%lu)\n",\ + __FILE__, __LINE__, __func__, (s)), malloc((s))); #define free(p) (printf("%s:%d:%s: free(%p)\n",\ __FILE__, __LINE__, __func__, (p)), free((p))); #endif // DEBUG @@ -27,32 +27,16 @@ int main(void) { Arena a = {0}; - printf("------ small alloc ------\n"); - + arena_alloc(&a, 4012); arena_alloc(&a, 100); - arena_alloc(&a, 100); - arena_alloc(&a, 100); - arena_alloc(&a, 100); - arena_print(a); - - printf("\n=========================================================\n\n"); - printf("------ big alloc ------\n"); - - arena_alloc(&a, 8000); - arena_print(a); - - printf("\n=========================================================\n\n"); - printf("------ reset ------\n"); + arena_alloc(&a, 2301); + arena_alloc(&a, 4015); + arena_alloc(&a, 10000); arena_reset(&a); - arena_print(a); - printf("\n=========================================================\n\n"); - printf("------ allocs after reset ------\n"); + arena_alloc(&a, 1000); - arena_alloc(&a, 300); - arena_alloc(&a, 400); - arena_alloc(&a, 9000); arena_print(a); arena_free(&a);