1.0.3 - Renamed parameter bytes to size in arena_alloc()

This commit is contained in:
seajee
2025-09-10 21:49:09 +02:00
parent 9657cfdb43
commit 38decaf81a

44
arena.h
View File

@@ -1,4 +1,4 @@
// arena.h - v1.0.2 - MIT License - https://github.com/seajee/arena.h // arena.h - v1.0.3 - MIT License - https://github.com/seajee/arena.h
// single header library for region-based memory management. // single header library for region-based memory management.
// //
// [License and changelog] // [License and changelog]
@@ -58,9 +58,9 @@
// configured by redefining it before including the header file. This function // configured by redefining it before including the header file. This function
// is not strictly necessary for initializing an arena (see example). // is not strictly necessary for initializing an arena (see example).
// //
// void *arena_alloc(Arena *a, size_t bytes) // void *arena_alloc(Arena *a, size_t size)
// //
// This function allocates a buffer of size <bytes> into the specified arena // This function allocates a buffer of <size> bytes into the specified arena
// and returns it's pointer. // and returns it's pointer.
// //
// void arena_free(Arena *a) // void arena_free(Arena *a)
@@ -145,7 +145,7 @@ typedef struct Arena {
} Arena; } Arena;
Arena arena_create(size_t region_capacity); Arena arena_create(size_t region_capacity);
void *arena_alloc(Arena *a, size_t bytes); void *arena_alloc(Arena *a, size_t size);
void arena_free(Arena *a); void arena_free(Arena *a);
void arena_reset(Arena *a); void arena_reset(Arena *a);
@@ -168,9 +168,9 @@ Arena arena_create(size_t region_capacity)
return a; return a;
} }
void *arena_alloc(Arena *a, size_t bytes) void *arena_alloc(Arena *a, size_t size)
{ {
if (a == NULL || bytes == 0) { if (a == NULL || size == 0) {
ARENA_ASSERT(!"Invalid, parameters"); ARENA_ASSERT(!"Invalid, parameters");
return NULL; return NULL;
} }
@@ -180,8 +180,9 @@ void *arena_alloc(Arena *a, size_t bytes)
// Empty arena // Empty arena
if (a->head == NULL) { if (a->head == NULL) {
size_t size = (bytes > region_capacity ? bytes : region_capacity); size_t alloc_size = (size > region_capacity ? size : region_capacity);
a->head = (Arena_Region*)ARENA_REALLOC(NULL, sizeof(*a->head) + size); a->head = (Arena_Region*)ARENA_REALLOC(
NULL, sizeof(*a->head) + alloc_size);
if (a->head == NULL) { if (a->head == NULL) {
ARENA_ASSERT(!"Reallocation failed"); ARENA_ASSERT(!"Reallocation failed");
@@ -189,23 +190,27 @@ void *arena_alloc(Arena *a, size_t bytes)
} }
a->head->next = NULL; a->head->next = NULL;
a->head->count = bytes; a->head->count = size;
a->head->capacity = size; a->head->capacity = alloc_size;
a->tail = a->head; a->tail = a->head;
return a->head->data; return a->head->data;
} }
// Not enough capacity // Not enough capacity
if (bytes > a->tail->capacity - a->tail->count) { if (size > a->tail->capacity - a->tail->count) {
// Find first suitable region // Find first suitable region
while (a->tail->next != NULL && bytes > a->tail->capacity - a->tail->count) { while (a->tail->next != NULL
&& size > a->tail->capacity - a->tail->count) {
a->tail = a->tail->next; a->tail = a->tail->next;
} }
// If not found create a new region // If not found create a new region
if (bytes > a->tail->capacity - a->tail->count) { if (size > a->tail->capacity - a->tail->count) {
size_t size = (bytes > region_capacity ? bytes : region_capacity); size_t alloc_size =
a->tail->next = (Arena_Region*)ARENA_REALLOC(NULL, sizeof(*a->tail) + 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) { if (a->tail->next == NULL) {
ARENA_ASSERT(!"Reallocation failed"); ARENA_ASSERT(!"Reallocation failed");
@@ -214,14 +219,14 @@ void *arena_alloc(Arena *a, size_t bytes)
a->tail = a->tail->next; a->tail = a->tail->next;
a->tail->next = NULL; a->tail->next = NULL;
a->tail->count = bytes; a->tail->count = size;
a->tail->capacity = size; a->tail->capacity = alloc_size;
return a->tail->data; return a->tail->data;
} }
} }
a->tail->count += bytes; a->tail->count += size;
return a->tail->data + a->tail->count - bytes; return a->tail->data + a->tail->count - size;
} }
void arena_free(Arena *a) void arena_free(Arena *a)
@@ -264,6 +269,7 @@ void arena_reset(Arena *a)
/* /*
* Revision history: * Revision history:
* *
* 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
* 1.0.1 (2025-08-02) Prevent name mangling of functions; don't reset * 1.0.1 (2025-08-02) Prevent name mangling of functions; don't reset
* region_capacity in arena_free() * region_capacity in arena_free()