summaryrefslogtreecommitdiff
path: root/anim/src/heap.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-10 14:48:26 -0300
committerGabriel Schneider <[email protected]>2026-11-10 15:14:31 -0300
commit7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb (patch)
treea6add92cd4dcbbf4c31cd1ce989d99b190258353 /anim/src/heap.zig
parented46c51fe127377eb1fde8dee83da984e0f61860 (diff)
downloadcomputeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.tar.gz
computeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.zip
canim: animations of recordings, as a debugger's whole context with a lens beside it
A package of its own (anim/), on sketchbook's anim module and pardes's syntax. A scene opens a recording, takes a cursor into it, and declares blocks that read the moment the cursor is at: the state column (registers each followed as a pointer and inked by where it points, the stack telescope, the backtrace), the program column (the whole source, the disassembly in address order, and the scene's lens: a node-link list over a hexdump of its heap, array cells, a call tree, a syscall crossing), and ties between them. Motion is a step told as a sentence (the line fires, its effects cascade a level at a time, the mark moves on last), timed by what the step contains; walk, hurry and sprint pace a loop. Five scenes from real recordings of fib, a linked list, a sort and syscalls. First drafts, by the agent's own handover: the frame of the design's second revision, the hexdump lens and the ties; no disassembly annotations, no gallery, scenes not yet reviewed against the mocks.
Diffstat (limited to 'anim/src/heap.zig')
-rw-r--r--anim/src/heap.zig105
1 files changed, 105 insertions, 0 deletions
diff --git a/anim/src/heap.zig b/anim/src/heap.zig
new file mode 100644
index 0000000..e9a0dcf
--- /dev/null
+++ b/anim/src/heap.zig
@@ -0,0 +1,105 @@
+//! The heap, to scale: a strip along the addresses malloc handed out, each
+//! live allocation a green block with the name of what points at it, each
+//! freed one hollow and dashed, the allocator's own bytes between them
+//! filled, and the bytes this step wrote in vermilion where they are.
+//! Reads `Moment.heap`, and the recording's index of every allocation made.
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const ui = @import("ui.zig");
+const cursors = @import("cursor.zig");
+const Cursor = cursors.Cursor;
+const Recording = @import("Recording.zig");
+const objects = @import("objects.zig");
+const theme = ui.theme;
+const Rect = ui.Rect;
+const t = theme.color.text;
+
+pub const Heap = struct {
+ cursor: Cursor,
+ box: Rect,
+ /// Variables each with a pigment (by its place here), as elsewhere.
+ track: []const []const u8 = &.{},
+ focus: bool = false,
+
+ pub fn draw(h: *const Heap, f: *sk.Frame, at: anim.Placed) void {
+ const u: ui.Ui = .{ .f = f, .at = at };
+ const now = h.cursor.now(at);
+ const rec = h.cursor.rec;
+ // The heap is memory: what a step did to it shows when memory strikes.
+ const moment = now.shown(.memory);
+ const at_moment = now.stop(.memory);
+ // Not "heap": a grid of the same bytes beside it has that name.
+ const body = u.panel(h.box.w, h.box.h, "heap map", .{ .focus = h.focus, .note = "to scale ยท low addresses on the left" });
+ if (rec.allocations.len == 0) return;
+ // The whole run's span, so nothing moves as chunks come and go.
+ var lo: u64 = std.math.maxInt(u64);
+ var hi: u64 = 0;
+ for (rec.allocations) |a| {
+ lo = @min(lo, a.ptr);
+ hi = @max(hi, a.ptr + a.size);
+ }
+ lo -|= 16;
+ const pad = theme.metric.panel.pad_x + 8;
+ const scale = @min(theme.metric.heap.units_per_byte, (body.w - 2 * pad - 60) / @as(f32, @floatFromInt(hi - lo)));
+ const strip_h: f32 = if (body.h < 220) theme.metric.heap.strip_h_stacked else theme.metric.heap.strip_h;
+ const y = body.y + (body.h - strip_h) / 2 - 6;
+ const live = moment.heap(f.scratch) catch return;
+
+ // Never used: a rule frame. The allocator's own bytes, filled:
+ // whatever lies between what it has handed out so far.
+ var used = lo;
+ for (rec.allocations) |a| {
+ if (a.moment <= at_moment) used = @max(used, a.ptr + a.size);
+ }
+ u.frame(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(hi - lo)) * scale, .h = strip_h }, theme.color.rule, theme.stroke.rule);
+ u.fill(.{ .x = pad, .y = y, .w = @as(f32, @floatFromInt(used - lo)) * scale, .h = strip_h }, theme.color.rule);
+ // Where it goes on.
+ for (0..3) |i| u.fill(.{ .x = pad + @as(f32, @floatFromInt(hi - lo)) * scale + 16 + @as(f32, @floatFromInt(i)) * 14, .y = y + strip_h / 2 - 3, .w = 6, .h = 6 }, t.faint);
+
+ for (rec.allocations, 0..) |a, i| {
+ if (a.moment > at_moment) continue;
+ // The newest allocation at this address is the one to draw.
+ const newest = for (rec.allocations[i + 1 ..]) |later| {
+ if (later.ptr == a.ptr and later.moment <= at_moment) break false;
+ } else true;
+ if (!newest) continue;
+ const r: Rect = .{ .x = pad + @as(f32, @floatFromInt(a.ptr - lo)) * scale, .y = y, .w = @as(f32, @floatFromInt(a.size)) * scale - 2, .h = strip_h };
+ const is_live = for (live) |l| {
+ if (l.ptr == a.ptr and l.born == a.event) break true;
+ } else false;
+ u.fill(r, theme.color.page);
+ if (is_live) {
+ const mark = theme.mark(.allocated);
+ const fresh = a.moment > now.stops[1];
+ const p = if (fresh) now.moving(.memory, 0, theme.time.quick) else 1;
+ u.faded(p).fill(r, mark.ember);
+ u.faded(p).frame(r, mark.solid, theme.stroke.line);
+ // The bytes this step wrote, where they are.
+ var b: u64 = 0;
+ while (b < a.size) : (b += 8) {
+ // The same strike as the words beside it, at scale: no arm, no pulse.
+ var heat = now.written(a.ptr + b, @min(8, a.size - b), .memory, 0);
+ heat.arm = 0;
+ heat.out = 0;
+ if (heat.level > 0) _ = u.written(.{ .x = r.x + @as(f32, @floatFromInt(b)) * scale + 3, .y = y + 3, .w = @as(f32, @floatFromInt(@min(8, a.size - b))) * scale - 4, .h = strip_h - 6 }, heat, null);
+ }
+ // Who points at it.
+ var x = r.x + 6;
+ for (objects.pointing(f.scratch, rec, moment, a.ptr)) |v| {
+ const name = rec.name(v.name);
+ const w = u.width(.mono, theme.size.mono.small, name);
+ if (x + w > r.x + r.w - 4) break;
+ const tracked = for (h.track, 0..) |want, k| {
+ if (std.mem.eql(u8, want, name)) break k;
+ } else null;
+ if (tracked) |k| u.fill(.{ .x = x - 4, .y = y + strip_h - 36, .w = w + 8, .h = 30 }, theme.color.pigment[k % theme.color.pigment.len]);
+ u.text(.mono, theme.size.mono.small, x, y + strip_h - 12, name, t.primary);
+ x += w + 12;
+ }
+ } else u.dashedFrame(r, theme.mark(.freed).solid, theme.stroke.line);
+ // Its address under it: the end that tells chunks apart.
+ u.text(.mono, theme.size.mono.small, r.x, y + strip_h + 30, u.print("{x:0>4}", .{a.ptr & 0xffff}), t.faint);
+ }
+ }
+};