Compare commits

..

6 Commits

Author SHA1 Message Date
seajee
c5a38cc501 v1.1.3 - Align Arena_Region data flexible array member 2026-01-14 18:11:15 +01:00
seajee
1ffc6e1402 Include stdalign.h 2026-01-10 17:45:01 +01:00
seajee
2d465afc7f v1.1.2 Minor changes; check result of alloc in arena_copy() 2026-01-10 17:42:14 +01:00
seajee
1b7e73d3cb v1.1.1 Implement memory alignment; improve docs 2025-11-18 21:27:33 +01:00
seajee
1956855adf v1.1.0 - New helper functions: arena_copy(), arena_strdup() 2025-11-18 18:22:02 +01:00
seajee
bbf3371038 Update docs and assertions 2025-11-17 02:49:43 +01:00
2 changed files with 177 additions and 113 deletions

250
arena.h
View File

@@ -1,4 +1,4 @@
// arena.h - v1.0.3 - MIT License - https://github.com/seajee/arena.h
// arena.h - v1.1.3 - MIT License
// single header library for region-based memory management.
//
// [License and changelog]
@@ -21,29 +21,33 @@
// Note: every compile-time option listed here should be configured before
// the #include "arena.h"
//
// #define ARENA_NO_ASSERT
//
// This macro disables assertions that happen when a reallocation
// fails. The disabled assertions will be replaced with a simple
// condition that returns NULL when it fails. Note that ARENA_ASSERT
// is only used in arena_alloc().
//
// #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.
// The disabled assertions will be replaced with a simple condition
// that returns NULL when it fails. This macro affects every function
// that returns a pointer.
//
// #define ARENA_ASSERT my_assert
//
// This macro defines an alternative function for assertions. In this
// library, ARENA_ASSERT is only used when arena_alloc() fails. Will
// be overwritten if ARENA_NO_ASSERT is defined.
// 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]
//
@@ -53,15 +57,9 @@
//
// Arena arena_create(size_t region_capacity)
//
// This function initializes an arena with a specified region capacity. By
// default the region capacity is ARENA_REGION_CAPACITY, which can be
// configured by redefining it before including the header file. This function
// is not strictly necessary for initializing an arena (see example).
//
// void *arena_alloc(Arena *a, size_t size)
//
// This function allocates a buffer of <size> bytes into the specified arena
// and returns it's pointer.
// This function is optional (see [Example] section): initializes an arena
// with a specified region capacity. It will override the default
// ARENA_REGION_CAPACITY.
//
// void arena_free(Arena *a)
//
@@ -77,6 +75,25 @@
// Warning: this functions may cause fragmentation, consider setting an
// appropriate region capacity.
//
// void *arena_alloc(Arena *a, size_t size)
//
// This function allocates a buffer of <size> bytes into the specified arena
// and returns it's pointer.
//
// void *arena_copy(Arena *a, const void *src, size_t size);
//
// This function allocates a buffer of <size> bytes into the arena and copies
// the contents of <src>.
//
// char *arena_strdup(Arena *a, const char *s);
//
// This functions acts like the strdup() function but it instead allocates on
// the specified arena. It duplicates a string in the arena.
//
// [Notes]
//
// This library is not Thread-safe.
//
// [Example]
//
#if 0
@@ -99,13 +116,19 @@ int main(void)
#ifndef ARENA_H_
#define ARENA_H_
#include <stdalign.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#ifndef ARENA_REGION_CAPACITY
# define ARENA_REGION_CAPACITY (8*1024)
#endif // ARENA_REGION_CAPACITY
#ifndef ARENA_ALIGNMENT
# define ARENA_ALIGNMENT alignof(max_align_t)
#endif // ARENA_ALIGNMENT
#ifndef ARENA_NO_ASSERT
# ifndef ARENA_ASSERT
# include <assert.h>
@@ -115,10 +138,10 @@ int main(void)
# define ARENA_ASSERT(...) ((void)0)
#endif // ARENA_NO_ASSERT
#ifndef ARENA_REALLOC
#ifndef ARENA_MALLOC
# include <stdlib.h>
# define ARENA_REALLOC realloc
#endif // ARENA_REALLOC
# define ARENA_MALLOC malloc
#endif // ARENA_MALLOC
#ifndef ARENA_FREE
# include <stdlib.h>
@@ -135,7 +158,7 @@ struct Arena_Region {
Arena_Region *next;
size_t count;
size_t capacity;
uint8_t data[];
alignas(ARENA_ALIGNMENT) uint8_t data[];
};
typedef struct Arena {
@@ -144,11 +167,16 @@ typedef struct Arena {
size_t region_capacity;
} Arena;
// Arena management
Arena arena_create(size_t region_capacity);
void *arena_alloc(Arena *a, size_t size);
void arena_free(Arena *a);
void arena_reset(Arena *a);
// Allocation other utilities
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);
#ifdef __cplusplus
}
#endif // __cplusplus
@@ -163,72 +191,12 @@ extern "C" { // Prevent name mangling of functions
Arena arena_create(size_t region_capacity)
{
Arena a = {0};
Arena a;
memset(&a, 0, sizeof(a));
a.region_capacity = region_capacity;
return a;
}
void *arena_alloc(Arena *a, size_t size)
{
if (a == NULL || size == 0) {
ARENA_ASSERT(!"Invalid, parameters");
return NULL;
}
size_t region_capacity = (a->region_capacity == 0
? ARENA_REGION_CAPACITY : a->region_capacity);
// Empty arena
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);
if (a->head == NULL) {
ARENA_ASSERT(!"Reallocation failed");
return NULL;
}
a->head->next = NULL;
a->head->count = size;
a->head->capacity = alloc_size;
a->tail = a->head;
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(!"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;
return a->tail->data + a->tail->count - size;
}
void arena_free(Arena *a)
{
if (a == NULL) {
@@ -260,6 +228,104 @@ void arena_reset(Arena *a)
a->tail = a->head;
}
void *arena_alloc(Arena *a, size_t size)
{
if (a == NULL || size == 0) {
ARENA_ASSERT(false && "Invalid parameters");
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);
// 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) {
a->head = (Arena_Region*)ARENA_MALLOC(sizeof(*a->head) + alloc_size);
if (a->head == NULL) {
ARENA_ASSERT(false && "Allocation failed");
return NULL;
}
a->head->next = NULL;
a->head->count = size;
a->head->capacity = alloc_size;
a->tail = a->head;
return a->head->data;
}
// Find first suitable region
Arena_Region *r = a->tail;
while (r != NULL && size > r->capacity - r->count) {
r = r->next;
}
// 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 && "Allocation 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)
{
if (a == NULL || src == NULL) {
ARENA_ASSERT(false && "Invalid parameters");
return NULL;
}
void *copy = arena_alloc(a, size);
if (copy == NULL) {
return NULL;
}
memcpy(copy, src, size);
return copy;
}
char *arena_strdup(Arena *a, const char *s)
{
if (a == NULL || s == NULL) {
ARENA_ASSERT(false && "Invalid parameters");
return NULL;
}
return (char*)arena_copy(a, s, strlen(s) + 1);
}
#ifdef __cplusplus
}
#endif // __cplusplus
@@ -269,6 +335,10 @@ void arena_reset(Arena *a)
/*
* Revision history:
*
* 1.1.3 (2026-01-14) Align Arena_Region data flexible array member
* 1.1.2 (2026-01-10) Minor changes; check result of alloc in arena_copy()
* 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
* 1.0.1 (2025-08-02) Prevent name mangling of functions; don't reset

40
test.c
View File

@@ -2,8 +2,8 @@
#include <stdlib.h>
#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
@@ -25,34 +25,28 @@ void arena_print(Arena arena)
int main(void)
{
printf("struct Arena_Region {\n");
printf(" Arena_Region *next; (%zu)\n", offsetof(struct Arena_Region, next));
printf(" size_t count; (%zu)\n", offsetof(struct Arena_Region, count));
printf(" size_t capacity; (%zu)\n", offsetof(struct Arena_Region, capacity));
printf(" uint8_t data[]; (%zu)\n", offsetof(struct Arena_Region, data));
printf("};\n");
printf("Alignment: %zu\n", ARENA_ALIGNMENT);
printf("================================================================\n");
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);