memory_arena
Loading...
Searching...
No Matches
arena_cleanup.c
Go to the documentation of this file.
1
24#include "arena.h"
25
26/*
27 * INTERNAL FUNCTION DECLARATIONS
28 */
29static inline void arena_free_buffer_if_owned(t_arena* arena);
30static inline void arena_free_growth_history(t_arena* arena);
31
32/*
33 * PUBLIC API
34 */
35
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}
113
149{
150 if (!arena || !*arena)
151 return;
152
153 arena_destroy(*arena);
154 free(*arena);
155 *arena = NULL;
156}
157
158/*
159 * INTERNAL HELPERS
160 */
161
186static inline void arena_free_buffer_if_owned(t_arena* arena)
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}
197
221static inline void arena_free_growth_history(t_arena* arena)
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}
Core public API for the Arena memory allocator.
void arena_destroy(t_arena *arena)
void arena_delete(t_arena **arena)
#define arena_poison_memory(ptr, size)
No-op poison macro when poisoning is disabled.
#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.
The main memory arena structure used for fast allocation.