memory_arena
Loading...
Searching...
No Matches
Files | Functions
Arena Allocation

Public functions for memory allocation from an arena. More...

Files

file  arena_alloc.c
 Public memory allocation functions for the arena allocator.
 
file  arena_calloc.c
 Zero-initialized memory allocation functions for arenas.
 
file  arena_hooks.c
 Allocation hook management for tracking and debugging arena memory usage.
 
file  arena_hooks.h
 Allocation hook system for arena-based memory tracking and introspection.
 

Functions

void arena_set_allocation_hook (t_arena *arena, arena_allocation_hook cb, void *context)
 Set or remove an allocation hook on a given arena.
 

Detailed Description

Public functions for memory allocation from an arena.

Function Documentation

◆ arena_set_allocation_hook()

void arena_set_allocation_hook ( t_arena *  arena,
arena_allocation_hook  cb,
void *  context 
)

#include <arena_hooks.h>

Set or remove an allocation hook on a given arena.

Set a custom allocation hook for the given arena.

This function installs a callback that will be invoked after each successful allocation (including realloc and calloc) within the specified arena.

If cb is NULL, any previously installed hook is removed.

Parameters
arenaPointer to the target arena.
cbHook callback function (or NULL to disable).
contextOptional user-defined context passed to the hook.
Returns
void
Warning
Do not perform allocations from the same arena within the hook itself. Doing so can cause recursion or deadlocks.
See also
arena_allocation_hook

This function installs a user-defined allocation hook on the specified arena. The hook is a callback function that will be invoked after each successful memory allocation (arena_alloc, arena_calloc, arena_realloc_last), allowing custom instrumentation, logging, debugging, or tracking.

The hook receives detailed metadata for each allocation, including:

  • The arena pointer.
  • A unique allocation ID.
  • The pointer to the allocated memory.
  • The size and offset of the allocation.
  • The number of alignment/wasted bytes.
  • An optional user-defined label (may be NULL).

If cb is NULL, the current hook will be removed and no further allocation events will be triggered.

Parameters
arenaPointer to the arena to attach the hook to.
cbHook callback function, or NULL to disable.
contextOptional user data or context to associate with the hook (not used internally).
Returns
void
Note
Hooks are invoked inside the arena_alloc_internal() implementation. If thread safety is enabled, this function acquires the arena lock to modify internal state.
Warning
Hooks should not perform allocations from the same arena to avoid recursion or deadlocks.
See also
arena_allocation_hook
arena_alloc
arena_hooks.h
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_hooks.c.

Definition at line 163 of file arena_hooks.c.

164{
165 if (!arena)
166 {
167 arena_report_error(NULL, "arena_set_allocation_hook failed: NULL arena");
168 return;
169 }
170
171 ARENA_LOCK(arena);
172 arena->hooks.hook_cb = cb;
173 arena->hooks.context = context;
174 ARENA_UNLOCK(arena);
175}
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.

References ARENA_LOCK, arena_report_error(), and ARENA_UNLOCK.