80 size_t new_size = current_size > 0 ? current_size : 64;
82 if (requested_size > SIZE_MAX - current_size)
85 const size_t needed = current_size + requested_size;
87 while (new_size < needed && new_size <= SIZE_MAX / 2)
90 if (new_size < needed)
121 if (arena->offset > arena->stats.peak_usage)
122 arena->stats.peak_usage = arena->offset;
156 if (arena->size == 0 || arena->offset > arena->size)
243 arena->buffer = NULL;
247 atomic_store_explicit(&arena->owns_buffer,
false, memory_order_release);
248 atomic_store_explicit(&arena->can_grow,
false, memory_order_release);
250 arena->grow_cb = NULL;
251 arena->parent_ref = NULL;
256 arena->debug.label = NULL;
257 arena->debug.error_cb = NULL;
258 arena->debug.error_context = NULL;
259 arena->debug.subarena_counter = 0;
261 arena->hooks.hook_cb = NULL;
262 arena->hooks.context = NULL;
Core public API for the Arena memory allocator.
#define ARENA_ID_LEN
Max characters for arena ID strings.
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
void arena_zero_metadata(t_arena *arena)
Zero out and reset all debug/stats metadata associated with the arena.
bool arena_is_valid(const t_arena *arena)
Check whether the given arena is in a valid, initialized state.
size_t default_grow_cb(size_t current_size, size_t requested_size)
Default growth policy callback for arena resizing.
void arena_update_peak(t_arena *arena)
Update the arena’s peak memory usage if the current usage exceeds it.
size_t last_alloc_size
Size of the last allocation.
size_t shrinks
Number of times the arena was shrunk.
size_t peak_usage
Highest offset reached (i.e., peak memory usage)
size_t reallocations
Total number of reallocations (via arena_realloc_last)
size_t growth_history_count
Number of growth events recorded.
size_t failed_allocations
Number of failed allocation attempts.
size_t wasted_alignment_bytes
Total bytes wasted due to alignment padding.
size_t last_alloc_id
Unique ID of the last allocation.
size_t bytes_allocated
Cumulative bytes allocated.
size_t * growth_history
Array of size values recorded during arena growth.
size_t alloc_id_counter
Total allocation ID counter (used for tracking)
size_t allocations
Total number of successful allocations.
size_t live_allocations
Number of active allocations (not yet released)
size_t last_alloc_offset
Offset of the last allocation.
void arena_stats_reset(t_arena_stats *stats)
Reset the given statistics structure to zero.
Structure holding runtime statistics for an arena.
The main memory arena structure used for fast allocation.