Complete v1.2.0 release

This commit is contained in:
2026-04-08 21:22:14 +02:00
parent d84e7bf067
commit e91dbb6a65

28
arena.h
View File

@@ -78,6 +78,13 @@
// Warning: this functions may cause fragmentation, consider setting an // Warning: this functions may cause fragmentation, consider setting an
// appropriate region capacity. // appropriate region capacity.
// //
// void arena_trim(Arena *a)
//
// This functions frees all of the regions where no buffers are allocated.
// This is useful after an arena_rewind() was performed to preserve memory.
// Note: this function always keeps the first region allocated to preserve an
// ideal behaviour.
//
// void *arena_alloc(Arena *a, size_t size) // 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
@@ -197,6 +204,7 @@ typedef struct Arena {
Arena arena_create(size_t region_capacity); Arena arena_create(size_t region_capacity);
void arena_free(Arena *a); void arena_free(Arena *a);
void arena_reset(Arena *a); void arena_reset(Arena *a);
void arena_trim(Arena *a);
// Allocation and other utilities // Allocation and other utilities
void *arena_alloc(Arena *a, size_t size); void *arena_alloc(Arena *a, size_t size);
@@ -246,7 +254,6 @@ void arena_free(Arena *a)
a->head = NULL; a->head = NULL;
a->tail = NULL; a->tail = NULL;
// a->region_capacity = 0;
} }
void arena_reset(Arena *a) void arena_reset(Arena *a)
@@ -262,6 +269,21 @@ void arena_reset(Arena *a)
a->tail = a->head; a->tail = a->head;
} }
void arena_trim(Arena *a)
{
if (a == NULL) {
return;
}
Arena_Region *to_free = a->tail->next;
a->tail->next = NULL;
while (to_free != NULL) {
Arena_Region *next = to_free->next;
ARENA_FREE(to_free);
to_free = next;
}
}
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) {
@@ -469,8 +491,8 @@ size_t arena__align(size_t n)
/* /*
* Revision history: * Revision history:
* *
* 1.2.0 (2026-04-08) New helper functions: arena_sprintf(), * 1.2.0 (2026-04-08) New functions: arena_sprintf(), arena_snapshot(),
* arena_snapshot() and arena_rewind() * arena_rewind() and arena_trim()
* 1.1.3 (2026-01-14) Align Arena_Region data flexible array member * 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.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.1 (2025-11-18) Implement memory alignment; improve docs