diff options
Diffstat (limited to 'anim/scenes/heap.zig')
| -rw-r--r-- | anim/scenes/heap.zig | 114 |
1 files changed, 75 insertions, 39 deletions
diff --git a/anim/scenes/heap.zig b/anim/scenes/heap.zig index e7f70f7..544ae91 100644 --- a/anim/scenes/heap.zig +++ b/anim/scenes/heap.zig @@ -133,8 +133,7 @@ const Lists = struct { pub fn draw(l: *const Lists, f: *sk.Frame, placed: anim.Placed) void { const box = l.stage.box(placed, .band); if (box.h < cm.tag + 24) return; - const u = canim.ui.Ui{ .f = f, .at = placed }; - const v = u.moved(box.x, box.y).faded(placed.value(l.stage.drawing)); + const v = canim.panelAt(f, placed, box).faded(placed.value(l.stage.drawing)); v.clip(.{ .x = 0, .y = 0, .w = box.w, .h = box.h }); defer v.unclip(); const now = l.cursor.now(placed); @@ -220,41 +219,62 @@ const Lists = struct { const xy = canim.ui.Vec{ std.math.lerp(at(s0)[0], at(s1)[0], p), std.math.lerp(at(s0)[1], at(s1)[1], p) }; const shows: f32 = if (from == null) after(now, 0, 12) else if (to == null) 1 - after(now, 0, 12) else 1; const nv = v.faded(shows); - const freed = (if (p >= 0.5) s1 else s0).row == 1; const r: canim.Rect = .{ .x = xy[0], .y = xy[1], .w = w, .h = h }; - const m = if (p >= 0.5) now.moment() else now.before(); _ = nv.address(r.x + 16, r.y - 8, node, .{ .ink = ui.rgb(theme.color.region.heap) }); - if (freed) { - // Freed: dashed, its first word the link (where the value was), the second hatched, `key`. - nv.dashedFrame(r, theme.color.region.heap, theme.stroke.rule); - nv.fill(.{ .x = r.x + 2, .y = r.y + 2, .w = w / 2 - 2, .h = h - 4 }, theme.color.well); - for (0..4) |k| { - const x0 = r.x + w / 2 + 8 + @as(f32, @floatFromInt(k)) * 14; - nv.line(.{ x0, r.y + h - 4 }, .{ x0 + 12, r.y + 4 }, theme.color.rule, theme.stroke.rule); + // Nothing turns in a frame: a node that changes lists turns as it lands (its last 12 f), + // and its value and link, where they change and are not struck, crossfade with the idea (12 f). + const lands = after(now, 36, 12); + const turns = after(now, 0, 12); + for ([2]Spot{ s0, s1 }, 0..) |spot, side| { + const freed = spot.row == 1; + const other_freed = (if (side == 0) s1 else s0).row == 1; + const look = if (freed == other_freed) @as(f32, if (side == 1) 1 else 0) else if (side == 1) lands else 1 - lands; + if (look <= 0) continue; + const lv = nv.faded(look); + if (freed) { + // Freed: dashed, its first word the link (where the value was), the second hatched, `key`. + lv.dashedFrame(r, theme.color.region.heap, theme.stroke.rule); + lv.fill(.{ .x = r.x + 2, .y = r.y + 2, .w = w / 2 - 2, .h = h - 4 }, theme.color.well); + for (0..4) |k| { + const x0 = r.x + w / 2 + 8 + @as(f32, @floatFromInt(k)) * 14; + lv.line(.{ x0, r.y + h - 4 }, .{ x0 + 12, r.y + 4 }, theme.color.rule, theme.stroke.rule); + } + lv.text(.mono, theme.size.mono.context, r.x + w - 40, r.y + 16, "key", t.faint); + } else { + lv.fill(.{ .x = r.x, .y = r.y, .w = w - well, .h = h }, theme.color.raised); + lv.fill(.{ .x = r.x + w - well, .y = r.y, .w = well, .h = h }, theme.color.well); + // A node changing hands is not struck: its words are the other list's now (the dump strikes them). + const heat = if (s0.row == s1.row) now.written(node, 8, .idea, 0) else canim.ui.Heat.none; + const cell: canim.Rect = .{ .x = r.x, .y = r.y, .w = w - well, .h = h }; + const ink = lv.cellWritten(cell, heat, theme.color.raised); + // Struck, the digits change under the strike's fill; else they crossfade. + const values = [2]u64{ (canim.Recording.word(now.before(), node, 8) catch null) orelse 0, (canim.Recording.word(now.moment(), node, 8) catch null) orelse 0 }; + const struck = heat.level >= 1; + const turned = if (struck) @as(f32, if (now.since(.idea, 0) >= theme.time.strike.in / 2.0) 1 else 0) else turns; + for (values, [2]f32{ if (values[0] != values[1]) 1 - turned else 0, if (values[0] != values[1]) turned else 1 }) |value, k| { + if (k <= 0) continue; + // Taken back before the program writes it: the old link, faint, as stored. + const stale = value != 0 and value >> 12 == node >> 12; + if (stale) lv.faded(k).label(.mono, theme.size.mono.context, cell, .center, lv.print("{x}", .{value}), t.faint) else lv.faded(k).label(ink.face, theme.size.mono.small, cell, .center, lv.print("{d}", .{@as(i64, @bitCast(value))}), if (heat.level >= 1) ink.ink else ui.rgb(t.primary)); + } + } + // Its link: the next node in its row, as the pointer protects or holds it. + const sq: canim.Rect = .{ .x = if (freed) r.x + 18 else r.x + w - well / 2 - 5, .y = r.y + h / 2 - 5, .w = 10, .h = 10 }; + var links: [2]u64 = undefined; + for (&links, [2]canim.Recording.Moment{ now.before(), now.moment() }) |*link, lm| { + const word = (canim.Recording.word(lm, node + if (freed) @as(u64, 0) else 8, 8) catch null) orelse 0; + link.* = if (freed) word ^ (node >> 12) else word; + } + for (links, [2]f32{ if ((links[0] == 0) != (links[1] == 0)) 1 - turns else 0, if ((links[0] == 0) != (links[1] == 0)) turns else 1 }) |next, k| { + if (k <= 0) continue; + if (next == 0) { + lv.faded(k).line(.{ sq.x - 6, sq.y + 16 }, .{ sq.x + 16, sq.y - 6 }, t.dim, theme.stroke.rule); + } else if (spotOf(program[shown], free[shown], next)) |ns| { + lv.faded(k).fill(sq, theme.color.region.heap); + const nx = at(ns); + if (@abs(nx[1] - r.y) < 1 and nx[0] > r.x) lv.faded(k).route(&.{ .{ sq.x + 10, r.y + h / 2 }, .{ nx[0] - 2, r.y + h / 2 } }, theme.color.region.heap, .{ .width = theme.stroke.line, .head = theme.metric.node.arrow_head }); + } } - nv.text(.mono, theme.size.mono.context, r.x + w - 40, r.y + 16, "key", t.faint); - } else { - nv.fill(.{ .x = r.x, .y = r.y, .w = w - well, .h = h }, theme.color.raised); - nv.fill(.{ .x = r.x + w - well, .y = r.y, .w = well, .h = h }, theme.color.well); - const value = (canim.Recording.word(m, node, 8) catch null) orelse 0; - // A node changing hands is not struck: its words are the other list's now (the dump strikes them). - const heat = if (s0.row == s1.row) now.written(node, 8, .idea, 0) else canim.ui.Heat.none; - const cell: canim.Rect = .{ .x = r.x, .y = r.y, .w = w - well, .h = h }; - const ink = nv.cellWritten(cell, heat, theme.color.raised); - // Taken back before the program writes it: the old link, faint, as stored. - const stale = value != 0 and value >> 12 == node >> 12; - if (stale) nv.label(.mono, theme.size.mono.context, cell, .center, nv.print("{x}", .{value}), t.faint) else nv.label(ink.face, theme.size.mono.small, cell, .center, nv.print("{d}", .{@as(i64, @bitCast(value))}), if (heat.level >= 1) ink.ink else ui.rgb(t.primary)); - } - // Its link: the next node in its row, as the pointer protects or holds it. - const sq: canim.Rect = .{ .x = if (freed) r.x + 18 else r.x + w - well / 2 - 5, .y = r.y + h / 2 - 5, .w = 10, .h = 10 }; - const word = (canim.Recording.word(m, node + if (freed) @as(u64, 0) else 8, 8) catch null) orelse 0; - const next = if (freed) word ^ (node >> 12) else word; - if (next == 0) { - nv.line(.{ sq.x - 6, sq.y + 16 }, .{ sq.x + 16, sq.y - 6 }, t.dim, theme.stroke.rule); - } else if (spotOf(program[shown], free[shown], next)) |ns| { - nv.fill(sq, theme.color.region.heap); - const nx = at(ns); - if (@abs(nx[1] - r.y) < 1 and nx[0] > r.x) nv.route(&.{ .{ sq.x + 10, r.y + h / 2 }, .{ nx[0] - 2, r.y + h / 2 } }, theme.color.region.heap, .{ .width = theme.stroke.line, .head = theme.metric.node.arrow_head }); } // Who points here: `head`, the local `n`, and the registers. var names: [2][4][]const u8 = undefined; @@ -317,6 +337,8 @@ const Chunks = struct { stage: *const canim.Stage, pub fn draw(c: *const Chunks, f: *sk.Frame, placed: anim.Placed) void { + // On the map its box is as long as its rows. + canim.map.measure(.view, (view_len + 15) / 16); const v = canim.hexdump.panel(f, placed, c.stage, "the heap, 405000 to 40506f", &.{}); defer v.u.unclip(); if (v.rows == 0) return; @@ -367,9 +389,14 @@ fn lineIn(rec: *canim.Recording, function: []const u8, n: usize, line: u32) u64 /// the idea; it closes with the advance. Never open over a step it does not concern. fn tcacheStep(at: canim.Cursor, s: *anim.Frame, tcache: *const canim.Loupe, wave: bool) void { const tm = theme.time; + // A free: its line's instructions first, a cascade onto the call row (the recording stops there, + // `break_call`); then the call, stepped over, goes on from that row without firing the line again. + const onto = wave and at.rec.own[at.moment(s) + 1] == at.rec.own[at.moment(s)]; + if (onto) at.walk(s, .moment, at.moment(s) + 1); const from = at.moment(s); const to = at.rec.next(.line, from); var plan = at.planned(from, to, wave); + plan.fires = !onto; const opens = plan.at[1] + 3 * 6.0 / 60.0 + 12.0 / 60.0; const idea = opens + tm.loupe.open + 24.0 / 60.0; const later = @max(idea - plan.at[2], 0); @@ -399,6 +426,9 @@ fn frame(s: *anim.Frame) void { const head_at = globalOf(rec, lineIn(rec, "push", 0, 13)); const lists = canim.add(s, Lists{ .cursor = at, .stage = ctx.stage, .head_at = head_at }); const chunks = canim.add(s, Chunks{ .cursor = at, .stage = ctx.stage }); + // On the map too: the drawing over the source, the heap's bytes over the registers. + ctx.map.add(s, Lists{ .cursor = at, .stage = ctx.map.stage, .head_at = head_at }); + ctx.map.add(s, Chunks{ .cursor = at, .stage = ctx.map.stage }); // The tcache, once there is one: a loupe from the row of the chunk it lives in, over the stack. const tc = tcacheOf(rec); const tcache = canim.loupe(s, .{ .cursor = at, .stage = ctx.stage, .from = .{ .row = .{ @@ -411,6 +441,7 @@ fn frame(s: *anim.Frame) void { .{ .at = 0x98, .len = 8, .reads = .pointer, .note = "the head of 0x20" }, } }, } } }); + ctx.map.loupes = .{ tcache, null }; var says: canim.Narrator = .{}; const joins = ctx.ties(s); canim.establish(s, .{ ctx.clock, ctx.registers, ctx.stack, ctx.source, ctx.disasm, chunks, lists, joins }); @@ -418,25 +449,30 @@ fn frame(s: *anim.Frame) void { // design/motion.md, "A scene: libc's heap from the inside": each caption // leads the steps it is about, and changes only in a rest. says.line(s, "Three pushes and two pops: what malloc and free\ndo to the heap."); - at.walk(s, .line, lineIn(rec, "push", 0, 11)); + at.hurry(s, .line, lineIn(rec, "push", 0, 11), .{}); says.line(s, "malloc(16) cut a chunk of 0x20 off the top: its size word,\n0x21, then the 16 bytes rax points at. The top is 0x20 smaller."); - at.walk(s, .line, rec.lineMoment(25, main.enter)); + at.walk(s, .line, lineIn(rec, "push", 0, 15)); + // The return to main runs with the two pushes after it. says.line(s, "Two more pushes, at a run: three chunks in a row, each\nwith P set, the chunk before it in use."); at.hurry(s, .line, rec.lineMoment(27, main.enter), .{}); says.line(s, "pop takes the head off the list: head = n->next."); + at.hurry(s, .line, lineIn(rec, "pop", 0, 19), .{}); at.walk(s, .line, lineIn(rec, "pop", 0, 20)); says.line(s, "free gave 9's chunk to the tcache, malloc's bin for 0x20.\nIts first word is the link now: 405, which is NULL, safe-linked."); tcacheStep(at, s, tcache, true); - at.walk(s, .line, rec.lineMoment(28, main.enter)); + at.hurry(s, .line, rec.lineMoment(28, main.enter), .{}); says.line(s, "The second pop: head moves on, to 7's node."); + at.hurry(s, .line, lineIn(rec, "pop", 1, 19), .{}); at.walk(s, .line, lineIn(rec, "pop", 1, 20)); says.line(s, "The second free chains to the first: 405455 is 405050 xor 405,\nthe chunk's own address shifted 12. The arc is the pointer it hides."); tcacheStep(at, s, tcache, true); + // Walked, the return: this caption's reading time is longer than its steps, and the picture would stand still. at.walk(s, .line, rec.lineMoment(29, main.enter)); says.line(s, "malloc(16) took the bin's head, 405030: the tcache points\nat 405050 again, the key is wiped, the old link stays."); - at.walk(s, .line, lineIn(rec, "push", 3, 11)); + at.hurry(s, .line, lineIn(rec, "push", 3, 11), .{}); tcacheStep(at, s, tcache, false); says.line(s, "push writes 25 over the old link: the list is 25, then 7."); - at.walk(s, .line, rec.lineMoment(30, main.enter)); + at.walk(s, .line, lineIn(rec, "push", 3, 15)); + at.hurry(s, .line, rec.lineMoment(30, main.enter), .{}); says.close(s); } |
