memory_arena
Loading...
Searching...
No Matches
Functions
arena_resize.c File Reference

Arena memory resizing logic — dynamic growth and shrinking. More...

#include "arena.h"
#include <math.h>
+ Include dependency graph for arena_resize.c:

Go to the source code of this file.

Functions

static bool arena_can_shrink (t_arena *arena, size_t new_size)
 Determine whether the arena is eligible for shrinking to a smaller size.
 
bool arena_grow (t_arena *arena, size_t required_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 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_grow_validate (t_arena *arena, size_t required_size)
 Validate whether the arena is allowed to grow for a given request.
 
bool arena_might_shrink (t_arena *arena)
 
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_grow (const t_arena *arena)
 Check whether the arena is allowed to grow.
 
static bool arena_should_maybe_shrink (size_t used, size_t size)
 Decide whether the arena buffer should be shrunk based on usage ratio.
 
void arena_shrink (t_arena *arena, size_t new_size)
 
static bool arena_shrink_apply (t_arena *arena, size_t new_size)
 Apply the memory shrink operation to an arena's buffer.
 
static size_t arena_shrink_target (size_t used)
 Compute the target buffer size for a shrink operation.
 
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 should_attempt_shrink (t_arena *arena)
 Determine whether the arena is eligible for dynamic shrinking.
 

Detailed Description

Arena memory resizing logic — dynamic growth and shrinking.

Author
Toonsa
Date
2025

This file implements the dynamic memory resizing features of the arena allocator, allowing it to grow when allocations exceed current capacity, and shrink when memory usage drops significantly.

Features covered in this file:

All operations are thread-safe and acquire internal locks on the arena during mutation of memory size or related metadata.

This module is especially useful for long-lived arenas in high-uptime applications, where memory usage patterns may fluctuate and reclaiming unused memory is desirable.

Note
The resizing logic assumes the arena owns its buffer and is marked as growable. Shrinking operations are designed to be conservative to prevent thrashing.

Definition in file arena_resize.c.

Function Documentation

◆ arena_grow()

bool arena_grow ( t_arena *  arena,
size_t  required_size 
)

Definition at line 142 of file arena_resize.c.

143{
144 if (!arena)
145 return false;
146
147 if (required_size == 0)
148 return true;
149
150 ARENA_LOCK(arena);
151
152 if (!arena_grow_validate(arena, required_size))
153 {
154 ARENA_UNLOCK(arena);
155 return false;
156 }
157
158 size_t old_size = arena->size;
159 size_t new_size = arena_grow_compute_new_size(arena, required_size);
160 if (new_size == 0)
161 {
162 arena_report_error(arena, "arena_grow failed: computed size invalid");
163 ARENA_UNLOCK(arena);
164 return false;
165 }
166
167 if (new_size > (size_t)(1ULL << 32)) // 4GiB
168 {
169 arena_report_error(arena, "arena_grow rejected size: %zu", new_size);
170 return false;
171 }
172 bool success = arena_grow_realloc_buffer(arena, new_size, old_size);
173 ARENA_UNLOCK(arena);
174 return success;
175}
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
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_grow_validate(t_arena *arena, size_t required_size)
Validate whether the arena is allowed to grow for a given request.
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.

References arena_grow_compute_new_size(), arena_grow_realloc_buffer(), arena_grow_validate(), ARENA_LOCK, arena_report_error(), and ARENA_UNLOCK.

◆ arena_might_shrink()

bool arena_might_shrink ( t_arena *  arena)

Definition at line 385 of file arena_resize.c.

386{
387 if (!should_attempt_shrink(arena))
388 return false;
389
390 ARENA_LOCK(arena);
391 ARENA_CHECK(arena);
392
393 const size_t used = arena->offset;
394 const size_t size = arena->size;
395
397 {
398 const size_t target = arena_shrink_target(used);
399
400 if (target < size)
401 {
402 arena_shrink(arena, target);
403 ARENA_UNLOCK(arena);
404 return true;
405 }
406 }
407
408 ARENA_UNLOCK(arena);
409 return false;
410}
#define ARENA_CHECK(arena)
size_t used
Number of bytes used in the arena buffer.
Definition arena_io.h:2
void arena_shrink(t_arena *arena, size_t new_size)
static size_t arena_shrink_target(size_t used)
Compute the target buffer size for a shrink operation.
static bool should_attempt_shrink(t_arena *arena)
Determine whether the arena is eligible for dynamic shrinking.
static bool arena_should_maybe_shrink(size_t used, size_t size)
Decide whether the arena buffer should be shrunk based on usage ratio.

References ARENA_CHECK, ARENA_LOCK, arena_should_maybe_shrink(), arena_shrink(), arena_shrink_target(), ARENA_UNLOCK, should_attempt_shrink(), and used.

◆ arena_shrink()

void arena_shrink ( t_arena *  arena,
size_t  new_size 
)

Definition at line 267 of file arena_resize.c.

268{
269 if (!arena)
270 return;
271
272 ARENA_LOCK(arena);
273 ARENA_CHECK(arena);
274
275 bool ok = arena_shrink_validate(arena, new_size) && arena_shrink_apply(arena, new_size);
276
277 (void) ok;
278 ARENA_UNLOCK(arena);
279}
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_shrink_apply(t_arena *arena, size_t new_size)
Apply the memory shrink operation to an arena's buffer.

References ARENA_CHECK, ARENA_LOCK, arena_shrink_apply(), arena_shrink_validate(), and ARENA_UNLOCK.