memory_arena
Loading...
Searching...
No Matches
Functions
Sub-Arena Internals

Internal helpers for configuring sub-arenas. More...

+ Collaboration diagram for Sub-Arena Internals:

Functions

static bool arena_alloc_sub_validate (t_arena *parent, t_arena *child)
 Validate the input parameters for creating a sub-arena.
 
static void arena_generate_subarena_id (t_arena *parent, t_arena *child)
 Generate a unique debug identifier for a sub-arena.
 
static void arena_setup_subarena (t_arena *parent, t_arena *child, void *buffer, size_t size, const char *label)
 Initialize a child arena as a sub-arena of a parent using a provided memory block.
 

Detailed Description

Internal helpers for configuring sub-arenas.

Function Documentation

◆ arena_alloc_sub_validate()

static bool arena_alloc_sub_validate ( t_arena *  parent,
t_arena *  child 
)
inlinestatic

#include <arena_alloc_sub.c>

Validate the input parameters for creating a sub-arena.

This internal function ensures that both parent and child arena pointers are valid before proceeding with a sub-arena allocation. It performs:

  • A null check on the parent and child pointers.
  • A basic sanity check on the parent pointer's address to ensure it isn't accidentally an invalid pointer (e.g., very low memory address).

If validation fails, an appropriate error is reported via arena_report_error().

Parameters
parentPointer to the parent arena from which memory will be allocated.
childPointer to the arena struct that will be initialized as a sub-arena.
Returns
true if both parameters are valid, false otherwise.
See also
arena_alloc_sub
arena_setup_subarena
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc_sub.c.

Definition at line 446 of file arena_alloc_sub.c.

447{
448 if (!parent || (uintptr_t) parent < 0x1000)
449 {
450 arena_report_error(NULL, "arena_alloc_sub failed: invalid parent arena");
451 return false;
452 }
453 if (!child)
454 {
455 arena_report_error(NULL, "arena_alloc_sub failed: NULL child");
456 return false;
457 }
458 return true;
459}
void arena_report_error(t_arena *arena, const char *fmt,...)
Report an error from an arena using its configured error callback.

References arena_report_error().

◆ arena_generate_subarena_id()

static void arena_generate_subarena_id ( t_arena *  parent,
t_arena *  child 
)
inlinestatic

#include <arena_alloc_sub.c>

Generate a unique debug identifier for a sub-arena.

This internal helper constructs a human-readable ID string for a child (sub-arena), based on its parent arena's ID. The generated ID takes the form:

`<PREF>.<N>`

Where <PREF> is the first four characters of the parent arena's ID and <N> is an incrementing counter (subarena index). This ID is stored in child->debug.id and can be used in logs, debug tools, or allocation tracking.

The parent's subarena counter is incremented after each call.

Parameters
parentPointer to the parent arena.
childPointer to the child arena to assign the ID.
Note
The child’s ID is truncated to ARENA_ID_LEN if the formatted string exceeds the limit.
See also
arena_setup_subarena
arena_alloc_sub_labeled_aligned
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc_sub.c.

Definition at line 532 of file arena_alloc_sub.c.

533{
534 int sub_id = parent->debug.subarena_counter++;
535 snprintf(child->debug.id, ARENA_ID_LEN, "%.4s.%d", parent->debug.id, sub_id);
536}
#define ARENA_ID_LEN
Max characters for arena ID strings.
Definition arena_config.h:6

References ARENA_ID_LEN.

◆ arena_setup_subarena()

static void arena_setup_subarena ( t_arena *  parent,
t_arena *  child,
void *  buffer,
size_t  size,
const char *  label 
)
inlinestatic

#include <arena_alloc_sub.c>

Initialize a child arena as a sub-arena of a parent using a provided memory block.

This internal helper prepares a child arena to operate as a sub-arena carved from its parent. It performs the following steps:

  • Initializes the child using arena_init_with_buffer() with the given buffer and size.
  • Marks the buffer as not owned by the child (ownership remains with the parent).
  • Sets a reference to the parent arena in child->parent_ref.
  • Generates a unique debug ID for the child based on its parent.
  • Sets a debug label for identification (defaults to "subarena" if label == NULL).
  • Validates the state of both arenas using ARENA_CHECK().

This function assumes that the memory buffer was already allocated from the parent arena.

Parameters
parentPointer to the parent arena.
childPointer to the arena to be configured as a sub-arena.
bufferMemory block allocated from the parent to back the child arena.
sizeSize of the memory block in bytes.
labelOptional debug label for the sub-arena. Can be NULL.
Note
This function does not allocate memory. It only sets up the child to use memory previously allocated from the parent.
See also
arena_alloc_sub_labeled_aligned
arena_init_with_buffer
arena_generate_subarena_id
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/allocation/arena_alloc_sub.c.

Definition at line 493 of file arena_alloc_sub.c.

494{
495 arena_init_with_buffer(child, buffer, size, false);
496 atomic_store_explicit(&child->owns_buffer, false, memory_order_release);
497 child->parent_ref = parent;
498 arena_generate_subarena_id(parent, child);
499 arena_set_debug_label(child, label ? label : "subarena");
500
501 ARENA_CHECK(parent);
502 ARENA_CHECK(child);
503}
void arena_init_with_buffer(t_arena *arena, void *buffer, size_t size, bool allow_grow)
#define ARENA_CHECK(arena)
void arena_set_debug_label(t_arena *arena, const char *label)
Assign a debug label to an arena.
static void arena_generate_subarena_id(t_arena *parent, t_arena *child)
Generate a unique debug identifier for a sub-arena.

References ARENA_CHECK, arena_generate_subarena_id(), arena_init_with_buffer(), and arena_set_debug_label().