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

Cleanup and teardown routines for memory arenas. More...

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

Go to the source code of this file.

Functions

void arena_delete (t_arena **arena)
 
void arena_destroy (t_arena *arena)
 
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

Cleanup and teardown routines for memory arenas.

Author
Toonsa
Date
2025

This file provides functions to safely destroy and deallocate arena resources, including internal memory buffers, metadata like growth history, and synchronization primitives. It ensures proper cleanup whether the arena is heap-allocated or stack-allocated.

API functions included:

These functions are used internally and publicly to manage arena lifecycles and to ensure memory safety, thread-safety, and diagnostic cleanliness.

Definition in file arena_cleanup.c.

Function Documentation

◆ arena_delete()

void arena_delete ( t_arena **  arena)

Definition at line 148 of file arena_cleanup.c.

149{
150 if (!arena || !*arena)
151 return;
152
153 arena_destroy(*arena);
154 free(*arena);
155 *arena = NULL;
156}
void arena_destroy(t_arena *arena)

References arena_destroy().

◆ arena_destroy()

void arena_destroy ( t_arena *  arena)

Definition at line 82 of file arena_cleanup.c.

83{
84 if (!arena)
85 return;
86
87 bool expected = false;
88 if (!atomic_compare_exchange_strong(&arena->is_destroying, &expected, true))
89 return;
90
91#ifdef ARENA_ENABLE_THREAD_SAFE
92 if (arena->use_lock)
93 {
94 ARENA_CHECK(arena);
95 ARENA_LOCK(arena);
96
99
100 arena_zero_metadata(arena);
101
102 ARENA_UNLOCK(arena);
103 arena->use_lock = false;
104 pthread_mutex_destroy(&arena->lock);
105 return;
106 }
107#endif
108
111 arena_zero_metadata(arena);
112}
#define ARENA_CHECK(arena)
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
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.
void arena_zero_metadata(t_arena *arena)
Zero out and reset all debug/stats metadata associated with the arena.

References ARENA_CHECK, arena_free_buffer_if_owned(), arena_free_growth_history(), ARENA_LOCK, ARENA_UNLOCK, and arena_zero_metadata().