3#include <notcurses/notcurses.h>
9void draw_bar_to_plane(
struct ncplane* plane,
unsigned y,
const char* label,
size_t value,
size_t max,
int width)
11 int filled = max > 0 ? (int) ((
double) value / max * width) : 0;
12 filled = filled > width ? width : filled;
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);
23 size_t size,
size_t offset,
const char* msg)
60 bool is_sub = (arena->parent_ref != NULL);
72 fprintf(stderr,
"[ARENA ERROR] %s\n", msg);
83 fprintf(stderr,
"STEP: %s\n", msg);
98 ncplane_dim_yx(vis->
main_plane, (
unsigned*) &rows, (
unsigned*) &cols);
101 ncplane_set_fg_rgb(vis->
main_plane, 0xFFD700);
102 ncplane_printf_yx(vis->
main_plane, y++, (cols - 36) / 2,
"=== 🚀 Interactive Arena Visualizer 🚀 ===");
108 ncplane_printf_yx(vis->
main_plane, y++, 0,
"Arena Label: %s",
109 vis->
arena->debug.label ? vis->
arena->debug.label :
"Unnamed");
113 const int bar_width = cols - 25;
115 size_t total = vis->
arena->size;
126 ncplane_set_fg_rgb(vis->
main_plane, 0xADD8E6);
127 ncplane_printf_yx(vis->
main_plane, y++, 0,
"--- Arena Stats ---");
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);
139 ncplane_set_fg_rgb(vis->
main_plane, 0x90EE90);
140 ncplane_printf_yx(vis->
main_plane, y++, 0,
"--- Events (Oldest ➡️ Newest) ---");
148 int lines_for_ring = rows - y - 2;
149 if (lines_for_ring < 1)
151 int max_offset = (int) hist - lines_for_ring;
152 max_offset = max_offset < 0 ? 0 : max_offset;
156 int start = (int) hist - lines_for_ring - vis->
scroll_offset;
157 start = start < 0 ? 0 : start;
159 for (
int i = start; i < (int) hist; i++)
163 if (y >= (
unsigned) rows - 1)
175 ncplane_printf_yx(vis->
main_plane, y++, 0,
"🧩 SUB: usage=%zu, size=%zu, offset=%zu [%s]", e->
usage,
178 ncplane_printf_yx(vis->
main_plane, y++, 0,
"📌 usage=%zu, size=%zu, offset=%zu [%s]", e->
usage, e->
size,
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");
187 notcurses_render(vis->
nc);
203 uint32_t key = notcurses_get_blocking(vis->
nc, NULL);
262 struct notcurses_options opts = {
264 .loglevel = NCLOGLEVEL_SILENT,
267 vis->
nc = notcurses_init(&opts, NULL);
270 fprintf(stderr,
"[Visualizer] Failed to init notcurses\n");
275 pthread_mutex_init(&vis->
lock, NULL);
Core public API for the Arena memory allocator.
size_t arena_used(t_arena *arena)
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.
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.
t_arena_history_entry history[MAX_HISTORY]
struct ncplane * main_plane
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.