147 if (required_size == 0)
158 size_t old_size = arena->size;
167 if (new_size > (
size_t)(1ULL << 32))
393 const size_t used = arena->offset;
394 const size_t size = arena->size;
443 bool owns = atomic_load_explicit(&arena->owns_buffer, memory_order_acquire);
444 bool grow = atomic_load_explicit(&arena->can_grow, memory_order_acquire);
475 size_t* temp = realloc(arena->stats.growth_history,
sizeof(
size_t) * (arena->stats.growth_history_count + 1));
478 arena->stats.growth_history = temp;
479 arena->stats.growth_history[arena->stats.growth_history_count++] = old_size;
518 if (required_size > SIZE_MAX - arena->offset)
556 size_t requested = arena->offset + required_size;
557 size_t new_size = cb(arena->size, required_size);
559 if (new_size < requested)
561 if (new_size > SIZE_MAX)
596 void* new_buf = realloc(arena->buffer, new_size);
600 arena->buffer = (uint8_t*) new_buf;
601 arena->size = new_size;
602 arena->stats.reallocations++;
606 ALOG(
"[arena_grow] Arena %p grown from %zu to %zu bytes\n", (
void*) arena, old_size, new_size);
645 bool owns = atomic_load_explicit(&arena->owns_buffer, memory_order_acquire);
646 bool grow = atomic_load_explicit(&arena->can_grow, memory_order_acquire);
651 if (new_size < arena->offset)
654 if (new_size == arena->offset)
657 double shrink_ratio = (double) new_size / (
double) arena->size;
722 void* new_buf = realloc(arena->buffer, new_size);
726 arena->buffer = (uint8_t*) new_buf;
727 arena->size = new_size;
728 arena->stats.shrinks++;
730 ALOG(
"[arena_shrink] Arena %p shrunk to %zu bytes\n", (
void*) arena, new_size);
760 return atomic_load_explicit(&arena->can_grow, memory_order_acquire);
793 double ratio = (double)
used / (
double) size;
Core public API for the Arena memory allocator.
size_t(* arena_grow_callback)(size_t current_size, size_t requested_size)
#define ARENA_SHRINK_PADDING
Extra padding when computing shrink sizes.
#define ARENA_MIN_SHRINK_RATIO
Shrink threshold: offset < ratio * size.
#define ALOG(...)
Debug logging macro (enabled only if ARENA_DEBUG_LOG is set).
#define ARENA_CHECK(arena)
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
size_t used
Number of bytes used in the arena buffer.
bool arena_grow(t_arena *arena, size_t required_size)
bool arena_might_shrink(t_arena *arena)
void arena_shrink(t_arena *arena, size_t new_size)
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
size_t default_grow_cb(size_t current_size, size_t requested_size)
Default growth policy callback for arena resizing.
static size_t arena_shrink_target(size_t used)
Compute the target buffer size for a shrink operation.
static bool arena_should_grow(const t_arena *arena)
Check whether the arena is allowed to grow.
static bool arena_grow_realloc_buffer(t_arena *arena, size_t new_size, size_t old_size)
Reallocate the arena's buffer to a new size.
static bool arena_shrink_validate(t_arena *arena, size_t new_size)
Validate whether an arena can be safely shrunk to a new size.
static bool arena_grow_validate(t_arena *arena, size_t required_size)
Validate whether the arena is allowed to grow for a given request.
static bool arena_shrink_apply(t_arena *arena, size_t new_size)
Apply the memory shrink operation to an arena's buffer.
static bool should_attempt_shrink(t_arena *arena)
Determine whether the arena is eligible for dynamic shrinking.
static bool arena_can_shrink(t_arena *arena, size_t new_size)
Determine whether the arena is eligible for shrinking to a smaller size.
static size_t arena_grow_compute_new_size(t_arena *arena, size_t required_size)
Compute the new size for the arena based on the growth policy.
static void arena_record_growth(t_arena *arena, size_t old_size)
Record the previous size of the arena in the growth history.
static bool arena_should_maybe_shrink(size_t used, size_t size)
Decide whether the arena buffer should be shrunk based on usage ratio.
The main memory arena structure used for fast allocation.