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

Public memory allocation functions for the arena allocator. More...

#include "arena.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
+ Include dependency graph for arena_alloc.c:

Go to the source code of this file.

Functions

void * arena_alloc (t_arena *arena, size_t size)
 
void * arena_alloc_aligned (t_arena *arena, size_t size, size_t alignment)
 
void * arena_alloc_aligned_labeled (t_arena *arena, size_t size, size_t alignment, const char *label)
 
void * arena_alloc_internal (t_arena *arena, size_t size, size_t alignment, const char *label)
 Core internal allocator with alignment and labeling support.
 
void * arena_alloc_labeled (t_arena *arena, size_t size, const char *label)
 
static bool arena_alloc_validate_input (t_arena *arena, size_t size, size_t alignment, const char *label)
 Validate input parameters for arena allocation.
 
static size_t arena_calc_aligned_offset (t_arena *arena, size_t alignment)
 Compute the next aligned offset within the arena.
 
static bool arena_check_overflow (t_arena *arena, size_t size, const char *label)
 Check whether an allocation would cause a size_t overflow.
 
static void arena_commit_allocation (t_arena *arena, size_t size, size_t wasted, size_t aligned_offset)
 Update offset, peak usage, and statistics.
 
static bool arena_ensure_capacity (t_arena *arena, size_t size, size_t alignment, const char *label, size_t *aligned_offset, size_t *wasted)
 Ensure that the arena has enough space for a new allocation with alignment.
 
static void arena_invoke_allocation_hook (t_arena *arena, void *ptr, size_t size, size_t offset, size_t wasted, const char *label)
 Invoke the user-defined allocation hook, if registered.
 
static bool arena_is_being_destroyed (t_arena *arena, const char *label)
 Check whether the arena is currently being destroyed.
 
static bool arena_try_grow (t_arena *arena, size_t size, const char *label)
 Attempt to grow the arena's buffer to satisfy an upcoming allocation.
 
static void arena_update_stats (t_arena *arena, size_t size, size_t wasted)
 Update allocation statistics after a successful arena allocation.
 
static void arena_zero_if_needed (void *ptr, size_t size, const char *label)
 Zero or poison a memory region depending on allocation label.
 

Detailed Description

Public memory allocation functions for the arena allocator.

Author
Toonsa
Date
2025

This file provides the main allocation interface for the arena memory system. It defines four public functions that allow clients to allocate memory from an arena with varying degrees of control over alignment and labeling:

Internally, all of these functions delegate to arena_alloc_internal(), which handles alignment, capacity checks, thread safety, debug labeling, and allocation hooks.

These public functions are designed to be safe, efficient, and flexible, and are the main entry points for memory allocation from a t_arena instance.

See also
arena_alloc
arena_alloc_aligned
arena_alloc_labeled
arena_alloc_aligned_labeled
arena_alloc_internal
arena.h

Definition in file arena_alloc.c.

Function Documentation

◆ arena_alloc()

void * arena_alloc ( t_arena *  arena,
size_t  size 
)

Definition at line 112 of file arena_alloc.c.

113{
114 return arena_alloc_internal(arena, size, ARENA_DEFAULT_ALIGNMENT, "arena_alloc");
115}
#define ARENA_DEFAULT_ALIGNMENT
Default alignment value supported.
void * arena_alloc_internal(t_arena *arena, size_t size, size_t alignment, const char *label)
Core internal allocator with alignment and labeling support.

References arena_alloc_internal(), and ARENA_DEFAULT_ALIGNMENT.

◆ arena_alloc_aligned()

void * arena_alloc_aligned ( t_arena *  arena,
size_t  size,
size_t  alignment 
)

Definition at line 167 of file arena_alloc.c.

168{
169 return arena_alloc_internal(arena, size, alignment, "arena_alloc_aligned");
170}

References arena_alloc_internal().

◆ arena_alloc_aligned_labeled()

void * arena_alloc_aligned_labeled ( t_arena *  arena,
size_t  size,
size_t  alignment,
const char *  label 
)

Definition at line 274 of file arena_alloc.c.

275{
276 if (!label)
277 label = "arena_alloc_aligned_labeled";
278 return arena_alloc_internal(arena, size, alignment, label);
279}

References arena_alloc_internal().

◆ arena_alloc_labeled()

void * arena_alloc_labeled ( t_arena *  arena,
size_t  size,
const char *  label 
)

Definition at line 217 of file arena_alloc.c.

218{
219 if (!label)
220 label = "arena_alloc_labeled";
221 return arena_alloc_internal(arena, size, ARENA_DEFAULT_ALIGNMENT, label);
222}

References arena_alloc_internal(), and ARENA_DEFAULT_ALIGNMENT.