memory_arena
Loading...
Searching...
No Matches
Functions
Arena Cleanup Internals

Internal helpers for teardown and memory release. More...

+ Collaboration diagram for Arena Cleanup Internals:

Functions

static void arena_free_buffer_if_owned (t_arena *arena)
 Free the arena's internal buffer if the arena owns it.
 
static void arena_free_growth_history (t_arena *arena)
 Free the memory used to store the arena's growth history.
 

Detailed Description

Internal helpers for teardown and memory release.

Function Documentation

◆ arena_free_buffer_if_owned()

static void arena_free_buffer_if_owned ( t_arena *  arena)
inlinestatic

#include <arena_cleanup.c>

Free the arena's internal buffer if the arena owns it.

This internal helper checks the owns_buffer atomic flag to determine whether the arena is responsible for freeing its memory buffer. If ownership is confirmed and the buffer is non-null, the function:

  • Applies optional memory poisoning via arena_poison_memory() for debugging.
  • Frees the memory buffer using free().
  • Clears the buffer pointer.
  • Resets the owns_buffer flag atomically to prevent double-free.

This function is typically used during cleanup routines such as arena_destroy() to safely deallocate memory only if it was dynamically allocated by the arena itself.

Parameters
arenaPointer to the arena whose buffer should be conditionally freed.
See also
arena_destroy
arena_set_allocated_buffer
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_cleanup.c.

Definition at line 186 of file arena_cleanup.c.

187{
188 bool owns = atomic_load_explicit(&arena->owns_buffer, memory_order_acquire);
189 if (owns && arena->buffer)
190 {
191 arena_poison_memory(arena->buffer, arena->size);
192 free(arena->buffer);
193 arena->buffer = NULL;
194 atomic_store_explicit(&arena->owns_buffer, false, memory_order_release);
195 }
196}
#define arena_poison_memory(ptr, size)
No-op poison macro when poisoning is disabled.

References arena_poison_memory.

◆ arena_free_growth_history()

static void arena_free_growth_history ( t_arena *  arena)
inlinestatic

#include <arena_cleanup.c>

Free the memory used to store the arena's growth history.

This internal helper checks whether the arena has recorded any growth history via the growth_history pointer in its stats. If so, it:

  • Optionally poisons the memory for debugging (if enabled).
  • Frees the growth_history array.
  • Sets the pointer to NULL to prevent dangling access.

This function is called during teardown (e.g., in arena_destroy) to release dynamically allocated metadata associated with growth tracking.

Parameters
arenaPointer to the arena whose growth history should be freed.
See also
arena_stats_record_growth
arena_destroy
Examples
/home/toonsa/myProjects/memoryArena/srcs/core/arena_cleanup.c.

Definition at line 221 of file arena_cleanup.c.

222{
223 if (arena->stats.growth_history)
224 {
225 arena_poison_memory(arena->stats.growth_history, sizeof(size_t) * arena->stats.growth_history_count);
226 free(arena->stats.growth_history);
227 arena->stats.growth_history = NULL;
228 }
229}

References arena_poison_memory.