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

Implementation of scratch arena pool system for temporary, fast, and reusable allocations. More...

#include "arena_scratch.h"
#include <stdlib.h>
#include <string.h>
+ Include dependency graph for arena_scratch.c:

Go to the source code of this file.

Functions

t_arena * scratch_acquire (t_scratch_arena_pool *pool)
 Acquire a temporary scratch arena from the pool.
 
void scratch_pool_destroy (t_scratch_arena_pool *pool)
 Destroy all arenas in a scratch pool and reset its metadata.
 
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.
 
static bool scratch_slot_init (t_scratch_slot *slot, size_t slot_size, int index)
 Initialize a single scratch slot with its own arena.
 

Detailed Description

Implementation of scratch arena pool system for temporary, fast, and reusable allocations.

Author
Toonsa
Date
2025

This module provides a lightweight pool of scratch arenas designed for short-lived, high-performance memory allocations. A scratch arena pool contains a fixed number of slots (SCRATCH_MAX_SLOTS), each backed by its own t_arena buffer, allowing for efficient reuse without heap fragmentation or frequent allocation overhead.

Key features:

When to use:

When not to use:

API overview:

This implementation uses atomic_bool for per-slot locking and supports optional pthread_mutex_t if ARENA_ENABLE_THREAD_SAFE is defined.

Note
All scratch arenas are growable by default. A typical use pattern is: acquire → allocate → release (no manual free/reset required).

Definition in file arena_scratch.c.