50 size_t* aligned_offset,
size_t* wasted);
220 label =
"arena_alloc_labeled";
277 label =
"arena_alloc_aligned_labeled";
323 if (alignment == 0 || (alignment & (alignment - 1)) != 0)
325 arena_report_error(arena,
"%s failed: alignment (%zu) is not a power-of-two", label, alignment);
359 if (atomic_load_explicit(&arena->is_destroying, memory_order_acquire))
390 if (size > SIZE_MAX - arena->offset)
392 arena->stats.failed_allocations++;
424 size_t curr = (size_t) (arena->buffer + arena->offset);
425 size_t aligned =
align_up(curr, alignment);
426 return aligned - (size_t) arena->buffer;
456 if (!arena->can_grow)
504 size_t* aligned_offset,
size_t* wasted)
507 *wasted = *aligned_offset - arena->offset;
509 if (*aligned_offset + size <= arena->size)
516 *wasted = *aligned_offset - arena->offset;
518 return *aligned_offset + size <= arena->size;
557 arena->stats.allocations++;
558 arena->stats.live_allocations++;
559 arena->stats.bytes_allocated += size;
560 arena->stats.wasted_alignment_bytes += wasted;
561 arena->stats.alloc_id_counter++;
562 arena->stats.last_alloc_size = size;
563 arena->stats.last_alloc_offset = arena->offset - size;
595 arena->offset = aligned_offset + size;
630 if (label && strcmp(label,
"arena_calloc_zero") == 0)
631 memset(ptr, 0, size);
677 hook(arena, (
int) arena->stats.alloc_id_counter, ptr, size, offset, wasted, label);
741 size_t aligned_offset = 0;
745 arena->stats.failed_allocations++;
752 void* result = arena->buffer + aligned_offset;
756 ALOG(
"[arena] %s: Allocated %zu bytes @ offset %zu (arena %p)\n", label, size, aligned_offset, (
void*) arena);
Core public API for the Arena memory allocator.
bool arena_grow(t_arena *arena, size_t required_size)
void * arena_alloc(t_arena *arena, size_t size)
void * arena_alloc_aligned_labeled(t_arena *arena, size_t size, size_t alignment, const char *label)
void * arena_alloc_aligned(t_arena *arena, size_t size, size_t alignment)
void * arena_alloc_labeled(t_arena *arena, size_t size, const char *label)
#define ARENA_DEFAULT_ALIGNMENT
Default alignment value supported.
#define arena_poison_memory(ptr, size)
No-op poison macro when poisoning is disabled.
#define ALOG(...)
Debug logging macro (enabled only if ARENA_DEBUG_LOG is set).
#define ARENA_CHECK(arena)
void(* arena_allocation_hook)(t_arena *arena, int alloc_id, void *ptr, size_t size, size_t offset, size_t wasted, const char *label)
Allocation hook callback type.
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
void * arena_alloc_internal(t_arena *arena, size_t size, size_t alignment, const char *label)
Core internal allocator with alignment and labeling support.
static size_t arena_calc_aligned_offset(t_arena *arena, size_t alignment)
Compute the next aligned offset within the arena.
static bool arena_alloc_validate_input(t_arena *arena, size_t size, size_t alignment, const char *label)
Validate input parameters for arena allocation.
static bool arena_is_being_destroyed(t_arena *arena, const char *label)
Check whether the arena is currently being destroyed.
static bool arena_check_overflow(t_arena *arena, size_t size, const char *label)
Check whether an allocation would cause a size_t overflow.
static void arena_invoke_allocation_hook(t_arena *arena, void *ptr, size_t size, size_t offset, size_t wasted, const char *label)
Invoke the user-defined allocation hook, if registered.
static bool arena_try_grow(t_arena *arena, size_t size, const char *label)
Attempt to grow the arena's buffer to satisfy an upcoming allocation.
static void arena_commit_allocation(t_arena *arena, size_t size, size_t wasted, size_t aligned_offset)
Update offset, peak usage, and statistics.
static void arena_update_stats(t_arena *arena, size_t size, size_t wasted)
Update allocation statistics after a successful arena allocation.
static void arena_zero_if_needed(void *ptr, size_t size, const char *label)
Zero or poison a memory region depending on allocation label.
static bool arena_ensure_capacity(t_arena *arena, size_t size, size_t alignment, const char *label, size_t *aligned_offset, size_t *wasted)
Ensure that the arena has enough space for a new allocation with alignment.
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
static size_t align_up(size_t value, size_t alignment)
Align a value upwards to the nearest multiple of alignment.
void arena_update_peak(t_arena *arena)
Update the arena’s peak memory usage if the current usage exceeds it.
The main memory arena structure used for fast allocation.