memory_arena
Loading...
Searching...
No Matches
arena_stats.c
Go to the documentation of this file.
1
39#include "arena.h"
40#include <stdlib.h>
41#include <string.h>
42
74void arena_print_stats(t_arena* arena, FILE* stream)
75{
76 if (!arena || !stream)
77 return;
78
79 fprintf(stream, "──────────────────────────────\n");
80 fprintf(stream, " Arena Diagnostics (%s)\n", arena->debug.label ? arena->debug.label : "unnamed");
81 fprintf(stream, "──────────────────────────────\n");
82
83 // Buffer & memory layout
84 fprintf(stream, "- Buffer Address: %p\n", (void*) arena->buffer);
85 fprintf(stream, "- Buffer Size: %zu bytes\n", arena->size);
86 fprintf(stream, "- Current Offset: %zu bytes\n", arena->offset);
87 fprintf(stream, "- Remaining Space: %zu bytes\n", arena->size - arena->offset);
88 fprintf(stream, "- Peak Usage: %zu bytes\n", arena->stats.peak_usage);
89 fprintf(stream, "- Can Grow: %s\n",
90 atomic_load_explicit(&arena->can_grow, memory_order_acquire) ? "yes" : "no");
91
92 // Allocation tracking
93 fprintf(stream, "- Allocations: %zu\n", arena->stats.allocations);
94 fprintf(stream, "- Reallocations: %zu\n", arena->stats.reallocations);
95 fprintf(stream, "- Failed Allocations: %zu\n", arena->stats.failed_allocations);
96 fprintf(stream, "- Live Allocations: %zu\n", arena->stats.live_allocations);
97 fprintf(stream, "- Bytes Allocated: %zu bytes\n", arena->stats.bytes_allocated);
98 fprintf(stream, "- Wasted Alignment Bytes: %zu bytes\n", arena->stats.wasted_alignment_bytes);
99 fprintf(stream, "- Shrinks: %zu\n", arena->stats.shrinks);
100
101 // Last allocation details
102 fprintf(stream, "- Last Alloc Size: %zu bytes\n", arena->stats.last_alloc_size);
103 fprintf(stream, "- Last Alloc Offset: %zu\n", arena->stats.last_alloc_offset);
104 fprintf(stream, "- Last Alloc ID: %zu\n", arena->stats.last_alloc_id);
105
106 // Debug / internal metadata
107 fprintf(stream, "- Debug ID: %s\n", arena->debug.id);
108 fprintf(stream, "- Subarena Counter: %d\n", arena->debug.subarena_counter);
109 fprintf(stream, "- Hook Installed: %s\n", arena->hooks.hook_cb ? "yes" : "no");
110#ifdef ARENA_ENABLE_THREAD_SAFE
111 fprintf(stream, "- Thread Safety: %s\n", arena->use_lock ? "enabled" : "disabled");
112#endif
113
114 // Growth history
115 if (arena->stats.growth_history_count > 0)
116 {
117 fprintf(stream, "- Growth History: ");
118 for (size_t i = 0; i < arena->stats.growth_history_count; ++i)
119 fprintf(stream, "%zu ", arena->stats.growth_history[i]);
120 fprintf(stream, "\n");
121 }
122 else
123 {
124 fprintf(stream, "- Growth History: (none)\n");
125 }
126}
127
164void arena_stats_record_growth(t_arena_stats* stats, size_t new_size)
165{
166 if (!stats)
167 return;
168
169 size_t count = stats->growth_history_count;
170 size_t* new_buffer = realloc(stats->growth_history, (count + 1) * sizeof(size_t));
171 if (!new_buffer)
172 {
173 ALOG("arena_stats_record_growth: failed to realloc for growth history\n");
174 return;
175 }
176
177 stats->growth_history = new_buffer;
178 stats->growth_history[count] = new_size;
179 stats->growth_history_count++;
180}
181
219{
220 t_arena_stats copy;
221 memset(&copy, 0, sizeof(copy));
222
223 if (!arena)
224 return copy;
225
226 ARENA_LOCK((t_arena*) arena);
227
228 copy = arena->stats;
229
230 ARENA_UNLOCK((t_arena*) arena);
231 return copy;
232}
Core public API for the Arena memory allocator.
#define ALOG(...)
Debug logging macro (enabled only if ARENA_DEBUG_LOG is set).
#define ARENA_UNLOCK(arena)
#define ARENA_LOCK(arena)
Acquire the mutex lock if thread safety is enabled.
size_t growth_history_count
Number of growth events recorded.
Definition arena_stats.h:62
size_t * growth_history
Array of size values recorded during arena growth.
Definition arena_stats.h:61
void arena_print_stats(t_arena *arena, FILE *stream)
Print formatted diagnostics about the arena to a stream.
Definition arena_stats.c:74
t_arena_stats arena_get_stats(const t_arena *arena)
Return a snapshot of the arena's internal statistics.
void arena_stats_record_growth(t_arena_stats *stats, size_t size)
Append a new growth event to the growth_history array.
Structure holding runtime statistics for an arena.
Definition arena_stats.h:54
The main memory arena structure used for fast allocation.