251 if (!pool || slot_size == 0)
252 return arena_report_error(NULL,
"scratch_pool_init failed: invalid arguments"),
false;
254 memset(pool, 0,
sizeof(*pool));
258#ifdef ARENA_ENABLE_THREAD_SAFE
260 pthread_mutex_init(&pool->
lock, NULL);
310#ifdef ARENA_ENABLE_THREAD_SAFE
312 pthread_mutex_destroy(&pool->
lock);
315 memset(pool, 0,
sizeof(*pool));
358 if (!atomic_exchange(&pool->
slots[i].
in_use,
true))
453 arena_report_error(NULL,
"scratch_slot_init failed: arena_init failed for slot %d", index);
456 atomic_store(&slot->
in_use,
false);
void arena_reset(t_arena *arena)
bool arena_init(t_arena *arena, size_t size, bool allow_grow)
void arena_destroy(t_arena *arena)
Scratch arena pool system for fast, reusable temporary allocations.
#define SCRATCH_UNLOCK(pool)
#define SCRATCH_MAX_SLOTS
Maximum number of concurrently usable scratch arenas in the pool.
#define SCRATCH_LOCK(pool)
Thread-safe locking macro (enabled only if ARENA_ENABLE_THREAD_SAFE is set)
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.
atomic_bool in_use
Atomic flag for acquisition tracking.
t_arena arena
Memory arena associated with this slot.
static bool scratch_slot_init(t_scratch_slot *slot, size_t slot_size, int index)
Initialize a single scratch slot with its own arena.
Represents a single slot in the scratch arena pool.
size_t slot_size
Size of each arena in bytes.
pthread_mutex_t lock
Mutex for protecting slot access.
t_scratch_slot slots[SCRATCH_MAX_SLOTS]
Array of scratch slots.
bool thread_safe
Whether locking is enabled.
void scratch_pool_destroy(t_scratch_arena_pool *pool)
Destroy all arenas in a scratch pool and reset its metadata.
t_arena * scratch_acquire(t_scratch_arena_pool *pool)
Acquire a temporary scratch arena from the pool.
bool scratch_pool_init(t_scratch_arena_pool *pool, size_t slot_size, bool thread_safe)
Initialize a scratch arena pool with pre-allocated slots.
void scratch_release(t_scratch_arena_pool *pool, t_arena *arena)
Release a scratch arena back to the pool for reuse.
Scratch arena pool structure holding multiple reusable memory arenas.
The main memory arena structure used for fast allocation.