diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/Registers.zig | 13 | ||||
| -rw-r--r-- | src/locations.zig | 9 | ||||
| -rw-r--r-- | src/rollback.zig | 123 | ||||
| -rw-r--r-- | src/views.zig | 16 |
4 files changed, 147 insertions, 14 deletions
diff --git a/src/Registers.zig b/src/Registers.zig index 5e23da5b..760aff9a 100644 --- a/src/Registers.zig +++ b/src/Registers.zig @@ -5,12 +5,17 @@ //! computed from the editor rather than stored (`_`, `#`, `.`, `%`) and the //! clipboard's paste side are read in edit.zig, which has the pane at hand. const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const Registers = @This(); list: std.ArrayList(Reg) = .empty, last_search: u21 = '/', +/// A value and the joined text are replaced whole, never written in place, +/// so a replica's rollback copy pins them as it does a file's text +/// (src/rollback.zig `pinned`), and they are freed raw: a safe build's +/// free would write over one the copy still holds. pub const Reg = struct { name: u21, /// gpa-owned, one per range, in document order @@ -30,9 +35,9 @@ pub fn deinit(r: *Registers, gpa: std.mem.Allocator) void { } fn freeReg(gpa: std.mem.Allocator, reg: *Reg) void { - for (reg.values) |v| gpa.free(v); + for (reg.values) |v| freeRaw(gpa, v); gpa.free(reg.values); - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); } pub fn get(r: *Registers, name: u21) ?*Reg { @@ -69,9 +74,9 @@ pub fn put(r: *Registers, gpa: std.mem.Allocator, name: u21, bytes: []const u8, freeReg(gpa, reg); reg.* = .{ .name = name, .values = values }; } - gpa.free(reg.values[i]); + freeRaw(gpa, reg.values[i]); reg.values[i] = copy; - if (reg.joined) |j| gpa.free(j); + if (reg.joined) |j| freeRaw(gpa, j); reg.joined = null; } diff --git a/src/locations.zig b/src/locations.zig index cdb228a6..40f22f85 100644 --- a/src/locations.zig +++ b/src/locations.zig @@ -1,4 +1,5 @@ const std = @import("std"); +const freeRaw = @import("memory.zig").freeRaw; const pardes = @import("pardes.zig"); const look = @import("look.zig"); const syntax = @import("syntax.zig"); @@ -25,12 +26,14 @@ pub const Row = struct { colors: []u8 = &.{}, }; +/// Raw (no safe build's overwrite): a replica's rollback copy pins a row +/// blob and what its rows own (src/rollback.zig `pinnedDeep`). pub fn freeRows(gpa: std.mem.Allocator, rows: []Row) void { for (rows) |row| { - gpa.free(row.path); - gpa.free(row.colors); + freeRaw(gpa, row.path); + freeRaw(gpa, row.colors); } - gpa.free(rows); + freeRaw(gpa, rows); } /// A list of rows being built, given up: each row's own bytes, then the list. diff --git a/src/rollback.zig b/src/rollback.zig index 79b8202e..a7027da6 100644 --- a/src/rollback.zig +++ b/src/rollback.zig @@ -11,10 +11,11 @@ //! - `Copy`, the walker's walk of the core's state (src/walk.zig: what a //! digest covers, so views, buffers, registers, counters and terminal //! metadata), saving the bytes of every allocation it reaches, the core -//! struct's own included. A file's text, its line index and its undo -//! snapshots are pinned instead: an edit puts a new buffer in their -//! place (File.setContentSpan) and never writes one, so the copy keeps -//! a reference and the put-off free keeps it alive. +//! struct's own included. A file's text, its line index, its undo +//! snapshots and a register's texts are pinned instead: an edit puts a +//! new buffer in their place (File.setContentSpan, Registers.put) and +//! never writes one, so the copy keeps a reference and the put-off free +//! keeps it alive. //! //! Putting the copy back writes each saved allocation's bytes over it, so //! every pointer in them points where it did, at what was there; then the @@ -197,6 +198,8 @@ pub const Copy = struct { /// of the blobs: a file's text or an undo snapshot's. where: []const u8 = "", pinnable: bool = false, + /// Whether it is a blob of rows that own allocations of their own. + deep: bool = false, const Region = struct { addr: usize, off: usize, len: usize }; const Pin = struct { addr: usize, len: usize, hash: u64, where: []const u8 }; @@ -342,6 +345,7 @@ pub const Copy = struct { if (comptime opaqueTo(F)) continue; c.where = @typeName(T) ++ "." ++ name; c.pinnable = comptime pinned(T, name); + c.deep = comptime pinnedDeep(T, name); if (comptime walk.lengthOf(T, name) != null and @typeInfo(F) == .array) { const len = comptime walk.lengthOf(T, name).?; if (comptime heapIn(@typeInfo(F).array.child, 0)) for (@field(v, name)[0..walk.lengthIn(v, len)]) |*e| try c.visit(@typeInfo(F).array.child, e); @@ -394,11 +398,14 @@ pub const Copy = struct { if (s.len == 0) return; const addr = @intFromPtr(s.ptr); if (try c.reached([]const E, addr)) return; - if (comptime !heapIn(E, 0)) if (c.pinnable and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + const flat = comptime !heapIn(E, 0); + if ((if (flat) c.pinnable else c.deep) and s.len * @sizeOf(E) >= blob_min and c.epoch.?.owns(addr) != null) { + // A blob of rows with allocations of their own pinned whole: + // they are as immutable as it, and kept alive with it. const bytes = std.mem.sliceAsBytes(s); try c.pins.append(c.meta, .{ .addr = addr, .len = bytes.len, .hash = if (checked) std.hash.Wyhash.hash(0, bytes) else 0, .where = c.where }); return; - }; + } _ = try c.allocation(addr); if (comptime heapIn(E, 0)) for (s) |*e| try c.visit(E, e); } @@ -409,7 +416,21 @@ pub const Copy = struct { fn pinned(comptime T: type, comptime name: []const u8) bool { const File = pardes.panes.File; return (T == File.State and (std.mem.eql(u8, name, "content") or std.mem.eql(u8, name, "line_starts"))) or - (T == File.Snapshot and std.mem.eql(u8, name, "content")); + (T == File.Snapshot and std.mem.eql(u8, name, "content")) or + // A register's values and joined text: a yank of a terminal's whole + // grid is megabytes, which every speculation would otherwise copy. + (T == pardes.Registers.Reg and (std.mem.eql(u8, name, "values") or std.mem.eql(u8, name, "joined"))); +} + +/// Blobs of rows that own allocations of their own (a location's path, its +/// colours), the array and they replaced whole and never written, freed raw +/// (locations.freeRows): a file's location rows (5 MB after a flood of +/// search results), the ones an edit dropped, a query's saved. +fn pinnedDeep(comptime T: type, comptime name: []const u8) bool { + const File = pardes.panes.File; + return (T == File.State and std.mem.eql(u8, name, "location_rows")) or + (T == File.DroppedMarks and std.mem.eql(u8, name, "rows")) or + (T == pardes.panes.Query.Saved and std.mem.eql(u8, name, "rows")); } fn annotatedStruct(comptime T: type) bool { @@ -497,3 +518,91 @@ test "a copy put back is the core as it was; one let go keeps what was done" { p.deinit(); try std.testing.expectEqual(@as(usize, 0), e.live.count()); } + +test "a view's slot while it is live keeps no pointer a rollback could put old bytes back through" { + // The C heap: a freed chunk comes back to the next allocation its size. + var e: Epoch = .{}; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + defer p.deinit(); + p.pump(.{}) catch {}; + _ = p.openView() orelse return error.NoView; + // View 0's column tag has a text of its own; out to view 1 and back. + const text = "the column tag of view 0, its own"; + p.col_tags[0].own = try gpa.dupe(u8, text); + p.toView(1); + p.toView(0); + // The live view drops it, and its memory is another's: something the + // copy has no business saving. + const was = p.col_tags[0].own.?; + gpa.free(was); + p.col_tags[0].own = null; + const other = try gpa.alloc(u8, text.len); + defer gpa.free(other); + if (other.ptr != was.ptr) return error.SkipZigTest; // the heap handed out another + @memset(other, 'a'); + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + // Written in place while the copy is held, then the copy put back. + @memset(other, 'b'); + c.restore(); + try std.testing.expect(std.mem.allEqual(u8, other, 'b')); +} + +test "a register's texts are pinned, not copied, and come back whole" { + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + // A terminal's whole grid yanked: megabytes, and joined once read. + const grid = try std.testing.allocator.alloc(u8, 2 << 20); + defer std.testing.allocator.free(grid); + @memset(grid, 'y'); + const reg = pardes.Registers.default; + p.registers.put(gpa, reg, grid, 0, 1, true); + _ = p.registers.text(gpa, reg) orelse return error.NoText; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.savedBytes() < 1 << 20); + try std.testing.expect(c.pinnedBytes() >= 4 << 20); + // A speculation yanks over it, and is undone. + p.registers.put(gpa, reg, "a word", 0, 1, true); + c.restore(); + const back = p.registers.value(reg, 0) orelse return error.Lost; + try std.testing.expectEqual(grid.len, back.len); + try std.testing.expect(std.mem.allEqual(u8, back, 'y')); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} + +test "a file's location rows are pinned whole, with what they own, and come back" { + const locations = @import("locations.zig"); + var e: Epoch = .{ .child = std.testing.allocator }; + defer e.deinit(); + const gpa = e.allocator(); + const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .file = "mise.toml", .image_allocator = gpa, .pdf_allocator = gpa, .tree_sitter_allocator = gpa, .frame_allocator = gpa }); + p.pump(.{}) catch {}; + const f = for (p.panes) |slot| { + if (slot) |pane| if (pane.file) |*file| break file; + } else return error.NoFile; + // A search's rows: each its own path. + const rows = try gpa.alloc(locations.Row, 500); + for (rows) |*row| row.* = .{ .kind = .match, .path = try gpa.dupe(u8, "src/rollback.zig"), .at = .{}, .location_end = 0, .code_start = 0 }; + f.location_rows = rows; + var c: Copy = .{}; + defer c.deinit(); + try c.take(&e, p); + try std.testing.expect(c.pinnedBytes() >= rows.len * @sizeOf(locations.Row)); + // A speculation's new search drops them, and is undone. + locations.freeRows(gpa, f.location_rows); + f.location_rows = &.{}; + c.restore(); + try std.testing.expectEqual(@as(usize, 500), f.location_rows.len); + try std.testing.expectEqualStrings("src/rollback.zig", f.location_rows[499].path); + p.deinit(); + try std.testing.expectEqual(@as(usize, 0), e.live.count()); +} diff --git a/src/views.zig b/src/views.zig index bf9b9147..b9760143 100644 --- a/src/views.zig +++ b/src/views.zig @@ -215,6 +215,7 @@ pub fn undumped(p: *Pardes, others: []const Dumped) !void { /// for one client (a Restore, `-l`) is one view. pub fn only(p: *Pardes) void { for (0..max) |k| if (k != p.view) close(p, @intCast(k)); + emptied(p, p.view); const k = p.view; if (k == 0) return; std.mem.swap(?*Parked, &p.parked[0], &p.parked[k]); @@ -390,6 +391,19 @@ pub fn to(p: *Pardes, k: u8) void { if (k == p.view) return; park(p); unpark(p, k); + emptied(p, k); +} + +/// View `k` is live: its slot, stale until it parks again (written whole +/// then), keeps no pointer to what it handed the live fields. A rollback +/// copy walks every slot (src/rollback.zig): one left there, freed by the +/// live view and the memory another's since, would be saved and its old +/// bytes put back over the other's. (A pane's share holds no pointer but +/// its selections, which `unpark` hands over.) +fn emptied(p: *Pardes, k: u8) void { + const slot = p.parked[k] orelse return; + slot.view = .{ .screen_w = p.screen_w, .screen_h = p.screen_h }; + slot.view.col_tags = @splat(.{ .what = .column, .gpa = p.gpa }); } /// A new view, as the live one is laid out and focused, with its panes @@ -431,6 +445,7 @@ pub fn close(p: *Pardes, k: u8) void { } else unreachable; park(p); unpark(p, other); + emptied(p, other); } const v = &p.parked[k].?.view; for (&v.col_tags) |*t| t.deinit(p.gpa); @@ -478,6 +493,7 @@ pub fn drawOthers(p: *Pardes, h: pardes.Host) !void { park(p); defer { copy(&fields, p, &p.parked[live].?.view); + emptied(p, live); p.view = live; for (p.panes) |s| if (s) |pane| { const pv = &pane.pv[live]; |
