From e91dbb6a65bf94f44cabeb1ff3ec9997b130f38e Mon Sep 17 00:00:00 2001 From: Cristian Gora Date: Wed, 8 Apr 2026 21:22:14 +0200 Subject: [PATCH] Complete v1.2.0 release --- arena.h | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/arena.h b/arena.h index 908a27f..01a910a 100644 --- a/arena.h +++ b/arena.h @@ -78,6 +78,13 @@ // Warning: this functions may cause fragmentation, consider setting an // 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) // // This function allocates a buffer of bytes into the specified arena @@ -197,6 +204,7 @@ typedef struct Arena { Arena arena_create(size_t region_capacity); void arena_free(Arena *a); void arena_reset(Arena *a); +void arena_trim(Arena *a); // Allocation and other utilities void *arena_alloc(Arena *a, size_t size); @@ -246,7 +254,6 @@ void arena_free(Arena *a) a->head = NULL; a->tail = NULL; - // a->region_capacity = 0; } void arena_reset(Arena *a) @@ -262,6 +269,21 @@ void arena_reset(Arena *a) 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) { if (a == NULL || size == 0) { @@ -469,8 +491,8 @@ size_t arena__align(size_t n) /* * Revision history: * - * 1.2.0 (2026-04-08) New helper functions: arena_sprintf(), - * arena_snapshot() and arena_rewind() + * 1.2.0 (2026-04-08) New functions: arena_sprintf(), arena_snapshot(), + * arena_rewind() and arena_trim() * 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