memory_arena
Loading...
Searching...
No Matches
Functions
arena_visualizer.c File Reference
#include "arena_visualizer.h"
#include "arena.h"
#include <notcurses/notcurses.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+ Include dependency graph for arena_visualizer.c:

Go to the source code of this file.

Functions

void arena_start_interactive_visualizer (t_arena_visualizer *vis, t_arena *arena)
 Start the interactive session (blocks until user quits).
 
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.
 
void arena_visualizer_enable_history_hook (t_arena_visualizer *vis, t_arena *arena)
 Attach our allocation hook to an arena.
 
void arena_visualizer_error_callback (const char *msg, void *context)
 
void draw_bar_to_plane (struct ncplane *plane, unsigned y, const char *label, size_t value, size_t max, int width)
 
void draw_visualizer (t_arena_visualizer *vis)
 
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) user presses 'q' any time => done.
 
void step_message (t_arena_visualizer *vis, const char *msg)
 
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)
 
int wait_for_key (t_arena_visualizer *vis)
 function that blocks reading keystrokes:
 

Function Documentation

◆ arena_start_interactive_visualizer()

void arena_start_interactive_visualizer ( t_arena_visualizer *  vis,
t_arena *  arena 
)

Start the interactive session (blocks until user quits).

The user can scroll the ring buffer with arrow keys, press 'n' to do next step, press 'q' to quit, etc.

Definition at line 257 of file arena_visualizer.c.

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}
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...
struct ncplane * main_plane
struct notcurses * nc
pthread_mutex_t lock
atomic_size_t history_index
atomic_size_t history_count

References s_arena_visualizer::arena, s_arena_visualizer::history_count, s_arena_visualizer::history_index, interactive_loop(), s_arena_visualizer::lock, s_arena_visualizer::main_plane, s_arena_visualizer::nc, s_arena_visualizer::script_done, and s_arena_visualizer::scroll_offset.

◆ arena_visualizer_allocation_hook()

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.

Definition at line 47 of file arena_visualizer.c.

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}
size_t arena_used(t_arena *arena)
Definition arena_state.c:73
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)
The main interactive visualizer.

References arena_used(), and vis_record_event().

◆ arena_visualizer_enable_history_hook()

void arena_visualizer_enable_history_hook ( t_arena_visualizer *  vis,
t_arena *  arena 
)

Attach our allocation hook to an arena.

Definition at line 250 of file arena_visualizer.c.

251{
252 if (!vis || !arena)
253 return;
255}
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.
void arena_set_allocation_hook(t_arena *arena, arena_allocation_hook cb, void *context)
Set or remove an allocation hook on a given arena.

References arena_set_allocation_hook(), and arena_visualizer_allocation_hook().

◆ arena_visualizer_error_callback()

void arena_visualizer_error_callback ( const char *  msg,
void *  context 
)

Definition at line 66 of file arena_visualizer.c.

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}

References vis_record_event().

◆ draw_bar_to_plane()

void draw_bar_to_plane ( struct ncplane *  plane,
unsigned  y,
const char *  label,
size_t  value,
size_t  max,
int  width 
)

Definition at line 9 of file arena_visualizer.c.

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}

◆ draw_visualizer()

void draw_visualizer ( t_arena_visualizer *  vis)

Definition at line 89 of file arena_visualizer.c.

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}
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
void draw_bar_to_plane(struct ncplane *plane, unsigned y, const char *label, size_t value, size_t max, int width)
t_arena_history_entry history[MAX_HISTORY]
#define MAX_HISTORY
char label[MAX_LABEL_LEN]
Each ring-buffer event: error, step, normal or sub-alloc.

References s_arena_visualizer::arena, arena_peak(), arena_remaining(), arena_used(), draw_bar_to_plane(), s_arena_visualizer::history, s_arena_visualizer::history_count, s_arena_visualizer::history_index, s_arena_history_entry::is_error, s_arena_history_entry::is_step, s_arena_history_entry::is_sub, s_arena_history_entry::label, s_arena_visualizer::main_plane, MAX_HISTORY, s_arena_visualizer::nc, s_arena_history_entry::offset, s_arena_visualizer::scroll_offset, s_arena_history_entry::size, s_arena_history_entry::usage, and used.

◆ interactive_loop()

static void interactive_loop ( t_arena_visualizer *  vis)
static

The main interactive loop: 1) user sees ring buffer 2) user presses 'n' to run next script step 3) user presses 'q' any time => done.

Definition at line 232 of file arena_visualizer.c.

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}
int wait_for_key(t_arena_visualizer *vis)
function that blocks reading keystrokes:

References wait_for_key().

◆ step_message()

void step_message ( t_arena_visualizer *  vis,
const char *  msg 
)

Definition at line 78 of file arena_visualizer.c.

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}

References vis_record_event().

◆ vis_record_event()

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 
)
static

Definition at line 22 of file arena_visualizer.c.

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}
#define MAX_LABEL_LEN

References s_arena_visualizer::history, s_arena_visualizer::history_count, s_arena_visualizer::history_index, s_arena_history_entry::is_error, s_arena_history_entry::is_step, s_arena_history_entry::is_sub, s_arena_history_entry::label, MAX_HISTORY, MAX_LABEL_LEN, s_arena_history_entry::offset, s_arena_history_entry::size, and s_arena_history_entry::usage.

◆ wait_for_key()

int wait_for_key ( t_arena_visualizer *  vis)

function that blocks reading keystrokes:

  • UP arrow => scroll_offset++
  • DOWN arrow => scroll_offset–
  • 'n' => next step => return 1
  • 'q' => quit => return 0 Otherwise keep waiting

Definition at line 198 of file arena_visualizer.c.

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}
void draw_visualizer(t_arena_visualizer *vis)

References draw_visualizer(), s_arena_visualizer::nc, and s_arena_visualizer::scroll_offset.