memory_arena
Loading...
Searching...
No Matches
arena_visualizer.c
Go to the documentation of this file.
1#include "arena_visualizer.h"
2#include "arena.h"
3#include <notcurses/notcurses.h>
4#include <stdio.h>
5#include <stdlib.h>
6#include <string.h>
7#include <unistd.h>
8
9void draw_bar_to_plane(struct ncplane* plane, unsigned y, const char* label, size_t value, size_t max, int width)
10{
11 int filled = max > 0 ? (int) ((double) value / max * width) : 0;
12 filled = filled > width ? width : filled;
13
14 ncplane_printf_yx(plane, y, 0, "%-8s [", label);
15 ncplane_set_fg_rgb(plane, 0x00FF00);
16 for (int i = 0; i < width; ++i)
17 ncplane_putstr_yx(plane, y, 10 + i, i < filled ? "█" : "░");
18 ncplane_set_fg_default(plane);
19 ncplane_printf_yx(plane, y, 11 + width, "] %zu / %zu", value, max);
20}
21
22static void vis_record_event(t_arena_visualizer* vis, bool is_sub, bool is_step, bool is_error, size_t usage,
23 size_t size, size_t offset, const char* msg)
24{
25 if (!vis)
26 return;
27
28 size_t idx = atomic_fetch_add(&vis->history_index, 1) % MAX_HISTORY;
29 t_arena_history_entry* e = &vis->history[idx];
30 e->is_sub = is_sub;
31 e->is_step = is_step;
32 e->is_error = is_error;
33
34 e->usage = usage;
35 e->size = size;
36 e->offset = offset;
37
38 snprintf(e->label, MAX_LABEL_LEN, "%s", (msg ? msg : "(unnamed)"));
39
40 size_t old_count = atomic_load(&vis->history_count);
41 if (old_count < MAX_HISTORY)
42 {
43 atomic_fetch_add(&vis->history_count, 1);
44 }
45}
46
47void arena_visualizer_allocation_hook(t_arena* arena, int id, void* ptr, size_t size, size_t offset, size_t wasted,
48 const char* label)
49{
50 (void) id;
51 (void) ptr;
52 (void) wasted;
53 if (!arena)
54 return;
55
56 t_arena_visualizer* vis = (t_arena_visualizer*) arena->hooks.context;
57 if (!vis)
58 return;
59
60 bool is_sub = (arena->parent_ref != NULL);
61 size_t usage = arena_used(arena);
62
63 vis_record_event(vis, is_sub, false, false, usage, size, offset, label);
64}
65
66void arena_visualizer_error_callback(const char* msg, void* context)
67{
68 t_arena_visualizer* vis = (t_arena_visualizer*) context;
69 if (!vis)
70 {
71 // fallback
72 fprintf(stderr, "[ARENA ERROR] %s\n", msg);
73 return;
74 }
75 vis_record_event(vis, false, false, true, 0, 0, 0, msg);
76}
77
78void step_message(t_arena_visualizer* vis, const char* msg)
79{
80 if (!vis)
81 {
82 // fallback
83 fprintf(stderr, "STEP: %s\n", msg);
84 return;
85 }
86 vis_record_event(vis, false, true, false, 0, 0, 0, msg);
87}
88
90{
91 if (!vis->nc || !vis->main_plane || !vis->arena)
92 return;
93
94 ncplane_erase(vis->main_plane);
95
96 unsigned y = 0;
97 int rows, cols;
98 ncplane_dim_yx(vis->main_plane, (unsigned*) &rows, (unsigned*) &cols);
99
100 // Header
101 ncplane_set_fg_rgb(vis->main_plane, 0xFFD700); // Gold
102 ncplane_printf_yx(vis->main_plane, y++, (cols - 36) / 2, "=== 🚀 Interactive Arena Visualizer 🚀 ===");
103
104 ncplane_set_fg_default(vis->main_plane);
105 y++;
106
107 // Arena Info
108 ncplane_printf_yx(vis->main_plane, y++, 0, "Arena Label: %s",
109 vis->arena->debug.label ? vis->arena->debug.label : "Unnamed");
110 y++;
111
112 // Usage bars
113 const int bar_width = cols - 25;
114 size_t used = arena_used(vis->arena);
115 size_t total = vis->arena->size;
116 size_t remain = arena_remaining(vis->arena);
117 size_t peak = arena_peak(vis->arena);
118
119 draw_bar_to_plane(vis->main_plane, y++, "Used", used, total, bar_width);
120 draw_bar_to_plane(vis->main_plane, y++, "Remain", remain, total, bar_width);
121 draw_bar_to_plane(vis->main_plane, y++, "Peak", peak, total, bar_width);
122
123 y++;
124
125 // Arena Stats
126 ncplane_set_fg_rgb(vis->main_plane, 0xADD8E6); // Light Blue
127 ncplane_printf_yx(vis->main_plane, y++, 0, "--- Arena Stats ---");
128 ncplane_set_fg_default(vis->main_plane);
129
130 ncplane_printf_yx(vis->main_plane, y++, 0, "Allocations: %zu", vis->arena->stats.allocations);
131 ncplane_printf_yx(vis->main_plane, y++, 0, "Reallocations: %zu", vis->arena->stats.reallocations);
132 ncplane_printf_yx(vis->main_plane, y++, 0, "Total Allocated: %zu bytes", vis->arena->stats.bytes_allocated);
133 ncplane_printf_yx(vis->main_plane, y++, 0, "Alignment Waste: %zu bytes",
134 vis->arena->stats.wasted_alignment_bytes);
135 ncplane_printf_yx(vis->main_plane, y++, 0, "Live Allocations: %zu", vis->arena->stats.live_allocations);
136 y++;
137
138 // Events Header
139 ncplane_set_fg_rgb(vis->main_plane, 0x90EE90); // Light green
140 ncplane_printf_yx(vis->main_plane, y++, 0, "--- Events (Oldest ➡️ Newest) ---");
141 ncplane_set_fg_default(vis->main_plane);
142
143 // Ring buffer rendering with smooth scrolling
144 size_t count = atomic_load(&vis->history_count);
145 size_t idx = atomic_load(&vis->history_index);
146 size_t hist = (count < MAX_HISTORY) ? count : MAX_HISTORY;
147
148 int lines_for_ring = rows - y - 2;
149 if (lines_for_ring < 1)
150 lines_for_ring = 1;
151 int max_offset = (int) hist - lines_for_ring;
152 max_offset = max_offset < 0 ? 0 : max_offset;
153 vis->scroll_offset = vis->scroll_offset < 0 ? 0 : vis->scroll_offset;
154 vis->scroll_offset = vis->scroll_offset > max_offset ? max_offset : vis->scroll_offset;
155
156 int start = (int) hist - lines_for_ring - vis->scroll_offset;
157 start = start < 0 ? 0 : start;
158
159 for (int i = start; i < (int) hist; i++)
160 {
161 t_arena_history_entry* e = &vis->history[(idx + i + (MAX_HISTORY - hist)) % MAX_HISTORY];
162
163 if (y >= (unsigned) rows - 1)
164 break;
165
166 uint64_t color = e->is_error ? 0xFF4500 : e->is_step ? 0x00BFFF : e->is_sub ? 0x32CD32 : 0xFFFFFF;
167
168 ncplane_set_fg_rgb(vis->main_plane, color);
169
170 if (e->is_error)
171 ncplane_printf_yx(vis->main_plane, y++, 0, "🔥 ERROR: %s", e->label);
172 else if (e->is_step)
173 ncplane_printf_yx(vis->main_plane, y++, 0, "🚩 STEP: %s", e->label);
174 else if (e->is_sub)
175 ncplane_printf_yx(vis->main_plane, y++, 0, "🧩 SUB: usage=%zu, size=%zu, offset=%zu [%s]", e->usage,
176 e->size, e->offset, e->label);
177 else
178 ncplane_printf_yx(vis->main_plane, y++, 0, "📌 usage=%zu, size=%zu, offset=%zu [%s]", e->usage, e->size,
179 e->offset, e->label);
180 }
181
182 // Footer with helpful instructions
183 ncplane_set_fg_rgb(vis->main_plane, 0xFFD700);
184 ncplane_printf_yx(vis->main_plane, rows - 1, 0, "📖 Arrows: Scroll ⬆️⬇️ | 'n': Next step ▶️ | 'q': Quit ❌\n");
185
186 ncplane_set_fg_default(vis->main_plane);
187 notcurses_render(vis->nc);
188}
189
199{
200 while (1)
201 {
202 draw_visualizer(vis);
203 uint32_t key = notcurses_get_blocking(vis->nc, NULL);
204 switch (key)
205 {
206 case 'q':
207 case 'Q':
208 case NCKEY_ESC:
209 return 0; // user wants to quit
210 case NCKEY_UP:
211 vis->scroll_offset++;
212 break;
213 case NCKEY_DOWN:
214 vis->scroll_offset--;
215 break;
216 case 'n':
217 case 'N':
218 return 1; // next step
219 default:
220 // do nothing
221 break;
222 }
223 }
224}
225
233{
234 // We won't do an internal scenario script here — we assume user code calls
235 // "step_message" + "alloc" from main. If you want the script integrated,
236 // you can do it here. But let's just show how to do indefinite steps:
237
238 while (1)
239 {
240 int ret = wait_for_key(vis);
241 if (!ret)
242 { // user pressed q => done
243 break;
244 }
245 // user pressed n => return to caller so the next step in main can run
246 return;
247 }
248}
249
251{
252 if (!vis || !arena)
253 return;
255}
256
258{
259 if (!vis || !arena)
260 return;
261
262 struct notcurses_options opts = {
263 .termtype = NULL,
264 .loglevel = NCLOGLEVEL_SILENT,
265 };
266
267 vis->nc = notcurses_init(&opts, NULL);
268 if (!vis->nc)
269 {
270 fprintf(stderr, "[Visualizer] Failed to init notcurses\n");
271 return;
272 }
273
274 vis->main_plane = notcurses_stdplane(vis->nc);
275 pthread_mutex_init(&vis->lock, NULL);
276
277 atomic_store(&vis->history_index, 0);
278 atomic_store(&vis->history_count, 0);
279
280 vis->scroll_offset = 0;
281 vis->script_done = false;
282 vis->arena = arena;
283
284 // We won't do an infinite loop here. We'll just do a single call
285 // to interactive_loop so user can press 'n' or 'q'
286 // Then we return control to main. So main can do partial steps, come back, etc.
287
288 interactive_loop(vis);
289 // user pressed q or n => we return
290}
Core public API for the Arena memory allocator.
size_t arena_used(t_arena *arena)
Definition arena_state.c:73
size_t arena_remaining(t_arena *arena)
size_t arena_peak(t_arena *arena)
size_t used
Number of bytes used in the arena buffer.
Definition arena_io.h:2
static void interactive_loop(t_arena_visualizer *vis)
The main interactive loop: 1) user sees ring buffer 2) user presses 'n' to run next script step 3) us...
void draw_bar_to_plane(struct ncplane *plane, unsigned y, const char *label, size_t value, size_t max, int width)
void arena_visualizer_error_callback(const char *msg, void *context)
void step_message(t_arena_visualizer *vis, const char *msg)
int wait_for_key(t_arena_visualizer *vis)
function that blocks reading keystrokes:
void arena_start_interactive_visualizer(t_arena_visualizer *vis, t_arena *arena)
Start the interactive session (blocks until user quits).
void arena_visualizer_enable_history_hook(t_arena_visualizer *vis, t_arena *arena)
Attach our allocation hook to an arena.
static void vis_record_event(t_arena_visualizer *vis, bool is_sub, bool is_step, bool is_error, size_t usage, size_t size, size_t offset, const char *msg)
void draw_visualizer(t_arena_visualizer *vis)
void arena_visualizer_allocation_hook(t_arena *arena, int id, void *ptr, size_t size, size_t offset, size_t wasted, const char *label)
The ring buffer hooks.
#define MAX_LABEL_LEN
t_arena_history_entry history[MAX_HISTORY]
struct ncplane * main_plane
struct notcurses * nc
pthread_mutex_t lock
#define MAX_HISTORY
atomic_size_t history_index
atomic_size_t history_count
char label[MAX_LABEL_LEN]
Each ring-buffer event: error, step, normal or sub-alloc.
The main interactive visualizer.
void arena_set_allocation_hook(t_arena *arena, arena_allocation_hook cb, void *context)
Set or remove an allocation hook on a given arena.
The main memory arena structure used for fast allocation.