memory_arena
Loading...
Searching...
No Matches
Files | Functions | Variables
Debugging and Validation

Functions and tools for tracking, labeling, poisoning, and validating arenas. More...

Files

file  arena_debug.c
 Debugging, error reporting, memory poisoning, and integrity checking utilities for the Arena memory allocation system.
 
file  arena_debug.h
 Debugging utilities and hooks for the arena memory allocator.
 

Functions

void arena_default_error_callback (const char *msg, void *ctx)
 Default fallback error callback (prints to stderr).
 
void arena_generate_id (t_arena *arena)
 Generate a unique debug ID for an arena.
 
void arena_report_error (t_arena *arena, const char *fmt,...)
 Report an error from an arena using its configured error callback.
 
void arena_set_debug_label (t_arena *arena, const char *label)
 Assign a debug label to an arena.
 
void arena_set_error_callback (t_arena *arena, arena_error_callback cb, void *context)
 Set a custom error callback function for reporting arena errors.
 

Variables

static atomic_int g_arena_id_counter = 0
 Global atomic counter for generating unique arena IDs.
 

Detailed Description

Functions and tools for tracking, labeling, poisoning, and validating arenas.

This group includes optional tools to enhance runtime diagnostics:

These are primarily intended for debug builds and are configurable via macros like ARENA_DEBUG_CHECKS.

Function Documentation

◆ arena_default_error_callback()

void arena_default_error_callback ( const char *  msg,
void *  ctx 
)

#include <arena_debug.h>

Default fallback error callback (prints to stderr).

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_create.c, and /home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 310 of file arena_debug.c.

311{
312 (void) ctx;
313 fprintf(stderr, "[ARENA ERROR] %s\n", msg);
314}

◆ arena_generate_id()

void arena_generate_id ( t_arena *  arena)

#include <arena_debug.h>

Generate a unique debug ID for an arena.

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_create.c, and /home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 102 of file arena_debug.c.

103{
104 if (!arena)
105 return;
106
107 snprintf(arena->debug.id, ARENA_ID_LEN, "A#%04d", atomic_fetch_add(&g_arena_id_counter, 1));
108}
#define ARENA_ID_LEN
Max characters for arena ID strings.
Definition arena_config.h:6
static atomic_int g_arena_id_counter
Global atomic counter for generating unique arena IDs.
Definition arena_debug.c:66

References ARENA_ID_LEN, and g_arena_id_counter.

◆ arena_report_error()

void arena_report_error ( t_arena *  arena,
const char *  fmt,
  ... 
)

#include <arena_debug.h>

Report an error from an arena using its configured error callback.

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc.c, /home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc_sub.c, /home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_calloc.c, /home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_realloc_last.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_create.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_hooks.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_resize.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_scratch.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_state.c, /home/toonsa/myProjects/memoryArena/srcs/core/arena_tlscratch.c, and /home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 253 of file arena_debug.c.

254{
255 va_list args;
256 va_start(args, fmt);
257
258 char message[512];
259 vsnprintf(message, sizeof(message), fmt, args);
260 va_end(args);
261
262 if (arena && arena->debug.error_cb)
263 {
264 arena->debug.error_cb(message, arena->debug.error_context);
265 }
266 else
267 {
268 fprintf(stderr, "[ARENA ERROR]");
269 if (arena && arena->debug.label)
270 fprintf(stderr, " (%s)", arena->debug.label);
271 fprintf(stderr, " %s\n", message);
272 }
273}

◆ arena_set_debug_label()

void arena_set_debug_label ( t_arena *  arena,
const char *  label 
)

#include <arena_debug.h>

Assign a debug label to an arena.

Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc_sub.c, and /home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 147 of file arena_debug.c.

148{
149 if (arena)
150 arena->debug.label = label;
151}

◆ arena_set_error_callback()

void arena_set_error_callback ( t_arena *  arena,
arena_error_callback  cb,
void *  context 
)

#include <arena_debug.h>

Set a custom error callback function for reporting arena errors.

Examples
/home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 201 of file arena_debug.c.

202{
203 if (!arena)
204 return;
205
206 arena->debug.error_cb = cb ? cb : arena_default_error_callback;
207 arena->debug.error_context = cb ? context : NULL;
208}
void arena_default_error_callback(const char *msg, void *ctx)
Default fallback error callback (prints to stderr).

References arena_default_error_callback().

Variable Documentation

◆ g_arena_id_counter

atomic_int g_arena_id_counter = 0
static

#include <arena_debug.c>

Global atomic counter for generating unique arena IDs.

This counter is used to assign a unique identifier string to each arena when arena_generate_id() is called. It ensures that each arena gets a distinct label (e.g., "A#0001", "A#0002", etc.), which can help in debugging, profiling, and tracking arena lifetimes.

The counter is atomic to ensure thread-safe incrementation in concurrent environments.

Note
This variable is internal to the debug system and should not be accessed directly. Use arena_generate_id() to retrieve or assign an arena ID.
Examples
/home/toonsa/myProjects/memoryArena/srcs/internal/arena_debug.c.

Definition at line 66 of file arena_debug.c.