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

Allocation hook management for tracking and debugging arena memory usage. More...

#include "arena.h"
+ Include dependency graph for arena_hooks.c:

Go to the source code of this file.

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

Allocation hook management for tracking and debugging arena memory usage.

Author
Toonsa
Date
2025

This module provides support for installing user-defined callback functions (hooks) that are triggered on every allocation performed within an arena.

Hooks can be used to:

Hooks are invoked only on successful allocations from:

Arena allocation hooks allow you to observe memory behavior without altering your allocation logic. You can:

How to use:

  1. Define a hook function of type arena_allocation_hook:
    void my_hook(t_arena* arena, int id, void* ptr, size_t size, size_t offset, size_t wasted, const char* label) {
    printf("[hook] ID: %d | size: %zu | label: %s\n", id, size, label ? label : "none");
    }
    The main memory arena structure used for fast allocation.
  2. Register it with the arena:
    arena_set_allocation_hook(my_arena, my_hook, NULL);
    void arena_set_allocation_hook(t_arena *arena, arena_allocation_hook cb, void *context)
    Set or remove an allocation hook on a given arena.
  3. Make allocations as usual. Your hook will be invoked on each.

You can also unset the hook by passing NULL to disable tracking.

Warning
The hook callback must not perform allocations from the same arena, or it may cause infinite recursion or deadlocks.
Note
Hooks are optional. If unset (NULL), no callbacks are triggered. Hooks can be updated or cleared at any time via arena_set_allocation_hook(), if cb is NULL, the current hook will be removed and no further allocation events will be displayed.
See also
arena_allocation_hook
arena_set_allocation_hook
arena_hooks.h
arena_alloc_internal

Definition in file arena_hooks.c.