memory_arena
Loading...
Searching...
No Matches
arena_scratch.h
Go to the documentation of this file.
1
31#ifndef ARENA_SCRATCH_H
32#define ARENA_SCRATCH_H
33
34#include "arena.h"
35#include <pthread.h>
36#include <stdatomic.h>
37#include <stdbool.h>
38#include <stddef.h>
39
41#define SCRATCH_MAX_SLOTS 64
42
44#ifdef ARENA_ENABLE_THREAD_SAFE
45#define SCRATCH_LOCK(pool) \
46 if ((pool)->thread_safe) \
47 pthread_mutex_lock(&(pool)->lock)
48
50#define SCRATCH_UNLOCK(pool) \
51 if ((pool)->thread_safe) \
52 pthread_mutex_unlock(&(pool)->lock)
53#else
54#define SCRATCH_LOCK(pool) ((void) 0)
55#define SCRATCH_UNLOCK(pool) ((void) 0)
56#endif
57
58#ifdef __cplusplus
59extern "C"
60{
61#endif
62
73 typedef struct s_scratch_slot
74 {
76 atomic_bool in_use;
78
96
111 bool scratch_pool_init(t_scratch_arena_pool* pool, size_t slot_size, bool thread_safe);
112
127
141
153 void scratch_release(t_scratch_arena_pool* pool, t_arena* arena);
154
155#ifdef __cplusplus
156}
157#endif
158
159#endif // ARENA_SCRATCH_H
Core public API for the Arena memory allocator.
#define SCRATCH_MAX_SLOTS
Maximum number of concurrently usable scratch arenas in the pool.
atomic_bool in_use
Atomic flag for acquisition tracking.
t_arena arena
Memory arena associated with this slot.
struct s_scratch_slot t_scratch_slot
Represents a single slot in the scratch arena pool.
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.
struct s_scratch_arena_pool t_scratch_arena_pool
Scratch arena pool structure holding multiple reusable memory arenas.
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.