memory_arena
Loading...
Searching...
No Matches
arena_scratch.c
Go to the documentation of this file.
1
199#include "arena_scratch.h"
200#include <stdlib.h>
201#include <string.h>
202
203/*
204 * INTERNAL HELPER DECLARATION
205 */
206
207static bool scratch_slot_init(t_scratch_slot* slot, size_t slot_size, int index);
208
209/*
210 * PUBLIC API
211 */
212
249bool scratch_pool_init(t_scratch_arena_pool* pool, size_t slot_size, bool thread_safe)
250{
251 if (!pool || slot_size == 0)
252 return arena_report_error(NULL, "scratch_pool_init failed: invalid arguments"), false;
253
254 memset(pool, 0, sizeof(*pool));
255 pool->slot_size = slot_size;
256 pool->thread_safe = thread_safe;
257
258#ifdef ARENA_ENABLE_THREAD_SAFE
259 if (thread_safe)
260 pthread_mutex_init(&pool->lock, NULL);
261#endif
262
263 for (int i = 0; i < SCRATCH_MAX_SLOTS; ++i)
264 {
265 if (!scratch_slot_init(&pool->slots[i], slot_size, i))
266 {
268 return false;
269 }
270 }
271 return true;
272}
273
303{
304 if (!pool)
305 return;
306
307 for (int i = 0; i < SCRATCH_MAX_SLOTS; ++i)
308 arena_destroy(&pool->slots[i].arena);
309
310#ifdef ARENA_ENABLE_THREAD_SAFE
311 if (pool->thread_safe)
312 pthread_mutex_destroy(&pool->lock);
313#endif
314
315 memset(pool, 0, sizeof(*pool));
316}
317
351{
352 if (!pool)
353 return arena_report_error(NULL, "scratch_acquire failed: pool is NULL"), NULL;
354
355 SCRATCH_LOCK(pool);
356 for (int i = 0; i < SCRATCH_MAX_SLOTS; ++i)
357 {
358 if (!atomic_exchange(&pool->slots[i].in_use, true))
359 {
360 arena_reset(&pool->slots[i].arena);
361 SCRATCH_UNLOCK(pool);
362 return &pool->slots[i].arena;
363 }
364 }
365 SCRATCH_UNLOCK(pool);
366 return arena_report_error(NULL, "scratch_acquire failed: all slots in use"), NULL;
367}
368
399{
400 if (!pool || !arena)
401 {
402 arena_report_error(NULL, "scratch_release failed: null pool or arena");
403 return;
404 }
405
406 for (int i = 0; i < SCRATCH_MAX_SLOTS; ++i)
407 {
408 if (&pool->slots[i].arena == arena)
409 {
410 atomic_store(&pool->slots[i].in_use, false);
411 return;
412 }
413 }
414
415 arena_report_error(arena, "scratch_release failed: arena not found in pool");
416}
417
418/*
419 * INTERNAL HELPER
420 */
421
449static bool scratch_slot_init(t_scratch_slot* slot, size_t slot_size, int index)
450{
451 if (!arena_init(&slot->arena, slot_size, true))
452 {
453 arena_report_error(NULL, "scratch_slot_init failed: arena_init failed for slot %d", index);
454 return false;
455 }
456 atomic_store(&slot->in_use, false);
457 return true;
458}
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.