v1.1.1 Implement memory alignment; improve docs

This commit is contained in:
seajee
2025-11-18 21:27:33 +01:00
parent 1956855adf
commit 1b7e73d3cb
2 changed files with 94 additions and 90 deletions

152
arena.h
View File

@@ -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. // single header library for region-based memory management.
// //
// [License and changelog] // [License and changelog]
@@ -21,6 +21,14 @@
// Note: every compile-time option listed here should be configured before // Note: every compile-time option listed here should be configured before
// the #include "arena.h" // 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 // #define ARENA_NO_ASSERT
// //
// This macro disables assertions that happen when allocations fail. // 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 NULL when it fails. This macro affects every function
// that returns a pointer. // 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 // #define ARENA_ASSERT my_assert
// //
// This macro defines an alternative function for assertions. This has // This macro defines an alternative function for assertions. This has
// no effect when ARENA_NO_ASSERT is defined. // no effect when ARENA_NO_ASSERT is defined.
// //
// #define ARENA_REALLOC my_realloc // #define ARENA_MALLOC my_malloc
// #define ARENA_FREE my_free // #define ARENA_FREE my_free
// //
// These macros define alternative functions for dynamic allocation // These macros define alternative functions for dynamic allocation
// and deallocation on the heap. They are only used for managing // and deallocation. They are only used for managing Arena_Region
// Arena_Region structures. // structures. If redefined ensure that your functions consider memory
// alignment.
// //
// [Function documentation] // [Function documentation]
// //
@@ -119,6 +124,10 @@ int main(void)
# define ARENA_REGION_CAPACITY (8*1024) # define ARENA_REGION_CAPACITY (8*1024)
#endif // ARENA_REGION_CAPACITY #endif // ARENA_REGION_CAPACITY
#ifndef ARENA_ALIGNMENT
# define ARENA_ALIGNMENT sizeof(max_align_t)
#endif // ARENA_ALIGNMENT
#ifndef ARENA_NO_ASSERT #ifndef ARENA_NO_ASSERT
# ifndef ARENA_ASSERT # ifndef ARENA_ASSERT
# include <assert.h> # include <assert.h>
@@ -128,10 +137,10 @@ int main(void)
# define ARENA_ASSERT(...) ((void)0) # define ARENA_ASSERT(...) ((void)0)
#endif // ARENA_NO_ASSERT #endif // ARENA_NO_ASSERT
#ifndef ARENA_REALLOC #ifndef ARENA_MALLOC
# include <stdlib.h> # include <stdlib.h>
# define ARENA_REALLOC realloc # define ARENA_MALLOC malloc
#endif // ARENA_REALLOC #endif // ARENA_MALLOC
#ifndef ARENA_FREE #ifndef ARENA_FREE
# include <stdlib.h> # include <stdlib.h>
@@ -158,10 +167,11 @@ typedef struct Arena {
} Arena; } Arena;
Arena arena_create(size_t region_capacity); Arena arena_create(size_t region_capacity);
void arena_free(Arena *a);
void *arena_alloc(Arena *a, size_t size); void *arena_alloc(Arena *a, size_t size);
void *arena_copy(Arena *a, const void *src, size_t size); void *arena_copy(Arena *a, const void *src, size_t size);
char *arena_strdup(Arena *a, const char *s); char *arena_strdup(Arena *a, const char *s);
void arena_free(Arena *a);
void arena_reset(Arena *a); void arena_reset(Arena *a);
#ifdef __cplusplus #ifdef __cplusplus
@@ -184,6 +194,24 @@ Arena arena_create(size_t region_capacity)
return a; 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) void *arena_alloc(Arena *a, size_t size)
{ {
if (a == NULL || size == 0) { if (a == NULL || size == 0) {
@@ -191,15 +219,28 @@ void *arena_alloc(Arena *a, size_t size)
return NULL; 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 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) { if (a->head == NULL) {
size_t alloc_size = (size > region_capacity ? size : region_capacity); a->head = (Arena_Region*)ARENA_MALLOC(sizeof(*a->head) + alloc_size);
a->head = (Arena_Region*)ARENA_REALLOC(
NULL, sizeof(*a->head) + alloc_size);
if (a->head == NULL) { if (a->head == NULL) {
ARENA_ASSERT(false && "Reallocation failed"); ARENA_ASSERT(false && "Reallocation failed");
return NULL; return NULL;
@@ -212,37 +253,34 @@ void *arena_alloc(Arena *a, size_t size)
return a->head->data; return a->head->data;
} }
// Not enough capacity // Find first suitable region
if (size > a->tail->capacity - a->tail->count) { Arena_Region *r = a->tail;
// Find first suitable region while (r != NULL && size > r->capacity - r->count) {
while (a->tail->next != NULL r = r->next;
&& 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;
}
} }
a->tail->count += size; // If found allocate on it
return a->tail->data + a->tail->count - size; 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) 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); 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) void arena_reset(Arena *a)
{ {
if (a == NULL) { if (a == NULL) {
@@ -309,6 +328,7 @@ void arena_reset(Arena *a)
/* /*
* Revision history: * 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.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.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 * 1.0.2 (2025-09-06) Bug fixes; new ARENA_NO_ASSERT macro

30
test.c
View File

@@ -2,8 +2,8 @@
#include <stdlib.h> #include <stdlib.h>
#ifdef DEBUG #ifdef DEBUG
#define realloc(p, s) (printf("%s:%d:%s: realloc(%p, %lu)\n",\ #define malloc(s) (printf("%s:%d:%s: malloc(%lu)\n",\
__FILE__, __LINE__, __func__, (p), (s)), realloc((p), (s))); __FILE__, __LINE__, __func__, (s)), malloc((s)));
#define free(p) (printf("%s:%d:%s: free(%p)\n",\ #define free(p) (printf("%s:%d:%s: free(%p)\n",\
__FILE__, __LINE__, __func__, (p)), free((p))); __FILE__, __LINE__, __func__, (p)), free((p)));
#endif // DEBUG #endif // DEBUG
@@ -27,32 +27,16 @@ int main(void)
{ {
Arena a = {0}; 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, 2301);
arena_alloc(&a, 100); arena_alloc(&a, 4015);
arena_alloc(&a, 100); arena_alloc(&a, 10000);
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_reset(&a); arena_reset(&a);
arena_print(a);
printf("\n=========================================================\n\n"); arena_alloc(&a, 1000);
printf("------ allocs after reset ------\n");
arena_alloc(&a, 300);
arena_alloc(&a, 400);
arena_alloc(&a, 9000);
arena_print(a); arena_print(a);
arena_free(&a); arena_free(&a);