summaryrefslogtreecommitdiff
path: root/anim/src/segments.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-11 23:46:02 -0300
committerGabriel Schneider <[email protected]>2026-11-12 01:40:04 -0300
commit60e07feef401d9044db3a1c925cb9d1f6d20dac9 (patch)
tree98957ba8425193f686c8e8692cd10a10f27dbf4b /anim/src/segments.zig
parent9c0696447dbc5cf37e56aee562c7ebba4f4d4e6b (diff)
downloadcomputeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.tar.gz
computeranim-60e07feef401d9044db3a1c925cb9d1f6d20dac9.zip
canim: relations as one data-oriented subsystem; aim; the ASan pair; the introduction's chapters
From the user. Relations between panes are one subsystem: per-frame tables of places and values in struct-of-arrays, owned by the frame; the kinds one table of data; a resolver of pure passes (join, filter, priority, cut) unit-tested on hand-written tables. Ported kinds draw byte-identical frames; a synthetic map of 4000 places and 6000 values resolves in 0.56 ms against the old table's 5.7. aim, a word holding a code address tied to its row, is the first new kind, off unless a scene turns it on. The ASan pair: one overflow built plain and with ASan, recorded by instruction; the frame as bytes eight a row; the plain run goes on alone in the column, its return address aimed at main's row until the letters reach it; the fault at ret. The introduction's three chapters are built and tested, not yet exported. Annotations, rows and scrolls that changed in one frame now fade; captions stand twelve seconds at most. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNCKgYCGiANnEwcEQxa
Diffstat (limited to 'anim/src/segments.zig')
-rw-r--r--anim/src/segments.zig605
1 files changed, 605 insertions, 0 deletions
diff --git a/anim/src/segments.zig b/anim/src/segments.zig
new file mode 100644
index 0000000..d28d2dd
--- /dev/null
+++ b/anim/src/segments.zig
@@ -0,0 +1,605 @@
+//! `segments` (design/README.md, "segments"; motion.md, "The address space
+//! enlarged"; mocks 30 and 34, treatment A): the user half of the address
+//! space to true scale, a slim bar, and each cluster of mappings enlarged
+//! once, linear, a box beside it with every named band's start and end
+//! addresses and its size. Everything from the recording's mappings at one
+//! moment (`Moment.mappings`, `mapping`): nothing is placed by hand.
+//!
+//! Drawn in the program column (x 64, 880 wide), between `top` and `foot`
+//! (the column's 84 to 888, the working frame's 80 to 864: `form`); the
+//! scene plays its clocks (frames, linear): `whole` (the bar, its ends, the
+//! mappings' lines), each cluster's `enlarge`, a band's `ink` echo. `Place`
+//! is a dump's place in it: the `tie` bracket and the `link` onto the
+//! stack's box.
+const std = @import("std");
+const sk = @import("sketch");
+const anim = @import("anim");
+const zt = @import("ztrace");
+const ui = @import("ui.zig");
+const Recording = @import("Recording.zig");
+const theme = ui.theme;
+const Rect = ui.Rect;
+const Vec = ui.Vec;
+const t = theme.color.text;
+const region = theme.color.region;
+
+/// The user half: 0 to 7fffffffffff.
+pub const user_end: u64 = 1 << 47;
+
+/// Touching mappings of one file and one permission (an anonymous one right
+/// after a file's, its bss, with it).
+pub const Band = struct { lo: u64, hi: u64, prot: u64, name: []const u8, ink: u24 };
+/// What has a row of names: a file once (the program by segment, each band its own).
+pub const Unit = struct { lo: u64, hi: u64, prot: ?u64, name: []const u8, ink: u24, program: bool };
+pub const Cluster = struct { lo: u64, hi: u64, bands: [2]u16, units: [2]u16 };
+
+pub const Space = struct {
+ bands: []Band,
+ units: []Unit,
+ clusters: []Cluster,
+ program: []const u8,
+};
+
+const Kind = zt.telescope.Region;
+
+fn inkOf(kind: Kind) u24 {
+ return switch (kind) {
+ .code, .rwx => region.code_,
+ .stack => region.stack,
+ .heap => region.heap,
+ .data => region.data,
+ .rodata => region.rodata,
+ .none => region.number,
+ };
+}
+
+/// A mapping's file as a name: its path's last part, without the kernel's ` (deleted)`.
+fn nameOf(path: []const u8) []const u8 {
+ const base = if (std.mem.lastIndexOfScalar(u8, path, '/')) |k| path[k + 1 ..] else path;
+ return if (std.mem.endsWith(u8, base, " (deleted)")) base[0 .. base.len - " (deleted)".len] else base;
+}
+
+/// The address space at moment `at`: its bands, the units named, the
+/// clusters top down (highest first). A cluster is mappings with less than
+/// 16 MiB between them; a band more than eight times the rest of its
+/// cluster is a cluster of its own (the tracer's ring under libc: in the
+/// libraries' box it would leave libc 12 units tall).
+pub fn read(rec: *const Recording, at: u64, arena: std.mem.Allocator) Space {
+ const mo = rec.moment(at);
+ const maps = mo.mappings(arena) catch &.{};
+ var bands: std.ArrayList(Band) = .empty;
+ var path_buf: [512]u8 = undefined;
+ for (maps) |mp| {
+ if (mp.addr >= user_end) continue;
+ var path: []const u8 = "";
+ _ = mo.mapping(mp.addr, &path, &path_buf) catch null;
+ const name = arena.dupe(u8, nameOf(path)) catch "";
+ if (bands.items.len > 0) {
+ const last = &bands.items[bands.items.len - 1];
+ if (last.hi == mp.addr and last.prot == mp.prot and (std.mem.eql(u8, last.name, name) or (name.len == 0 and last.name.len > 0 and last.name[0] != '['))) {
+ last.hi = mp.addr + mp.len;
+ continue;
+ }
+ }
+ bands.append(arena, .{ .lo = mp.addr, .hi = mp.addr + mp.len, .prot = mp.prot, .name = name, .ink = inkOf(Kind.of(mp, name)) }) catch break;
+ }
+ // The program: the file the pc is in.
+ var pc_path: []const u8 = "";
+ _ = mo.mapping(rec.register(at, "rip"), &pc_path, &path_buf) catch null;
+ const program = arena.dupe(u8, nameOf(pc_path)) catch "";
+
+ // Units: the program's bands each, every other file once.
+ var units: std.ArrayList(Unit) = .empty;
+ var k: usize = 0;
+ while (k < bands.items.len) {
+ const b = bands.items[k];
+ if (b.name.len == 0) {
+ k += 1;
+ continue;
+ }
+ if (std.mem.eql(u8, b.name, program)) {
+ units.append(arena, .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = true }) catch break;
+ k += 1;
+ continue;
+ }
+ var u: Unit = .{ .lo = b.lo, .hi = b.hi, .prot = b.prot, .name = b.name, .ink = b.ink, .program = false };
+ k += 1;
+ while (k < bands.items.len and bands.items[k].lo == u.hi and std.mem.eql(u8, bands.items[k].name, u.name)) : (k += 1) {
+ u.hi = bands.items[k].hi;
+ if (u.prot != bands.items[k].prot) {
+ u.prot = null;
+ u.ink = region.rodata;
+ }
+ }
+ units.append(arena, u) catch break;
+ }
+
+ // Clusters, low to high first, then turned top down.
+ var clusters: std.ArrayList(Cluster) = .empty;
+ var first: usize = 0;
+ for (bands.items, 0..) |b, i| {
+ const last = i + 1 == bands.items.len or bands.items[i + 1].lo - b.hi >= 16 << 20;
+ if (!last) continue;
+ splitOff(arena, &clusters, bands.items, first, i + 1);
+ first = i + 1;
+ }
+ std.mem.reverse(Cluster, clusters.items);
+ for (clusters.items) |*c| {
+ var lo_u: u16 = @intCast(units.items.len);
+ var hi_u: u16 = 0;
+ for (units.items, 0..) |u, i| if (u.lo >= c.lo and u.hi <= c.hi) {
+ lo_u = @min(lo_u, @as(u16, @intCast(i)));
+ hi_u = @max(hi_u, @as(u16, @intCast(i + 1)));
+ };
+ c.units = if (hi_u > lo_u) .{ lo_u, hi_u } else .{ 0, 0 };
+ }
+ return .{ .bands = bands.items, .units = units.items, .clusters = clusters.items, .program = program };
+}
+
+/// Bands `a` to `b` as a cluster, or three where one band outweighs the rest eightfold.
+fn splitOff(arena: std.mem.Allocator, out: *std.ArrayList(Cluster), bands: []const Band, a: usize, b: usize) void {
+ var big = a;
+ for (a..b) |i| if (bands[i].hi - bands[i].lo > bands[big].hi - bands[big].lo) {
+ big = i;
+ };
+ const whole = bands[b - 1].hi - bands[a].lo;
+ const len = bands[big].hi - bands[big].lo;
+ if (b - a > 1 and len > 8 * (whole - len)) {
+ for ([_][2]usize{ .{ a, big }, .{ big, big + 1 }, .{ big + 1, b } }) |r| if (r[1] > r[0]) {
+ out.append(arena, .{ .lo = bands[r[0]].lo, .hi = bands[r[1] - 1].hi, .bands = .{ @intCast(r[0]), @intCast(r[1]) }, .units = .{ 0, 0 } }) catch {};
+ };
+ return;
+ }
+ out.append(arena, .{ .lo = bands[a].lo, .hi = bands[b - 1].hi, .bands = .{ @intCast(a), @intCast(b) }, .units = .{ 0, 0 } }) catch {};
+}
+
+/// A size as the names give it: `4 KiB`, `132 KiB`, `2.1 MiB`, `64 MiB`, `128 TiB`.
+pub fn size(buf: []u8, bytes: u64) []const u8 {
+ const units = [_][]const u8{ "KiB", "MiB", "GiB", "TiB" };
+ var v: f64 = @as(f64, @floatFromInt(bytes)) / 1024;
+ var u: usize = 0;
+ while (v >= 1024 and u + 1 < units.len) : (u += 1) v /= 1024;
+ if (u > 0 and v < 10 and @abs(v - @round(v)) >= 0.05) return std.fmt.bufPrint(buf, "{d:.1} {s}", .{ v, units[u] }) catch "";
+ return std.fmt.bufPrint(buf, "{d} {s}", .{ @as(u64, @intFromFloat(@round(v))), units[u] }) catch "";
+}
+
+fn perms(prot: u64) [3]u8 {
+ return .{ if (prot & 1 != 0) 'r' else '-', if (prot & 2 != 0) 'w' else '-', if (prot & 4 != 0) 'x' else '-' };
+}
+
+/// Rows `pitch` apart, each as near its target as the others let it
+/// (crowded ones pooled round their mean), in order, centres in `lo` to `hi`.
+pub fn pool(targets: []const f32, out: []f32, lo: f32, hi: f32, pitch: f32) void {
+ const n = targets.len;
+ if (n == 0) return;
+ var sums: [64]f32 = undefined;
+ var counts: [64]f32 = undefined;
+ var nb: usize = 0;
+ for (targets, 0..) |target, i| {
+ sums[nb] = target - pitch * @as(f32, @floatFromInt(i));
+ counts[nb] = 1;
+ nb += 1;
+ while (nb > 1 and sums[nb - 2] / counts[nb - 2] > sums[nb - 1] / counts[nb - 1]) {
+ sums[nb - 2] += sums[nb - 1];
+ counts[nb - 2] += counts[nb - 1];
+ nb -= 1;
+ }
+ }
+ var i: usize = 0;
+ const top = @max(hi - pitch * @as(f32, @floatFromInt(n - 1)), lo);
+ for (sums[0..nb], counts[0..nb]) |s, c| for (0..@intFromFloat(c)) |_| {
+ out[i] = std.math.clamp(s / c, lo, top) + pitch * @as(f32, @floatFromInt(i));
+ i += 1;
+ };
+}
+
+// ------------------------------------------------------------- the drawing
+
+/// The program column's measures (mock 30): the bar 20 wide (inside 16),
+/// the boxes 108 wide inside, the leaders' room, then the names.
+const m = struct {
+ const bar = 160;
+ const bar_w = 20;
+ const box = 304;
+ const box_w = 108;
+ const leader = 8;
+ const address = 476;
+ const indent = 24;
+ const size_end = 632;
+ const name = 656;
+ const pitch = 24;
+ const mono = theme.size.mono.context;
+ /// Box heights: a cluster of one band; the rest share what is left.
+ const one = 72;
+ const gap_min = 24;
+};
+
+/// Frames of `enlarge` (motion.md): the line framed, the ride, the page lines, the names.
+const e = struct {
+ const frame = 6;
+ const ride = 72;
+ const pages = 12;
+ const names = 90;
+ const apart = 2;
+ const leader = 6;
+ const fade = 12;
+};
+
+/// Under the camera (nearer than x1), nothing drawn under the stage (y 888):
+/// the caption's band is the caption's. Returns the clip it replaced.
+pub fn clipStage(f: *sk.Frame, placed: anim.Placed) ??sk.Rect {
+ const w = @floor(f.in.window_width);
+ const h = @floor(f.in.window_height);
+ const one = @min(w / theme.canvas.width, h / theme.canvas.height);
+ if (one <= 0 or placed.scale / one < 1.01) return null;
+ const was = ui.bound;
+ const stage: sk.Rect = .{ .x = 0, .y = 0, .w = w, .h = (h - theme.canvas.height * one) / 2 + (theme.canvas.stage.y + theme.canvas.stage.h) * one };
+ ui.bound = stage;
+ f.clip(stage);
+ return was;
+}
+
+pub fn unclipStage(f: *sk.Frame, was: ??sk.Rect) void {
+ const old = was orelse return;
+ ui.bound = old;
+ if (old) |r| f.clip(r) else f.clip(null);
+}
+
+pub const Segments = struct {
+ rec: *const Recording,
+ at: u64,
+ x: f32 = theme.layout.program.x,
+ /// 0: the column's height (84 to 888); 1: the working frame's (80 to 864).
+ form: anim.Value,
+ /// Frames into the whole's build (negative: not begun).
+ whole: anim.Value,
+ /// Frames into each cluster's `enlarge`, top down (negative: not begun).
+ rides: [6]anim.Value,
+ /// `ink`'s echoes: the band holding `addr` framed as read, `shows` of it.
+ echoes: [16]Echo = undefined,
+ n_echoes: usize = 0,
+
+ pub const Echo = struct { addr: u64, shows: anim.Value };
+
+ pub fn init(s: *anim.Frame, rec: *const Recording, at: u64) Segments {
+ var rides: [6]anim.Value = undefined;
+ for (&rides) |*r| r.* = s.value(-1);
+ return .{ .rec = rec, .at = at, .form = s.value(0), .whole = s.value(-1), .rides = rides };
+ }
+
+ /// The bar's top and foot at `form` (0 the column, 1 the working frame).
+ fn span(form: f32) [2]f32 {
+ const k = theme.ease(.glide, form);
+ return .{ std.math.lerp(84, 80, k), std.math.lerp(888, 864, k) };
+ }
+
+ pub const Layout = struct {
+ space: Space,
+ top: f32,
+ foot: f32,
+ /// Each cluster's box, as it lands (inside its frame).
+ boxes: [6]Rect,
+ /// Each cluster's line on the whole.
+ lines: [6]f32,
+ };
+
+ /// Where everything is at `form` (`Segments.form`'s value).
+ pub fn layout(g: *const Segments, form: f32, arena: std.mem.Allocator) Layout {
+ const ends = span(form);
+ const space = read(g.rec, g.at, arena);
+ var out: Layout = .{ .space = space, .top = ends[0], .foot = ends[1], .boxes = undefined, .lines = undefined };
+ const n = @min(space.clusters.len, 6);
+ // Heights: a cluster of one band 72; the program's as its rows; the
+ // libraries' (the most units but the program's) what is left.
+ var heights: [6]f32 = @splat(0);
+ var fixed: f32 = 0;
+ var rest: ?usize = null;
+ for (space.clusters[0..n], 0..) |c, i| {
+ const lines: f32 = @floatFromInt(2 * (c.units[1] - c.units[0]) + 1);
+ const has_program = for (space.units[c.units[0]..c.units[1]]) |u| {
+ if (u.program) break true;
+ } else false;
+ if (c.bands[1] - c.bands[0] == 1 or lines <= 3) heights[i] = m.one else if (has_program) heights[i] = m.pitch * lines - 12 else if (rest == null) {
+ rest = i;
+ continue;
+ } else heights[i] = m.pitch * lines - 12;
+ fixed += heights[i];
+ }
+ const gaps: f32 = @floatFromInt(@max(n, 1) - 1);
+ const room = ends[1] - ends[0];
+ var gap: f32 = 44;
+ if (rest) |r| heights[r] = @max(room - fixed - gap * gaps, m.one) else gap = @max((room - fixed) / @max(gaps, 1), m.gap_min);
+ var y = ends[0];
+ for (0..n) |i| {
+ out.boxes[i] = .{ .x = g.x + m.box, .y = y, .w = m.box_w, .h = heights[i] };
+ y += heights[i] + gap;
+ // The lines on the whole: the top clusters 2 apart under its top, the foot's over its foot.
+ out.lines[i] = if (space.clusters[i].lo > user_end / 2) ends[0] + 2 + 2 * @as(f32, @floatFromInt(i)) else ends[1] - 4 - 2 * @as(f32, @floatFromInt(n - 1 - i));
+ }
+ return out;
+ }
+
+ /// Where `addr` is in its cluster's box once it has landed; null outside every box.
+ pub fn place(l: Layout, addr: u64) ?Vec {
+ for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) {
+ const b = l.boxes[i];
+ return .{ b.x + b.w / 2, b.y + b.h * @as(f32, @floatCast(@as(f64, @floatFromInt(c.hi - addr)) / @as(f64, @floatFromInt(c.hi - c.lo)))) };
+ };
+ return null;
+ }
+
+ pub fn draw(g: *const Segments, f: *sk.Frame, placed: anim.Placed) void {
+ const u: ui.Ui = .{ .f = f, .at = placed };
+ const l = g.layout(placed.value(g.form), f.scratch);
+ const whole = placed.value(g.whole);
+ if (whole < 0) return;
+ const was = clipStage(f, placed);
+ defer unclipStage(f, was);
+ const bar_x = g.x + m.bar;
+ const h = l.foot - l.top;
+ // The bar, from the top down (72 f), framed 2 in `dim`, coming in over its first 6 f.
+ const grown = h * theme.ease(.standard, std.math.clamp(whole / 72, 0, 1));
+ u.faded(std.math.clamp(whole / 6, 0, 1)).frame(.{ .x = bar_x, .y = l.top, .w = m.bar_w, .h = @max(grown, 4) }, t.dim, theme.stroke.rule);
+ // Its ends' addresses and its size (18 f), ranged right 12 left of it.
+ const ends_in = std.math.clamp((whole - 72) / 18, 0, 1);
+ if (ends_in > 0) {
+ const v = u.faded(ends_in);
+ var buf: [16]u8 = undefined;
+ for ([_]struct { []const u8, f32 }{ .{ "7fffffffffff", l.top }, .{ "0", l.foot }, .{ size(&buf, user_end), (l.top + l.foot) / 2 } }) |row|
+ v.text(.mono, m.mono, bar_x - 12 - v.width(.mono, m.mono, row[0]), row[1] + 7.2, row[0], t.dim);
+ }
+ // Every mapping's line inside its end (12 f each, across 72 f).
+ const n = @min(l.space.clusters.len, 6);
+ for (0..n) |i| {
+ const shows = std.math.clamp((whole - 90 - 60 * @as(f32, @floatFromInt(i)) / @as(f32, @floatFromInt(@max(n, 2) - 1))) / 12, 0, 1);
+ if (shows > 0) u.faded(shows).fill(.{ .x = bar_x + 2, .y = l.lines[i], .w = m.bar_w - 4, .h = 2 }, biggest(l.space, l.space.clusters[i]).ink);
+ }
+ for (0..n) |i| g.enlarged(u, l, i, placed.value(g.rides[i]));
+ for (g.echoes[0..g.n_echoes]) |echo| g.echoed(u, l, echo.addr, placed.value(echo.shows));
+ }
+
+ /// The ink of a cluster's largest band (its line on the whole).
+ fn biggest(space: Space, c: Cluster) Band {
+ var best = space.bands[c.bands[0]];
+ for (space.bands[c.bands[0]..c.bands[1]]) |b| if (b.hi - b.lo > best.hi - best.lo) {
+ best = b;
+ };
+ return best;
+ }
+
+ /// Cluster `i`'s `enlarge`, `clock` frames in.
+ fn enlarged(g: *const Segments, line_u: ui.Ui, l: Layout, i: usize, clock: f32) void {
+ if (clock < 0) return;
+ const c = l.space.clusters[i];
+ const land = l.boxes[i];
+ const bar_r = g.x + m.bar + m.bar_w;
+ const line_y = l.lines[i] + 1;
+ // The line framed bright (2 units, 3 out), going as the box lands.
+ const bright = std.math.clamp(clock / e.frame, 0, 1) * (1 - std.math.clamp((clock - 66) / 12, 0, 1));
+ if (bright > 0) line_u.faded(bright).frame(.{ .x = g.x + m.bar - 1, .y = line_y - 5, .w = m.bar_w + 2, .h = 10 }, t.bright, theme.stroke.rule);
+ // The ride: the box's left corners along the magnification's lines.
+ const p = theme.ease(.glide, std.math.clamp((clock - e.frame) / e.ride, 0, 1));
+ if (p <= 0) return;
+ const box: Rect = .{
+ .x = std.math.lerp(bar_r, land.x, p),
+ .y = std.math.lerp(line_y, land.y, p),
+ .w = std.math.lerp(m.bar_w - 4, land.w, p),
+ .h = std.math.lerp(0, land.h, p),
+ };
+ // Out of the line: the box comes in over the ride's first frames, never in one.
+ const u = line_u.faded(std.math.clamp(p * 8, 0, 1));
+ u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y - 1 }, theme.color.rule, theme.stroke.rule);
+ u.line(.{ bar_r, line_y }, .{ box.x - 2, box.y + box.h + 1 }, theme.color.rule, theme.stroke.rule);
+ // Its bands at its scale all the way: a mapping a 2-unit line until it is taller.
+ const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
+ const pages = std.math.clamp((clock - e.frame - e.ride) / e.pages, 0, 1);
+ u.clip(.{ .x = box.x, .y = box.y - 1, .w = box.w, .h = box.h + 2 });
+ for (l.space.bands[c.bands[0]..c.bands[1]], c.bands[0]..) |b, k| {
+ const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
+ const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2);
+ u.fill(.{ .x = box.x, .y = top, .w = box.w, .h = bh }, ui.mix(theme.color.page, b.ink, 0.3));
+ // Between two bands of one file a page line, where both are 6 tall.
+ if (k + 1 < c.bands[1] and pages > 0) {
+ const below = l.space.bands[k + 1];
+ if (below.hi == b.lo and std.mem.eql(u8, below.name, b.name) and b.name.len > 0 and bh >= 6 and @as(f32, @floatFromInt(below.hi - below.lo)) * scale >= 6)
+ u.faded(pages).fill(.{ .x = box.x, .y = top + bh - 1, .w = box.w, .h = 2 }, theme.color.page);
+ }
+ }
+ u.unclip();
+ u.frame(.{ .x = box.x - 2, .y = box.y - 2, .w = box.w + 4, .h = box.h + 4 }, t.dim, theme.stroke.rule);
+ // Its names, rows top down, once it has landed.
+ g.names(u, l, i, clock - e.names, scale);
+ }
+
+ const Row = struct { kind: enum { edge, unit }, addr: u64, unit: usize, target: f32, ink: u24 };
+
+ /// Cluster `i`'s rows of names: an edge of every named unit, each unit
+ /// between its edges, top down (highest address first).
+ fn rows(l: Layout, i: usize, arena: std.mem.Allocator) []Row {
+ const c = l.space.clusters[i];
+ const box = l.boxes[i];
+ const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
+ var out: std.ArrayList(Row) = .empty;
+ var k = c.units[1];
+ while (k > c.units[0]) {
+ k -= 1;
+ const un = l.space.units[k];
+ const tall = @as(f32, @floatFromInt(un.hi - un.lo)) * scale;
+ if (!un.program and tall < 6) continue;
+ const yh = box.y + @as(f32, @floatFromInt(c.hi - un.hi)) * scale;
+ const yl = box.y + @as(f32, @floatFromInt(c.hi - un.lo)) * scale;
+ const last = if (out.items.len > 0) out.items[out.items.len - 1] else null;
+ if (last == null or last.?.kind != .edge or last.?.addr != un.hi) out.append(arena, .{ .kind = .edge, .addr = un.hi, .unit = k, .target = yh, .ink = un.ink }) catch {};
+ out.append(arena, .{ .kind = .unit, .addr = un.lo, .unit = k, .target = std.math.clamp((yh + yl) / 2, box.y, box.y + box.h), .ink = un.ink }) catch {};
+ out.append(arena, .{ .kind = .edge, .addr = un.lo, .unit = k, .target = yl, .ink = un.ink }) catch {};
+ }
+ // An edge two units share takes the ink of the one that starts there.
+ for (out.items, 0..) |*r, j| if (r.kind == .edge and j + 1 < out.items.len and out.items[j + 1].kind == .unit) {
+ r.ink = l.space.units[out.items[j + 1].unit].ink;
+ };
+ return out.items;
+ }
+
+ /// Where cluster `i`'s rows may stand: its share of the column, down to the gaps' middles.
+ fn share(l: Layout, i: usize) [2]f32 {
+ const n = @min(l.space.clusters.len, 6);
+ const lo = if (i == 0) l.top else (l.boxes[i - 1].y + l.boxes[i - 1].h + l.boxes[i].y) / 2 + 12;
+ const hi = if (i + 1 == n) l.foot + 12 else (l.boxes[i].y + l.boxes[i].h + l.boxes[i + 1].y) / 2 - 12;
+ return .{ lo, hi };
+ }
+
+ fn names(g: *const Segments, u: ui.Ui, l: Layout, i: usize, clock: f32, scale: f32) void {
+ if (clock < 0) return;
+ const all = rows(l, i, u.f.scratch);
+ if (all.len == 0) return;
+ var targets: [64]f32 = undefined;
+ var ys: [64]f32 = undefined;
+ const n = @min(all.len, 64);
+ for (all[0..n], 0..) |r, k| targets[k] = r.target;
+ const room = share(l, i);
+ pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch);
+ const box = l.boxes[i];
+ const right = box.x + box.w + 2;
+ const ax = g.x + m.address;
+ var buf: [32]u8 = undefined;
+ var first_program: ?usize = null;
+ var last_program: usize = 0;
+ for (all[0..n], 0..) |r, k| {
+ const at = clock - @as(f32, @floatFromInt(k)) * e.apart;
+ if (at <= 0) break;
+ const y = ys[k];
+ const un = l.space.units[r.unit];
+ switch (r.kind) {
+ .edge => {
+ const drawn = std.math.clamp(at / e.leader, 0, 1);
+ u.route(&.{ .{ right, r.target }, .{ right + m.leader, r.target }, .{ ax - 20, y }, .{ ax - 8, y } }, theme.color.rule, .{ .width = theme.stroke.rule, .head = null, .progress = drawn, .radius = 0 });
+ const shows = std.math.clamp((at - e.leader) / e.fade, 0, 1);
+ if (shows > 0) _ = u.faded(shows).address(ax, y + 7.2, r.addr, .{ .ink = ui.rgb(r.ink) });
+ },
+ .unit => {
+ const v = u.faded(std.math.clamp(at / e.fade, 0, 1));
+ if (un.prot) |p| v.text(.mono, m.mono, ax + m.indent, y + 7.2, &perms(p), t.dim);
+ const sz = size(&buf, un.hi - un.lo);
+ v.text(.mono, m.mono, g.x + m.size_end - v.width(.mono, m.mono, sz), y + 7.2, sz, t.dim);
+ if (un.program) {
+ if (first_program == null) first_program = k;
+ last_program = k;
+ } else v.text(.mono, m.mono, g.x + m.name, y + 7.2, un.name, t.primary);
+ // A file whose permissions differ: each part's written inside its band, where it is 28 tall.
+ if (un.prot == null) g.insides(v, l, i, un, scale);
+ },
+ }
+ }
+ // The program's bracket and name, with its last row.
+ if (first_program) |a| {
+ const at = clock - @as(f32, @floatFromInt(last_program)) * e.apart;
+ const v = u.faded(std.math.clamp(at / e.fade, 0, 1));
+ const bx = g.x + m.name - 14;
+ v.bracket(bx, ys[a] - 8, ys[last_program] + 8, theme.color.rule, theme.metric.bracket.quiet, -1, false);
+ v.text(.mono, m.mono, g.x + m.name, (ys[a] + ys[last_program]) / 2 + 7.2, l.space.program, t.primary);
+ }
+ }
+
+ fn insides(_: *const Segments, v: ui.Ui, l: Layout, i: usize, un: Unit, scale: f32) void {
+ const c = l.space.clusters[i];
+ const box = l.boxes[i];
+ for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (b.lo >= un.lo and b.hi <= un.hi) {
+ const bh = @as(f32, @floatFromInt(b.hi - b.lo)) * scale;
+ if (bh < 28) continue;
+ const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
+ v.label(.mono, m.mono, .{ .x = box.x, .y = top, .w = box.w, .h = bh }, .center, &perms(b.prot), t.primary);
+ };
+ }
+
+ /// `ink`'s echo: the band holding `addr` framed as read inside its box
+ /// (2 units, 4 in from its sides, 6 over and under), its row of names with it.
+ fn echoed(g: *const Segments, u: ui.Ui, l: Layout, addr: u64, shows: f32) void {
+ if (shows <= 0) return;
+ const v = u.faded(shows);
+ const read_ink = theme.mark(.read).solid;
+ for (l.space.clusters, 0..) |c, i| if (i < 6 and addr >= c.lo and addr < c.hi) {
+ const box = l.boxes[i];
+ const scale = box.h / @as(f32, @floatFromInt(c.hi - c.lo));
+ for (l.space.bands[c.bands[0]..c.bands[1]]) |b| if (addr >= b.lo and addr < b.hi) {
+ const top = box.y + @as(f32, @floatFromInt(c.hi - b.hi)) * scale;
+ const bh = @max(@as(f32, @floatFromInt(b.hi - b.lo)) * scale, 2);
+ v.frame(.{ .x = box.x + 4, .y = top - 6, .w = box.w - 8, .h = bh + 12 }, read_ink, theme.stroke.rule);
+ };
+ // Its row of names: the unit holding it.
+ const all = rows(l, i, u.f.scratch);
+ var targets: [64]f32 = undefined;
+ var ys: [64]f32 = undefined;
+ const n = @min(all.len, 64);
+ for (all[0..n], 0..) |r, k| targets[k] = r.target;
+ const room = share(l, i);
+ pool(targets[0..n], ys[0..n], room[0], room[1], m.pitch);
+ for (all[0..n], 0..) |r, k| if (r.kind == .unit) {
+ const un = l.space.units[r.unit];
+ if (addr < un.lo or addr >= un.hi) continue;
+ const x0 = g.x + m.address + m.indent - 6;
+ v.frame(.{ .x = x0, .y = ys[k] - 12, .w = g.x + m.size_end + 6 - x0, .h = 24 }, read_ink, theme.stroke.rule);
+ };
+ };
+ }
+};
+
+/// A dump's place (design/README.md, `segments`, "a dump's place"): the
+/// `tie` bracket on its rows in the stack's ink (6 f), then the `link`
+/// from the bracket's top up into the stage's top margin (y 56), across,
+/// and down onto the stack's box at the dump's address, head first (36 f,
+/// `standard`); the stack's band framed bright as it lands (6 f in, 12
+/// out); then both quiet to standing (24 f, 60% toward the page).
+pub const Place = struct {
+ segments: *const Segments,
+ addr: u64,
+ /// The bracket: its x and its rows' top and foot.
+ x: f32,
+ y0: f32,
+ y1: f32,
+ /// Frames into the tie (negative: not begun); frames into its quieting.
+ clock: anim.Value,
+ quiet: anim.Value,
+
+ pub fn draw(p: *const Place, f: *sk.Frame, placed: anim.Placed) void {
+ const clock = placed.value(p.clock);
+ if (clock < 0) return;
+ const was = clipStage(f, placed);
+ defer unclipStage(f, was);
+ const u: ui.Ui = .{ .f = f, .at = placed };
+ const k = 1 - (1 - theme.metric.arc.standing) * theme.ease(.glide, std.math.clamp(placed.value(p.quiet) / 24, 0, 1));
+ const ink = ui.mix(theme.color.page, region.stack, k);
+ const v = u.faded(std.math.clamp(clock / 6, 0, 1));
+ v.bracket(p.x, p.y0, p.y1, ink, theme.metric.bracket.tie, 1, false);
+ const l = p.segments.layout(placed.value(p.segments.form), f.scratch);
+ const to = Segments.place(l, p.addr) orelse return;
+ const top: f32 = 56;
+ const drawn = theme.ease(.standard, std.math.clamp((clock - 6) / 36, 0, 1));
+ u.route(&.{ .{ p.x, p.y0 }, .{ p.x, top }, .{ to[0], top }, .{ to[0], to[1] } }, ink, .{ .width = theme.stroke.tie.link, .head = .{ 10, 10 }, .progress = drawn, .radius = 8 });
+ // The stack's band framed bright as the head lands.
+ const bright = std.math.clamp((clock - 42) / 6, 0, 1) * (1 - std.math.clamp((clock - 48) / 12, 0, 1));
+ if (bright > 0) for (l.space.clusters, 0..) |c, i| if (i < 6 and p.addr >= c.lo and p.addr < c.hi) {
+ const b = l.boxes[i];
+ u.faded(bright).frame(.{ .x = b.x - 5, .y = b.y - 5, .w = b.w + 10, .h = b.h + 10 }, t.bright, theme.stroke.line);
+ };
+ }
+};
+
+test "sizes as the names give them" {
+ var buf: [16]u8 = undefined;
+ try std.testing.expectEqualStrings("4 KiB", size(&buf, 4096));
+ try std.testing.expectEqualStrings("132 KiB", size(&buf, 0x21000));
+ try std.testing.expectEqualStrings("2.1 MiB", size(&buf, 0x223000));
+ try std.testing.expectEqualStrings("64 MiB", size(&buf, 0x4002000));
+ try std.testing.expectEqualStrings("128 TiB", size(&buf, user_end));
+}
+
+test "rows pooled round their mean, in order, inside their room" {
+ var ys: [4]f32 = undefined;
+ pool(&.{ 100, 100, 100, 300 }, &ys, 0, 1000, 24);
+ try std.testing.expectEqual(@as(f32, 76), ys[0]);
+ try std.testing.expectEqual(@as(f32, 124), ys[2]);
+ try std.testing.expectEqual(@as(f32, 300), ys[3]);
+ pool(&.{ 0, 0, 0, 0 }, &ys, 50, 1000, 24);
+ try std.testing.expectEqual(@as(f32, 50), ys[0]);
+ try std.testing.expectEqual(@as(f32, 122), ys[3]);
+}