diff options
| -rw-r--r-- | core/Core.zig | 2 | ||||
| -rw-r--r-- | core/views.zig | 6 | ||||
| -rw-r--r-- | core/walk.zig | 55 |
3 files changed, 41 insertions, 22 deletions
diff --git a/core/Core.zig b/core/Core.zig index 6c981adf..835eb86c 100644 --- a/core/Core.zig +++ b/core/Core.zig @@ -1753,7 +1753,7 @@ fn updateInner(p: *Pardes, ev: Event) void { v.cell_pixels.w = @max(1, sz.cell_pixels.w); v.cell_pixels.h = @max(1, sz.cell_pixels.h); layout.compute(p); - p.resize_count += 1; + p.resize_count +|= 1; }, .output => |o| { const pane = p.panes[o.pane.index()] orelse return; diff --git a/core/views.zig b/core/views.zig index 4f2d5744..4c020981 100644 --- a/core/views.zig +++ b/core/views.zig @@ -82,6 +82,12 @@ pub const Shares = struct { k: u8, panes: []const Share = &.{}, + /// Its view, a slot (walk.zig `.index`). + pub const replica = .{ .index = .{.{ "k", "slots" }} }; + pub fn slots(_: *const Shares, k: usize) bool { + return k < max; + } + pub const Share = struct { id: u16, pv: PaneView }; }; diff --git a/core/walk.zig b/core/walk.zig index 51b0a384..7a77fdb6 100644 --- a/core/walk.zig +++ b/core/walk.zig @@ -1398,8 +1398,11 @@ fn countOf(n: u64) DecodeError!usize { } /// An allocation a load made: what a reference to it finds, and what a -/// failed load frees. -const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false }; +/// failed load frees. `of`: what it holds (`typeWord` of its element); a +/// reference read back is to a value of its own type, or the dump is +/// refused (a damaged one would have one type's bytes read as another's, +/// pointers and all). +const Made = struct { bytes: []u8, alignment: std.mem.Alignment, gpa: std.mem.Allocator, dead: bool = false, of: u64 = 0 }; /// How far a reader had made things before a value: what that value made /// is everything after it (`letGo`). @@ -1509,8 +1512,8 @@ pub const Loader = struct { } } - fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment) DecodeError!usize { - try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); + fn made(l: *Self, bytes: []u8, alignment: std.mem.Alignment, of: u64) DecodeError!usize { + try l.shared.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of }); return l.shared.items.len - 1; } @@ -1609,6 +1612,11 @@ pub const Loader = struct { /// The next root, made: what it points at is `gpa`'s. pub fn get(l: *Self, comptime T: type) DecodeError!T { + // A root that fails lets go of what it had made: its caller's books + // (a load's, cut back past what it frees itself) never hold half + // of a value. + const at = l.mark(); + errdefer l.letGo(at); var out: T = undefined; try l.fresh(T, &out); try l.value(T, try l.root(), &out); @@ -1750,12 +1758,12 @@ pub const Loader = struct { }, .one => { const C = @typeInfo(T).pointer.child; - if (try l.ref()) |again| { + if (try l.ref(C)) |again| { out.* = @ptrCast(@alignCast(again.ptr)); return; } const one = try l.gpa.create(C); - const at = l.made(std.mem.asBytes(one), .of(C)) catch |err| { + const at = l.made(std.mem.asBytes(one), .of(C), typeWord(C)) catch |err| { l.gpa.destroy(one); return err; }; @@ -1821,22 +1829,23 @@ pub const Loader = struct { /// A reference to a pointer or slice made before, or null for a /// new one (its length then read). - fn ref(l: *Self) DecodeError!?[]u8 { + fn ref(l: *Self, comptime E: type) DecodeError!?[]u8 { const n = try l.scalar(u32); if (n == 0) return null; if (n > l.shared.items.len) return error.BadDump; const got = l.shared.items[n - 1]; if (got.dead) return error.Incompatible; + if (got.of != typeWord(E)) return error.BadDump; return got.bytes; } fn slice(l: *Self, comptime E: type, s: SrcType) DecodeError![]E { - if (try l.ref()) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; + if (try l.ref(E)) |again| return @as([*]E, @ptrCast(@alignCast(again.ptr)))[0 .. again.len / @max(1, @sizeOf(E))]; const n = try l.count(); if (n == 0) return &.{}; if (n > l.bytes.len - l.at) return error.EndOfDump; const many = try l.gpa.alloc(E, n); - const at = l.made(std.mem.sliceAsBytes(many), .of(E)) catch |err| { + const at = l.made(std.mem.sliceAsBytes(many), .of(E), typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -1994,14 +2003,14 @@ pub const Loader = struct { .float => _ = try l.take(s.bits / 8), .optional => if ((try l.take(1))[0] != 0) try l.skip(s.child), .one => if (try l.scalar(u32) == 0) { - _ = try l.made(&.{}, .@"1"); + _ = try l.made(&.{}, .@"1", 0); l.shared.items[l.shared.items.len - 1].dead = true; try l.skip(s.child); }, .slice => if (try l.scalar(u32) == 0) { const n = try l.scalar(u64); if (n > 0) { - _ = try l.made(&.{}, .@"1"); + _ = try l.made(&.{}, .@"1", 0); l.shared.items[l.shared.items.len - 1].dead = true; } try l.skipMany(s.child, n); @@ -2390,6 +2399,9 @@ pub const Get = struct { /// The value at `c`, made: what it points at is `gpa`'s. pub fn get(l: *Get, comptime T: type, c: Cursor) Error!T { + // As `Loader.get`: a value that fails leaves nothing made. + const at = l.mark(); + errdefer l.letGo(at); var out: T = undefined; try l.fresh(T, &out); try l.value(T, c, &out); @@ -2441,8 +2453,8 @@ pub const Get = struct { /// An allocation made for the value at `slot`, kept: what a value met /// again is, and what `rollback` frees. - fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot) Error!usize { - try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa }); + fn keep(l: *Get, bytes: []u8, alignment: std.mem.Alignment, slot: xitdb.Slot, of: u64) Error!usize { + try l.made.append(l.own, .{ .bytes = bytes, .alignment = alignment, .gpa = l.gpa, .of = of }); const at = l.made.items.len - 1; // Not kept after all: the caller frees it. if (placed(slot)) l.memo.put(l.own, slotWord(slot), at) catch |err| { @@ -2453,13 +2465,14 @@ pub const Get = struct { } /// The value made before for the same place, when there was one. - fn again(l: *Get, slot: xitdb.Slot) Error!?[]u8 { + fn again(l: *Get, slot: xitdb.Slot, comptime E: type) Error!?[]u8 { if (!placed(slot)) return null; const at = l.memo.get(slotWord(slot)) orelse return null; // Made, then let go of by the reader's caller (a dump's mounts). if (at >= l.made.items.len) return null; const m = l.made.items[at]; if (m.dead) return error.Incompatible; + if (m.of != typeWord(E)) return error.BadDump; return m.bytes; } @@ -2539,12 +2552,12 @@ pub const Get = struct { }, .one => { const C = @typeInfo(T).pointer.child; - if (try l.again(slot)) |bytes| { + if (try l.again(slot, C)) |bytes| { out.* = @ptrCast(@alignCast(bytes.ptr)); return; } const one = try l.gpa.create(C); - const at = l.keep(std.mem.asBytes(one), .of(C), slot) catch |err| { + const at = l.keep(std.mem.asBytes(one), .of(C), slot, typeWord(C)) catch |err| { l.gpa.destroy(one); return err; }; @@ -2674,7 +2687,7 @@ pub const Get = struct { /// A slice of `E` at `c`: numbers' bytes, a blob's text by its hash, or /// a list; one met again is the same. fn slice(l: *Get, comptime E: type, c: Cursor, slot: xitdb.Slot) Error![]E { - if (try l.again(slot)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))]; + if (try l.again(slot, E)) |bytes| return @as([*]E, @ptrCast(@alignCast(bytes.ptr)))[0 .. bytes.len / @max(1, @sizeOf(E))]; if (comptime numbers(E)) { switch (slot.tag) { .bytes, .short_bytes => {}, @@ -2692,7 +2705,7 @@ pub const Get = struct { l.gpa.free(got.bytes); return &.{}; } - _ = l.keep(got.bytes, .@"1", slot) catch |err| { + _ = l.keep(got.bytes, .@"1", slot, typeWord(u8)) catch |err| { l.gpa.free(got.bytes); return err; }; @@ -2704,7 +2717,7 @@ pub const Get = struct { const n = got.bytes.len / width; if (n == 0) return &.{}; const many = try l.gpa.alloc(E, n); - const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| { + const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -2721,7 +2734,7 @@ pub const Get = struct { // sizes nothing. if (n > (c.db.core.length() catch return error.BadDump) / (@bitSizeOf(xitdb.Slot) / 8)) return error.BadDump; const many = try l.gpa.alloc(E, n); - const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot) catch |err| { + const at = l.keep(std.mem.sliceAsBytes(many), .of(E), slot, typeWord(E)) catch |err| { l.gpa.free(many); return err; }; @@ -2742,7 +2755,7 @@ pub const Get = struct { const at = (texts.getCursor(std.mem.readInt(u256, got.value[0..32], .big)) catch return error.BadDump) orelse return error.BadDump; const text = at.readBytesAlloc(l.gpa, most(at)) catch |err| return if (err == error.OutOfMemory) error.OutOfMemory else error.BadDump; const bytes: []u8 = @constCast(text); - _ = l.keep(bytes, .@"1", slot) catch |err| { + _ = l.keep(bytes, .@"1", slot, typeWord(u8)) catch |err| { l.gpa.free(bytes); return err; }; |
