memory_arena
Loading...
Searching...
No Matches
arena_internal.c
Go to the documentation of this file.
1
30#include "arena.h"
31#include <stdbool.h>
32
78size_t default_grow_cb(size_t current_size, size_t requested_size)
79{
80 size_t new_size = current_size > 0 ? current_size : 64;
81
82 if (requested_size > SIZE_MAX - current_size)
83 return SIZE_MAX;
84
85 const size_t needed = current_size + requested_size;
86
87 while (new_size < needed && new_size <= SIZE_MAX / 2)
88 new_size *= 2;
89
90 if (new_size < needed)
91 new_size = needed;
92
93 return new_size;
94}
95
119{
120 ARENA_LOCK(arena);
121 if (arena->offset > arena->stats.peak_usage)
122 arena->stats.peak_usage = arena->offset;
123 ARENA_UNLOCK(arena);
124}
125
150bool arena_is_valid(const t_arena* arena)
151{
152 if (!arena)
153 return false;
154 if (!arena->buffer)
155 return false;
156 if (arena->size == 0 || arena->offset > arena->size)
157 return false;
158 return true;
159}
160
188{
189 if (!stats)
190 return;
191
192 stats->allocations = 0;
193 stats->reallocations = 0;
194 stats->failed_allocations = 0;
195 stats->live_allocations = 0;
196 stats->bytes_allocated = 0;
197 stats->wasted_alignment_bytes = 0;
198 stats->shrinks = 0;
199 stats->peak_usage = 0;
200 stats->last_alloc_size = 0;
201 stats->last_alloc_offset = 0;
202 stats->last_alloc_id = (size_t) -1;
203 stats->alloc_id_counter = 0;
204 stats->growth_history = NULL;
205 stats->growth_history_count = 0;
206}
207
242{
243 arena->buffer = NULL;
244 arena->size = 0;
245 arena->offset = 0;
246
247 atomic_store_explicit(&arena->owns_buffer, false, memory_order_release);
248 atomic_store_explicit(&arena->can_grow, false, memory_order_release);
249
250 arena->grow_cb = NULL;
251 arena->parent_ref = NULL;
252
253 arena_stats_reset(&arena->stats);
254
255 memset(arena->debug.id, 0, ARENA_ID_LEN);
256 arena->debug.label = NULL;
257 arena->debug.error_cb = NULL;
258 arena->debug.error_context = NULL;
259 arena->debug.subarena_counter = 0;
260
261 arena->hooks.hook_cb = NULL;
262 arena->hooks.context = NULL;
263}
Core public API for the Arena memory allocator.
#define ARENA_ID_LEN
Max characters for arena ID strings.
Definition arena_config.h:6
#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.
Definition arena_stats.h:64
size_t shrinks
Number of times the arena was shrunk.
Definition arena_stats.h:60
size_t peak_usage
Highest offset reached (i.e., peak memory usage)
Definition arena_stats.h:58
size_t reallocations
Total number of reallocations (via arena_realloc_last)
Definition arena_stats.h:56
size_t growth_history_count
Number of growth events recorded.
Definition arena_stats.h:62
size_t failed_allocations
Number of failed allocation attempts.
Definition arena_stats.h:68
size_t wasted_alignment_bytes
Total bytes wasted due to alignment padding.
Definition arena_stats.h:59
size_t last_alloc_id
Unique ID of the last allocation.
Definition arena_stats.h:66
size_t bytes_allocated
Cumulative bytes allocated.
Definition arena_stats.h:57
size_t * growth_history
Array of size values recorded during arena growth.
Definition arena_stats.h:61
size_t alloc_id_counter
Total allocation ID counter (used for tracking)
Definition arena_stats.h:67
size_t allocations
Total number of successful allocations.
Definition arena_stats.h:55
size_t live_allocations
Number of active allocations (not yet released)
Definition arena_stats.h:63
size_t last_alloc_offset
Offset of the last allocation.
Definition arena_stats.h:65
void arena_stats_reset(t_arena_stats *stats)
Reset the given statistics structure to zero.
Structure holding runtime statistics for an arena.
Definition arena_stats.h:54
The main memory arena structure used for fast allocation.