From e699ffe4182f66e28180ea2d91ab1fb8c408d49e Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 17 Nov 2026 00:45:31 -0300 Subject: Delete the Store; State is checked against the 64 MiB a save holds A sketch's data is State: flat bytes, what grows has its capacity in its type. The Store, a heap in a file mapped at 0x2000_0000_0000 with 64 GiB reserved, its power-of-two allocator, its root adopt and migration, and its test, are gone from memory.zig; so are Context.store and store_root, Info.store, Memory.store, the Store declaration's glue, Init.store, Frame.store and Console.store, the web host's store in wasm memory, the host's StoreFile wiring and `.store` file, the `.store` suffix in moving an old state into .sketchbook/, the build's tests' `rm` of it, and layout.check's `pointers` flag (State is the one checked memory now). Its one real user, micscope, moved off it in the change before. Old `.state.store` files are not read, converted or deleted, and stay ignored by .gitignore. A sketch that still declares a Store gets no error for the declaration: nothing reads it, and a `*Store` parameter is a compile error (no argument of that type). layout.of(T, .state) refuses a State bigger than layout.max_state, 64 MiB, one constant: "State is N bytes, and a save holds at most 64 MiB: lower a capacity (an array, an sk.List), or keep what need not be saved in Session." The pointer-hash gap the spec found (a Store pointer's element type changing kept the old bytes) is gone with the only persisted memory that held pointers. README: no Store row, allocator row, migration paragraph, file bullet or known limits; draw and command lose their store parameter; State's row says 64 MiB at most; data that grows and is the user's work goes in a List in State or, bigger than a save, in files the sketch appends to. Tests: zig build test, 166/166 steps (the store's unit test gone; new: a 65-MiB State fails to compile with the message above, by expect_errors). micscope, spheres, noc's e2e and drat-meout's test unchanged and green. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNBECMWcJD5pdS4SR6T --- README.md | 55 ++++------- build.zig | 19 +++- src/abi.zig | 16 +-- src/host/Host.zig | 24 +---- src/host/memory.zig | 257 +------------------------------------------------ src/host/root.zig | 37 +++---- src/layout.zig | 28 +++--- src/sketch/request.zig | 2 +- src/sketch/root.zig | 31 ++---- src/sketch/web.zig | 7 +- 10 files changed, 89 insertions(+), 387 deletions(-) diff --git a/README.md b/README.md index 5b262d3..7691d85 100644 --- a/README.md +++ b/README.md @@ -44,24 +44,23 @@ An app is an ordinary module. Every declaration is optional: | Declaration | Meaning | | --- | --- | | `name`, `width`, `height` | Window title and initial canvas | -| `State` | Saved to a file, kept in every session's keyframes and marks, replayed: what `update` changes. No pointers and no host handles (compile errors). | +| `State` | Saved to a file, kept in every session's keyframes and marks, replayed: what `update` changes. No pointers, no host handles, and at most 64 MiB (compile errors). | | `Session` | This process: survives reloads, never saved. Caches, image and buffer handles, playback cursors. | -| `Store` | On disk, at the same address in every run: survives reloads and restarts. Big data that is not replayed (a recording, a document). May point into itself; no host handles. | | `init(state, host)` | Seed brand-new state, after the declared defaults | | `setup(session, init) !void` | Initialize resources once per fresh Session, including startup with restored State | | `update(state, input)` | **Deterministic**: the host replays recorded input through it | -| `draw(state, session, store, frame)` | Pictures and sound, as commands; may change Session and Store | -| `command(state, session, store, console, line) !bool` | Console lines the host does not know | +| `draw(state, session, frame)` | Pictures and sound, as commands; may change Session | +| `command(state, session, console, line) !bool` | Console lines the host does not know | | `commands` | Console completions | Functions take the parameters they name, matched by type and in any order: -`draw(s: *const State, f: *sk.Frame)` and `draw(s: *const State, store: -*Store, ss: *Session, f: *sk.Frame)` both work. `update` can only ask for +`draw(s: *const State, f: *sk.Frame)` and `draw(f: *sk.Frame, ss: *Session, +s: *const State)` both work. `update` can only ask for `State` and `Input`; asking for more is a compile error, since a replay could not reproduce it. -`setup` receives `*sk.Init` with `host`, `session`, and `store` allocators; -it may also request Session, Store, or Host directly. It gets no State, Input, +`setup` receives `*sk.Init` with `host` and the `session` allocator; it may +also request Session or Host directly. It gets no State, Input, or Frame. Load assets and create/upload GPU buffers there, then use those resources in `draw`. `try init.host.writeBuffer(buffer, offset, bytes)` submits a copy immediately; the source bytes may be released when it returns. WebGPU @@ -82,14 +81,11 @@ Memory comes by lifetime, one allocator each: | --- | --- | --- | | `f.scratch` | this frame | all at once, after the frame is drawn | | `f.session` | this process | one allocation at a time; the rest when Session's layout changes, along with the images and buffers the session made | -| `f.store` | on disk, `.store` | one allocation at a time; `--fresh` empties it | -A Store layout change migrates field by field like State: unchanged fields, -pointers included, keep their values, since the store never moves. -`command` gets the session and store allocators on its `Console`. +`command` gets the session allocator on its `Console`. -`update` sees only State, so when a key should do something to Session or -Store, State carries a request: `seek: sk.Request(f32)`, sent with +`update` sees only State, so when a key should do something to Session, +State carries a request: `seek: sk.Request(f32)`, sent with `s.seek.send(0.5)`. Session keeps an `sk.Handled`, and `draw` runs `if (ss.seek.take(s.seek)) |to| ...` once per request. A fresh Session counts earlier requests as handled, so nothing re-fires after a restart. @@ -131,7 +127,7 @@ State. `decodeSound`, `newSound`, `samples`, `readFile`, `appendFile`, `env`, `log`, `textSize`). Commands may be built from locals: the methods copy what they keep (text, GPU descriptions, uniforms) into scratch. Bulk data (vertices, samples, pixels, uploads) is -not copied and must outlive the frame: scratch, session or store memory. +not copied and must outlive the frame: scratch or session memory. `f.text(x, y, scale, bytes, color)` draws one line from its top left corner; scale 1 is a 13 px monospace face (8 px a character on a 16 px @@ -168,8 +164,8 @@ move. The web host ignores `raster`. dialog; the answer comes back as the console line `open `, so it reaches the sketch's `command` like anything typed there. `f.run(line)` does the same with any line, after the frame: `draw` cannot write State, -so this is how it tells State something only it learns (how many takes a -Store holds, say). +so this is how it tells State something only it learns (which of its +files are already written, say). `f.host.appendFile(name, bytes)` appends to a file of the sketch's own, `.` beside its state file (in `.sketchbook/` under `zig build @@ -375,9 +371,8 @@ after a reload that shrinks it. The default buffer is the element's own defaults, never `std.mem.zeroes` (an enum may have no 0). **Data that grows.** State is kept in keyframes, replayed and migrated, so -it holds what `update` changes, at a size fixed in its type. Store is not -kept in keyframes or replayed, so what is in it does not follow a seek. Three -cases, by who needs the data: +it holds what `update` changes, at a size fixed in its type, 64 MiB at most. +Three cases, by who needs the data: - `update` needs it: a `List` in State, sized for the worst case (the quadtree's 4096 points, 32 KiB; flocking's 2048 boids). @@ -391,7 +386,8 @@ cases, by who needs the data: may derive it differently. A cheap derivation needs no cache at all: the quadtree builds its tree in `f.scratch` every frame. - It is the user's work, not a function of State (a recording, a - document): Store. + document): a `List` in State, or, bigger than a save, files of the + sketch's own (`appendFile`), named from State, as micscope's takes are. The timeline then needs nothing more: a seek changes the key like an update does (`examples/noc`: `koch`, `lsystem`, `quadtree`). @@ -781,17 +777,12 @@ a talk with a slider, a loop, a choice, two slides and `beside`. rewritten, and the snapshot slots of files from before sessions become marks `1` to `7` in a run that records; a file that is not a state file is refused, never truncated. -- **Store file.** `.state.store` is a heap mapped at a fixed address - (`0x2000_0000_0000`, 64 GiB reserved), grown in place, locked to one - process. Power-of-two size classes with free lists in the file. - Keyframes, marks and replays do not cover it (only `draw` and `command` - change it). - **Where files go.** `zig build run`, `probe` and `export` keep a sketch's - files in `.sketchbook/`: `.state`, `.state.store` and - `.xitdb`. The host makes the directory with a `.gitignore` of `*`, + files in `.sketchbook/`: `.state`, `.xitdb` and the + `.state.` it appends to. The host makes the directory with a `.gitignore` of `*`, so neither git nor jj takes in a database that grows (the jj snapshot a build makes would commit one while it is under a megabyte), and moves in, - once, the state and store an older build kept in the project's root; + once, the state an older build kept in the project's root; when both exist, nothing moves and the host says which is used. Without `--state`, a run keeps them in `$XDG_DATA_HOME//` (`~/.local/share//` without it), which is where a shipped game's @@ -1103,17 +1094,13 @@ Add `.sketchbook = .{ .path = "..." }` to your `build.zig.zon`. `f.openFile` answers with a line of any length. - Every upload, compute dispatch and GPU pass is its own command buffer and submit. Fine at a handful per frame; batch them per frame if that grows. -- The store is not crash-safe: a crash in the middle of an allocation can - leave it inconsistent, and `--fresh` starts it over. -- Handles inside Store containers (`std.ArrayList(sk.Image)`) are not caught - at compile time: the check does not follow pointers. - Native antialiasing resolves coverage per sample within a run of shapes, the canvas per shape, so where two shapes' edges meet the web can show a faint seam native does not. `fill` rects and images are antialiased natively only inside a target. Without a GPU renderer, native draws aliased. A stroke that overlaps itself blends twice natively where it overlaps, once on the web. -- On the web: nothing persists (Store lasts as long as the page) and no +- On the web: nothing persists and no sessions are recorded, and multisampling is 4× at most. Every voice is silent there, whatever its sound (loaded, decoded or made with `newSound`), and so are groups' gains; `decodeSound` gives `.none`, and diff --git a/build.zig b/build.zig index 02d312e..0367809 100644 --- a/build.zig +++ b/build.zig @@ -183,6 +183,15 @@ pub fn build(b: *Build) void { b.createModule(.{ .root_source_file = b.path("src/math/root.zig"), .target = target, .optimize = optimize }), b.createModule(.{ .root_source_file = b.path("src/host/root.zig"), .target = target, .optimize = optimize, .imports = &.{ .{ .name = "abi", .module = abi }, .{ .name = "c", .module = c }, .{ .name = "gpu", .module = gpu }, .{ .name = "tracy", .module = tracy }, .{ .name = "cloud9", .module = cloud9 }, .{ .name = "xitdb", .module = xitdb } } }), }) |module| test_step.dependOn(&b.addRunArtifact(b.addTest(.{ .root_module = module })).step); + // A State bigger than a save holds is a compile error that says what to do. + const too_big = b.addObject(.{ .name = "too_big", .root_module = b.createModule(.{ + .root_source_file = b.addWriteFiles().add("too_big.zig", "comptime { _ = @import(\"abi\").layout.of(struct { bytes: [(64 << 20) + 1]u8 }, .state); }"), + .target = target, + .optimize = optimize, + .imports = &.{.{ .name = "abi", .module = abi }}, + }) }); + too_big.expect_errors = .{ .contains = "a save holds at most 64 MiB: lower a capacity (an array, an sk.List), or keep what need not be saved in Session." }; + test_step.dependOn(&too_big.step); // The anim module alone: its unit tests, and below the player's scenes. const test_anim = b.step("test-anim", "Test the anim module: its unit tests and the player on its test scenes"); const anim_tests = b.addRunArtifact(b.addTest(.{ .root_module = b.createModule(.{ .root_source_file = b.path("src/anim/root.zig"), .target = target, .optimize = optimize, .imports = anim_imports }) })); @@ -386,11 +395,11 @@ pub fn build(b: *Build) void { const killed = b.addSystemCommand(&.{ "sh", "-c", \\set -e \\db="${3%.state}.xitdb" - \\rm -f "$3" "$3.store" "$db" + \\rm -f "$3" "$db" \\if "$0" --headless --script "$1" --state "$3" "$4"; then echo "killed run exited"; exit 1; else test $? = 137; fi \\"$0" --headless --script "$2" --state "$3" "$4" \\test "$(stat -c %s "$db")" -lt 1048576 - \\rm -f "$3" "$3.store" "$db" + \\rm -f "$3" "$db" }); killed.setName("a session survives kill -9"); killed.addArtifactArg(host_exe); @@ -414,12 +423,12 @@ pub fn build(b: *Build) void { const past = b.addSystemCommand(&.{ "sh", "-c", \\set -e \\db="${4%.state}.xitdb" - \\rm -f "$4" "$4.store" "$db" + \\rm -f "$4" "$db" \\"$0" --headless --script "$1" --state "$4" "$5" \\"$0" --headless --script "$2" --state "$4" "$5" \\"$0" --headless --script "$3" --state "$4" "$6" \\test "$(stat -c %s "$db")" -lt 1048576 - \\rm -f "$4" "$4.store" "$db" + \\rm -f "$4" "$db" }); past.setName("past sessions replay"); past.addArtifactArg(host_exe); @@ -498,7 +507,7 @@ pub fn build(b: *Build) void { \\test -s data/replay/replay.state; test -s data/replay/replay.xitdb \\"$exe" --headless --state replay.state --console "set updates 7" --console quit "$lib" \\"$exe" --headless --state .sketchbook/replay.state --console quit "$lib" 2>err - \\grep -q moved err; test ! -e replay.state; test ! -e replay.state.store; grep -qx '*' .sketchbook/.gitignore + \\grep -q moved err; test ! -e replay.state; grep -qx '*' .sketchbook/.gitignore \\printf 'expect updates 7\n' >check.script \\"$exe" --headless --state .sketchbook/replay.state --script check.script "$lib" \\"$exe" --headless --state replay.state --console quit "$lib" diff --git a/src/abi.zig b/src/abi.zig index f88fca5..aecb654 100644 --- a/src/abi.zig +++ b/src/abi.zig @@ -313,8 +313,8 @@ pub const Voice = struct { /// Slices must stay alive until the host consumes the frame, right after /// `draw` returns. `sketch.Frame` copies the small ones (text, GPU /// descriptions, uniforms) into scratch memory itself; bulk data (vertices, -/// samples, pixels, uploads) must already live in scratch, session or store -/// memory, or be a literal. +/// samples, pixels, uploads) must already live in scratch or session memory, +/// or be a literal. pub const Command = union(enum) { clear: Color, rect: struct { dst: Rect, color: Color }, @@ -751,12 +751,8 @@ pub const Context = struct { /// This process: survives reloads; frees one allocation at a time, and /// releases the rest when Session's layout changes. session: std.mem.Allocator, - /// On disk, next to the state file: survives reloads and restarts, at - /// the same addresses, so Store may point into it. - store: std.mem.Allocator, - /// The sketch's `Session` and `Store` values. + /// The sketch's `Session` value. session_root: [*]u8, - store_root: [*]u8, /// Filled by `draw`, using `scratch`. commands: *std.ArrayList(Command), }; @@ -805,7 +801,6 @@ pub const Info = struct { height: u32, state: Layout, session: Layout, - store: Layout, /// Console completions offered by the sketch. commands: []const []const u8 = &.{}, /// What the sketch was built from: its repository's revision ("jj: @@ -818,7 +813,7 @@ pub const Info = struct { }; /// Which memory `sketch_defaults` fills. -pub const Memory = enum(u8) { state, session, store }; +pub const Memory = enum(u8) { state, session }; /// The entry points of a sketch, exported from libraries under these names. /// What became of a console line offered to a sketch. @@ -840,8 +835,7 @@ pub const Sketch = struct { setup: *const fn (*const Context) callconv(.c) bool, /// Deterministic: new state from state and input, nothing else. update: *const fn (*const Context, [*]u8) callconv(.c) void, - /// Presentation: reads state, may change session and store, appends - /// commands. + /// Presentation: reads state, may change session, appends commands. draw: *const fn (*const Context, [*]const u8) callconv(.c) void, /// A console line the host did not understand. command: *const fn (*const Context, [*]u8, *const []const u8) callconv(.c) Outcome, diff --git a/src/host/Host.zig b/src/host/Host.zig index 05d08f7..75b49b3 100644 --- a/src/host/Host.zig +++ b/src/host/Host.zig @@ -74,7 +74,6 @@ state: memory.StateFile, session: memory.Session, /// A failed resource setup disables app callbacks while keeping host controls alive. session_ready: bool = false, -store: memory.StoreFile, /// Images and buffers the sketch made (`newImage`, `decodeImage`, /// `newBuffer`): Session holds their handles, so they go when it resets. made: struct { images: std.ArrayList(abi.Image) = .empty, buffers: std.ArrayList(abi.Buffer) = .empty } = .{}, @@ -184,12 +183,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std if (state_path.len > 0) options.state = state_path; var state = try memory.StateFile.open(gpa, options.state); errdefer state.close(); - var store_buf: [std.fs.max_path_bytes]u8 = undefined; - var store = memory.StoreFile.open(gpa, try std.mem.print(&store_buf, "{s}.store", .{options.state}), options.fresh) catch |err| { - if (err == error.StoreInUse) std.log.err("{s}.store is open in another process", .{options.state}); - return err; - }; - errdefer store.close(); self.* = .{ .gpa = gpa, @@ -204,7 +197,6 @@ pub fn init(self: *Host, gpa: std.mem.Allocator, io: std.Io, environ: *const std .audio = .{ .gpa = gpa }, .state = state, .session = .init(gpa), - .store = store, .timeline = undefined, .sessions = .{ .gpa = gpa, .io = io }, .sketch = sketch, @@ -267,7 +259,6 @@ pub fn deinit(self: *Host) void { self.written.deinit(self.gpa); self.timeline.deinit(); self.session.deinit(self.gpa); - self.store.close(); self.made.images.deinit(self.gpa); self.made.buffers.deinit(self.gpa); self.state.close(); @@ -658,9 +649,7 @@ fn context(self: *Host) abi.Context { .input = &self.input, .scratch = self.frame_arena.allocator(), .session = self.session.heap.allocator(), - .store = self.store.allocator(), .session_root = self.session.bytes.ptr, - .store_root = self.store.root(), .commands = &self.commands, }; } @@ -774,7 +763,6 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { const adoption = try self.state.adopt(self.gpa, info.state, sketch.defaults); const session_reset = try self.session.adopt(self.gpa, info.session, sketch.defaults); if (session_reset) self.releaseMade(); - const stored = try self.store.adopt(self.gpa, info.store, sketch.defaults); _ = self.info_arena.reset(.retain_capacity); const arena = self.info_arena.allocator(); self.name = try arena.dupe(u8, info.name); @@ -797,10 +785,6 @@ fn adopt(self: *Host, sketch: abi.Sketch) !void { if (!self.session_ready) self.console.visible = true; } if (session_reset and self.reloads > 0) self.print("session layout changed: session reset", .{}); - switch (stored) { - .unchanged, .created => {}, - .migrated => |m| self.print("store layout changed: kept {d} fields, reset {d} to defaults", .{ m.kept, m.reset }), - } } /// Frees what the old Session made: its handles are gone with it. @@ -1155,11 +1139,10 @@ test "session setup runs for restored state and layout reset, not same-layout re const State = extern struct { value: u32 = 1 }; const Session1 = extern struct { ready: u32 = 0 }; const Session2 = extern struct { ready: u32 = 0, extra: u32 = 0 }; - const Store = struct {}; var setup_calls: usize = 0; var init_calls: usize = 0; var fail_setup = false; - const description: abi.Info = .{ .abi = abi.hash, .name = "setup-test", .width = 1, .height = 1, .state = abi.layout.of(State, .state), .session = abi.layout.of(Session1, .session), .store = abi.layout.of(Store, .store) }; + const description: abi.Info = .{ .abi = abi.hash, .name = "setup-test", .width = 1, .height = 1, .state = abi.layout.of(State, .state), .session = abi.layout.of(Session1, .session) }; const changed: abi.Info = blk: { var value = description; value.session = abi.layout.of(Session2, .session); @@ -1175,7 +1158,6 @@ test "session setup runs for restored state and layout reset, not same-layout re switch (kind) { .state => @as(*State, @ptrCast(@alignCast(ptr))).* = .{}, .session => @as(*Session1, @ptrCast(@alignCast(ptr))).* = .{}, - .store => {}, } } fn defaults2(kind: abi.Memory, ptr: [*]u8) callconv(.c) void { @@ -1203,9 +1185,7 @@ test "session setup runs for restored state and layout reset, not same-layout re var tmp = std.testing.tmpDir(.{}); defer tmp.cleanup(); var state_buf: [std.fs.max_path_bytes]u8 = undefined; - var store_buf: [std.fs.max_path_bytes]u8 = undefined; const state_path = try std.mem.print(&state_buf, ".zig-cache/tmp/{s}/setup.state", .{&tmp.sub_path}); - const store_path = try std.mem.print(&store_buf, ".zig-cache/tmp/{s}/setup.store", .{&tmp.sub_path}); { var state = try memory.StateFile.open(a, state_path); defer state.close(); @@ -1218,8 +1198,6 @@ test "session setup runs for restored state and layout reset, not same-layout re host.gpa = a; host.state = try memory.StateFile.open(a, state_path); defer host.state.close(); - host.store = try memory.StoreFile.open(a, store_path, false); - defer host.store.close(); host.session = .init(a); defer host.session.deinit(a); host.info_arena = .init(a); diff --git a/src/host/memory.zig b/src/host/memory.zig index 044fc3b..796fb39 100644 --- a/src/host/memory.zig +++ b/src/host/memory.zig @@ -5,10 +5,6 @@ //! a probe can read it from another process, and a reload with a changed //! layout can carry the old values over field by field. //! -//! `StoreFile`: a heap in a file, mapped at the same address in every run, -//! so what it holds may point into it: the sketch's `Store` and everything -//! allocated for it outlive restarts. -//! //! `Session`: this process's memory, kept across reloads. const std = @import("std"); const posix = std.posix; @@ -515,14 +511,14 @@ fn writeLayout(region: []u8, layout: Layout) !void { h.strings_len = @intCast(strings); } -/// The layout stored in a state or store file's header, or null if it has -/// none. Reads both formats, by magic: a v1 header (`file_magic_v1`, -/// `store_layout_magic_v1`) has no element leaves, so its arrays are whole -/// leaves only, which migrate while their type is unchanged. +/// The layout stored in a state file's header, or null if it has none. +/// Reads both formats, by magic: a v1 header (`file_magic_v1`) has no +/// element leaves, so its arrays are whole leaves only, which migrate while +/// their type is unchanged. pub fn readLayout(arena: std.mem.Allocator, map: []const u8) !?Layout { const h: *const Header = @ptrCast(@alignCast(map.ptr)); if (h.hash == 0 and h.field_count == 0) return null; - const v1 = std.mem.eql(u8, &h.magic, &file_magic_v1) or std.mem.eql(u8, &h.magic, &store_layout_magic_v1); + const v1 = std.mem.eql(u8, &h.magic, &file_magic_v1); const stride: usize = if (v1) @sizeOf(StoredFieldV1) else @sizeOf(StoredField); const strings_start = @sizeOf(Header) + @as(usize, h.field_count) * stride; if (strings_start + h.strings_len > header_len or map.len < header_len) return error.CorruptHeader; @@ -582,208 +578,6 @@ pub fn mapReadOnly(path: []const u8) []align(page) const u8 { return posix.mmap(null, @intCast(len), .{ .READ = true }, .{ .TYPE = .SHARED }, fd, 0) catch &.{}; } -/// Where every store is mapped, in every process: far from where Linux puts -/// executables (0x55...) and mappings (0x7f...). -pub const store_base: usize = 0x2000_0000_0000; -/// The most a store can grow to. Only reserved: pages exist as they are used. -pub const store_reserve: usize = 64 << 30; -/// The allocator's record (`Meta`); unchanged by the layout format. -pub const store_magic = "SKSTORE1".*; -/// The store's layout header, like `file_magic` and `file_magic_v1`. -pub const store_layout_magic = "SKSTORE2".*; -pub const store_layout_magic_v1 = "SKSTORE1".*; -/// The layout region is a state file header; the allocator's own record -/// follows, then the heap. -const meta_offset = header_len; -const heap_offset = 2 * header_len; - -/// A heap in a file, with the sketch's `Store` at its root. Mapped at -/// `store_base` in every run, so pointers it holds into itself stay valid -/// across restarts. Not crash-safe: a crash in the middle of an allocation -/// can leave it inconsistent (`--fresh` starts over). -/// -/// Blocks come in power-of-two sizes, each class with a free list threaded -/// through the free blocks, so growing a list by doubling reuses what it -/// frees. The file grows by mapping more of it in place: nothing moves. -pub const StoreFile = struct { - fd: posix.fd_t, - /// Bytes of the file mapped at `store_base`. - len: usize, - /// The root's layout, owned by `arena`. - layout: Layout, - arena: std.heap.ArenaAllocator, - - const Meta = extern struct { - magic: [8]u8 = store_magic, - base: u64 = store_base, - /// Offset of the first byte never handed out. - top: u64 = heap_offset, - /// Offset of the sketch's Store, or 0 before the first adopt. - root: u64 = 0, - /// Offsets of the first free block of each size class (2^i bytes), 0 - /// when none; a free block's first 8 bytes hold the next one's. - free: [64]u64 = @splat(0), - }; - - pub fn open(gpa: std.mem.Allocator, path: []const u8, fresh: bool) !StoreFile { - const fd = try posix.openat(posix.AT.FDCWD, path, .{ .ACCMODE = .RDWR, .CREAT = true }, 0o600); - errdefer _ = posix.system.close(fd); - // The same file mapped by two processes would be two heaps writing - // over each other. - if (linux.errno(linux.flock(fd, posix.LOCK.EX | posix.LOCK.NB)) != .SUCCESS) return error.StoreInUse; - const reserved = posix.mmap(@ptrFromInt(store_base), store_reserve, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED_NOREPLACE = true }, -1, 0) catch return error.StoreAddressTaken; - // Old kernels treat FIXED_NOREPLACE as a hint. - if (@intFromPtr(reserved.ptr) != store_base) { - posix.munmap(reserved); - return error.StoreAddressTaken; - } - errdefer posix.munmap(reserved); - var self: StoreFile = .{ .fd = fd, .len = 0, .layout = .{}, .arena = .init(gpa) }; - errdefer self.arena.deinit(); - const size: usize = @intCast(try fileSize(fd)); - if (!fresh and size > 0) { - // Checked before mapping, which may lengthen the file: a file - // that is not a store is refused untouched, never wiped. - var m: Meta = undefined; - const read = linux.pread(fd, std.mem.asBytes(&m), @sizeOf(Meta), meta_offset); - if (size < heap_offset or read != @sizeOf(Meta) or !std.mem.eql(u8, &m.magic, &store_magic) or m.base != store_base or m.top > size) return error.NotAStoreFile; - try self.grow(size); - self.layout = try readLayout(self.arena.allocator(), self.region()) orelse .{}; - return self; - } - try self.wipe(); - return self; - } - - pub fn close(self: *StoreFile) void { - posix.munmap(@as([*]align(page) u8, @ptrFromInt(store_base))[0..store_reserve]); - _ = posix.system.close(self.fd); - self.arena.deinit(); - } - - /// Empties the store: no root, nothing allocated. - fn wipe(self: *StoreFile) !void { - // Mapped pages past the new end would fault: map the reserve back. - if (self.len > heap_offset) { - _ = try posix.mmap(@ptrFromInt(store_base + heap_offset), self.len - heap_offset, .{}, .{ .TYPE = .PRIVATE, .ANONYMOUS = true, .NORESERVE = true, .FIXED = true }, -1, 0); - self.len = heap_offset; - } - try truncate(self.fd, heap_offset); - try self.grow(heap_offset); - @memset(self.region(), 0); - self.header().* = .{ .magic = store_layout_magic, .size = 0, .alignment = 1, .hash = 0, .stride = 0, .used = 0, .field_count = 0, .strings_len = 0 }; - self.meta().* = .{}; - self.layout = .{}; - } - - /// Maps the file up to `need` bytes, growing it when it is shorter. - fn grow(self: *StoreFile, need: usize) !void { - if (need <= self.len) return; - const len = std.mem.alignForward(usize, @max(need, self.len * 2, 1 << 20), page); - if (len > store_reserve) return error.StoreFull; - if (try fileSize(self.fd) < len) try truncate(self.fd, len); - _ = try posix.mmap(@ptrFromInt(store_base + self.len), len - self.len, .{ .READ = true, .WRITE = true }, .{ .TYPE = .SHARED, .FIXED = true }, self.fd, self.len); - self.len = len; - } - - fn region(_: *StoreFile) []u8 { - return @as([*]u8, @ptrFromInt(store_base))[0..header_len]; - } - fn header(_: *StoreFile) *Header { - return @ptrFromInt(store_base); - } - fn meta(_: *StoreFile) *Meta { - return @ptrFromInt(store_base + meta_offset); - } - - /// The sketch's Store. - pub fn root(self: *StoreFile) [*]u8 { - const offset = self.meta().root; - return @ptrFromInt(store_base + if (offset == 0) heap_offset else offset); - } - - pub const Adoption = StateFile.Adoption; - - /// Makes the root hold `new`'s layout: the new defaults, then every field - /// whose path and type are unchanged, copied over. Pointer fields keep - /// pointing where they did, since the store never moves. - pub fn adopt(self: *StoreFile, gpa: std.mem.Allocator, new: abi.Layout, defaults: *const fn (abi.Memory, [*]u8) callconv(.c) void) !Adoption { - const m = self.meta(); - if (m.root != 0 and self.layout.hash == new.hash and self.layout.size == new.size) return .unchanged; - const a = self.allocator(); - const alignment: std.mem.Alignment = .fromByteUnits(@max(new.alignment, 1)); - const fresh = a.rawAlloc(@max(new.size, 1), alignment, @returnAddress()) orelse return error.StoreFull; - @memset(fresh[0..@max(new.size, 1)], 0); - defaults(.store, fresh); - - var kept = self.arena; - defer kept.deinit(); - self.arena = .init(gpa); - const old_layout = self.layout; - self.layout = try Layout.copy(self.arena.allocator(), new); - var result: Adoption = .created; - if (m.root != 0) { - const old: [*]u8 = @ptrFromInt(store_base + m.root); - result = .{ .migrated = migrate(old_layout, old[0..old_layout.size], self.layout, fresh[0..new.size]) }; - a.rawFree(old[0..@max(old_layout.size, 1)], .fromByteUnits(@max(old_layout.alignment, 1)), @returnAddress()); - } - self.header().* = .{ .magic = store_layout_magic, .size = new.size, .alignment = new.alignment, .hash = new.hash, .stride = 0, .used = 1, .field_count = 0, .strings_len = 0 }; - try writeLayout(self.region(), self.layout); - m.root = @intFromPtr(fresh) - store_base; - return result; - } - - pub fn allocator(self: *StoreFile) std.mem.Allocator { - return .{ .ptr = self, .vtable = &.{ .alloc = alloc, .resize = resize, .remap = remap, .free = free } }; - } - - fn class(len: usize, alignment: std.mem.Alignment) u6 { - return @intCast(std.math.log2_int_ceil(usize, @max(len, alignment.toByteUnits(), 16))); - } - - fn alloc(ctx: *anyopaque, len: usize, alignment: std.mem.Alignment, _: usize) ?[*]u8 { - const self: *StoreFile = @ptrCast(@alignCast(ctx)); - const m = self.meta(); - const c = class(len, alignment); - const size = @as(usize, 1) << c; - if (m.free[c] != 0) { - const block: [*]u8 = @ptrFromInt(store_base + m.free[c]); - if (alignment.check(@intFromPtr(block))) { - m.free[c] = @as(*const u64, @ptrCast(@alignCast(block))).*; - return block; - } - } - const offset = std.mem.alignForward(usize, m.top, @max(@min(size, page), alignment.toByteUnits())); - self.grow(offset + size) catch return null; - m.top = offset + size; - return @ptrFromInt(store_base + offset); - } - - /// In place within the block's size class. - fn resize(_: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, _: usize) bool { - return class(memory.len, alignment) == class(new_len, alignment); - } - - fn remap(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, new_len: usize, ret: usize) ?[*]u8 { - return if (resize(ctx, memory, alignment, new_len, ret)) memory.ptr else null; - } - - fn free(ctx: *anyopaque, memory: []u8, alignment: std.mem.Alignment, _: usize) void { - const self: *StoreFile = @ptrCast(@alignCast(ctx)); - const m = self.meta(); - const c = class(memory.len, alignment); - @as(*u64, @ptrCast(@alignCast(memory.ptr))).* = m.free[c]; - m.free[c] = @intFromPtr(memory.ptr) - store_base; - } -}; - -fn truncate(fd: posix.fd_t, len: usize) !void { - switch (posix.errno(posix.system.ftruncate(fd, @intCast(len)))) { - .SUCCESS => {}, - else => |err| return posix.unexpectedErrno(err), - } -} - /// Session memory: survives reloads while its layout hash is unchanged. /// Everything allocated from `heap` is released with it. pub const Session = struct { @@ -952,47 +746,6 @@ test "state file migrates live state, and a reopen reads the layout back" { try std.testing.expectEqual(V2{ .level = 1, .score = 99 }, @as(*V2, @ptrCast(file.slot(0).ptr)).*); } -test "a store keeps its root and what it points to across a reopen, and migrates by field" { - const V1 = struct { list: std.ArrayList(u32) = .empty, count: u32 = 0 }; - const V2 = struct { added: u8 = 7, list: std.ArrayList(u32) = .empty }; - const D = struct { - fn v1(_: abi.Memory, p: [*]u8) callconv(.c) void { - @as(*V1, @ptrCast(@alignCast(p))).* = .{}; - } - fn v2(_: abi.Memory, p: [*]u8) callconv(.c) void { - @as(*V2, @ptrCast(@alignCast(p))).* = .{}; - } - }; - var tmp = std.testing.tmpDir(.{}); - defer tmp.cleanup(); - var buf: [std.fs.max_path_bytes]u8 = undefined; - const path = try std.mem.print(&buf, ".zig-cache/tmp/{s}/test.store", .{&tmp.sub_path}); - const gpa = std.testing.allocator; - { - var store = try StoreFile.open(gpa, path, false); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); - const v: *V1 = @ptrCast(@alignCast(store.root())); - for (0..10_000) |i| try v.list.append(store.allocator(), @intCast(i)); - v.count = 3; - try std.testing.expectError(error.StoreInUse, StoreFile.open(gpa, path, false)); - } - { - var store = try StoreFile.open(gpa, path, false); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.unchanged, try store.adopt(gpa, abi.layout.of(V1, .store), D.v1)); - const adoption = try store.adopt(gpa, abi.layout.of(V2, .store), D.v2); - try std.testing.expectEqual(@as(usize, 1), adoption.migrated.reset); - const v: *V2 = @ptrCast(@alignCast(store.root())); - try std.testing.expectEqual(@as(u8, 7), v.added); - try std.testing.expectEqual(@as(u32, 9999), v.list.items[9999]); - v.list.deinit(store.allocator()); - } - var store = try StoreFile.open(gpa, path, true); - defer store.close(); - try std.testing.expectEqual(StoreFile.Adoption.created, try store.adopt(gpa, abi.layout.of(V2, .store), D.v2)); -} - test "arrays migrate element by element: fields, enums by name, shrink and growth" { const E1 = struct { x: f32 = 0, mood: enum(u8) { calm = 1, wild = 2 } = .calm }; const E2 = struct { added: u8 = 9, mood: enum(u8) { wild = 5, calm = 6, sleepy = 7 } = .sleepy, x: f32 = 0 }; diff --git a/src/host/root.zig b/src/host/root.zig index c64af92..629c466 100644 --- a/src/host/root.zig +++ b/src/host/root.zig @@ -95,10 +95,10 @@ pub fn main(init: std.process.Init, linked: ?abi.Sketch) !void { // A scripted run from fresh state works on files of its own, moved to // the given path at exit: two runs of one build step (two `zig build`s - // at once share its output path) never share a state or store file, - // nor the files the sketch appends to, nor fail on the store's lock - // (its sessions are in memory). An export's are removed instead: - // nothing reads them, and a player may have the given ones open. + // at once share its output path) never share a state file, nor the + // files the sketch appends to (its sessions are in memory). An export's + // are removed instead: nothing reads them, and a player may have the + // given ones open. var private_buf: [std.fs.max_path_bytes]u8 = undefined; if (options.state.len == 0 and (script_path != null or options.export_to != null)) options.state = "sketch.state"; const given = options.state; @@ -156,9 +156,9 @@ const usage = /// A state file in a directory named `.sketchbook` (`zig build run`'s): /// the directory is made, with a `.gitignore` of `*` (its sessions database -/// grows, and a jj snapshot would commit it), and the state and store files -/// an older build kept beside it are moved in, once. A file is never -/// replaced: when both exist, nothing moves. +/// grows, and a jj snapshot would commit it), and the state file an older +/// build kept beside it is moved in, once. A file is never replaced: when +/// both exist, nothing moves. fn sketchbookDir(io: std.Io, state: []const u8) void { const dir = std.fs.path.dirname(state) orelse return; if (!std.mem.eql(u8, std.fs.path.basename(dir), ".sketchbook")) return; @@ -167,20 +167,15 @@ fn sketchbookDir(io: std.Io, state: []const u8) void { var buf: [std.fs.max_path_bytes]u8 = undefined; const ignore = std.fmt.bufPrint(&buf, "{s}/.gitignore", .{dir}) catch return; cwd.writeFile(io, .{ .sub_path = ignore, .data = "*\n", .flags = .{ .exclusive = true } }) catch {}; - for ([_][]const u8{ "", ".store" }) |suffix| { - var from_buf: [std.fs.max_path_bytes]u8 = undefined; - var to_buf: [std.fs.max_path_bytes]u8 = undefined; - const parent = std.fs.path.dirname(dir) orelse "."; - const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}{s}", .{ parent, std.fs.path.basename(state), suffix }, 0) catch return; - const to = std.fmt.bufPrintSentinel(&to_buf, "{s}{s}", .{ state, suffix }, 0) catch return; - switch (std.os.linux.errno(std.os.linux.renameat2(std.os.linux.AT.FDCWD, from.ptr, std.os.linux.AT.FDCWD, to.ptr, .{ .NOREPLACE = true }))) { - .SUCCESS => std.debug.print("sketchbook: moved {s} to {s}\n", .{ from, to }), - .EXIST => { - std.debug.print("sketchbook: {s} and {s} both exist: nothing moved, {s} is used\n", .{ from, to, to }); - return; - }, - else => {}, - } + var from_buf: [std.fs.max_path_bytes]u8 = undefined; + var to_buf: [std.fs.max_path_bytes]u8 = undefined; + const parent = std.fs.path.dirname(dir) orelse "."; + const from = std.fmt.bufPrintSentinel(&from_buf, "{s}/{s}", .{ parent, std.fs.path.basename(state) }, 0) catch return; + const to = std.fmt.bufPrintSentinel(&to_buf, "{s}", .{state}, 0) catch return; + switch (std.os.linux.errno(std.os.linux.renameat2(std.os.linux.AT.FDCWD, from.ptr, std.os.linux.AT.FDCWD, to.ptr, .{ .NOREPLACE = true }))) { + .SUCCESS => std.debug.print("sketchbook: moved {s} to {s}\n", .{ from, to }), + .EXIST => std.debug.print("sketchbook: {s} and {s} both exist: nothing moved, {s} is used\n", .{ from, to, to }), + else => {}, } } diff --git a/src/layout.zig b/src/layout.zig index 5e60e22..d61ebf3 100644 --- a/src/layout.zig +++ b/src/layout.zig @@ -19,17 +19,20 @@ //! - Session lives in this process: anything, as long as pointers go into //! memory the host owns (its heaps, mapped files), never into the sketch //! library, which a reload unmaps. -//! - Store is a file mapped at the same address in every run: pointers into -//! the store itself, but no host handles. const std = @import("std"); const abi = @import("abi.zig"); +/// The most a State may be, in bytes: a save holds at most 64 MiB. +pub const max_state = 64 << 20; + pub fn of(comptime T: type, comptime memory: abi.Memory) abi.Layout { return comptime blk: { @setEvalBranchQuota(1_000_000); switch (memory) { - .state => check(T, "State", @typeName(T), false), - .store => check(T, "Store", @typeName(T), true), + .state => { + check(T, @typeName(T)); + if (@sizeOf(T) > max_state) @compileError(std.fmt.comptimePrint("State is {d} bytes, and a save holds at most {d} MiB: lower a capacity (an array, an sk.List), or keep what need not be saved in Session.", .{ @sizeOf(T), max_state >> 20 })); + }, .session => {}, } const Leaves = struct { @@ -155,17 +158,16 @@ pub fn resolve(fields: []const abi.Field, path: []const u8) ?Resolved { return null; } -fn check(comptime T: type, comptime memory: []const u8, comptime path: []const u8, comptime pointers: bool) void { +fn check(comptime T: type, comptime path: []const u8) void { comptime { - if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError(memory ++ " cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session."); + if (T == abi.Image or T == abi.Audio or T == abi.Buffer) @compileError("State cannot hold host handles (" ++ path ++ "): they mean nothing to the next process. Keep them in Session."); switch (@typeInfo(T)) { - // Not followed: types can point to themselves. - .pointer => if (!pointers) @compileError(memory ++ " cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Store or Session."), - .array => |a| check(a.child, memory, path ++ "[]", pointers), - .vector => |v| check(v.child, memory, path ++ "[]", pointers), - .optional => |o| check(o.child, memory, path, pointers), - .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), - .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, memory, path ++ "." ++ name, pointers), + .pointer => @compileError("State cannot hold pointers (" ++ path ++ "): it is saved to disk and outlives every address. Keep indices or offsets, or put it in Session."), + .array => |a| check(a.child, path ++ "[]"), + .vector => |v| check(v.child, path ++ "[]"), + .optional => |o| check(o.child, path), + .@"struct" => |s| for (s.field_names, s.field_types) |name, F| check(F, path ++ "." ++ name), + .@"union" => |u| for (u.field_names, u.field_types) |name, F| check(F, path ++ "." ++ name), else => {}, } } diff --git a/src/sketch/request.zig b/src/sketch/request.zig index 0b8b400..4a8e414 100644 --- a/src/sketch/request.zig +++ b/src/sketch/request.zig @@ -1,5 +1,5 @@ //! Requests from `update` that `draw` carries out once. `update` sees only -//! State and input, so when a key should do something to Session or Store +//! State and input, so when a key should do something to Session //! (start the microphone, open a dialog, seek a stream), State holds the //! request and Session remembers which ones it has handled: //! diff --git a/src/sketch/root.zig b/src/sketch/root.zig index d15046d..6d496bb 100644 --- a/src/sketch/root.zig +++ b/src/sketch/root.zig @@ -7,19 +7,18 @@ //! pub const height = 600; //! pub const State = struct { ... }; // saved, snapshotted, replayed: no pointers //! pub const Session = struct { ... }; // this process: survives reloads -//! pub const Store = struct { ... }; // on disk: survives restarts too //! pub const commands = .{"hello"}; // console completions //! pub fn init(state: *State, host: sk.Host) void //! pub fn setup(session: *Session, init: *sk.Init) !void //! pub fn update(state: *State, in: *const sk.Input) void -//! pub fn draw(state: *const State, session: *Session, store: *Store, f: *sk.Frame) void -//! pub fn command(state: *State, store: *Store, c: *sk.Console, line: []const u8) !bool +//! pub fn draw(state: *const State, session: *Session, f: *sk.Frame) void +//! pub fn command(state: *State, session: *Session, c: *sk.Console, line: []const u8) !bool //! //! Functions take the parameters they name, by type, in any order: `draw` -//! may ask for `*Session`, `*Store`, both or neither. `update` must be +//! may ask for `*Session` or not. `update` must be //! deterministic, so it gets `State` and `Input` only: the host replays //! recorded input through it. `draw` presents state as pictures and sound, -//! and changes only Session and Store. Geometry and colors are duck-typed: +//! and changes only Session. Geometry and colors are duck-typed: //! see `shape`. const std = @import("std"); pub const abi = @import("abi"); @@ -67,7 +66,6 @@ pub const Face = abi.Face; pub const Init = struct { host: Host, session: std.mem.Allocator, - store: std.mem.Allocator, }; /// What `draw` receives: input, host services, memory by lifetime and the @@ -82,14 +80,12 @@ pub const Frame = struct { scratch: std.mem.Allocator, /// For Session: this process. session: std.mem.Allocator, - /// For Store: on disk, across restarts. - store: std.mem.Allocator, /// A library's own state for this frame (canim's table of places), in /// `scratch`: null at each frame's start. user: ?*anyopaque = null, pub fn init(ctx: *const abi.Context) Frame { - return .{ .ctx = ctx, .in = ctx.input, .host = ctx.host, .scratch = ctx.scratch, .session = ctx.session, .store = ctx.store }; + return .{ .ctx = ctx, .in = ctx.input, .host = ctx.host, .scratch = ctx.scratch, .session = ctx.session }; } pub fn emit(f: *Frame, command: abi.Command) void { @@ -291,12 +287,11 @@ fn paint(value: anytype) Color { } /// What `command` receives: somewhere to answer, the current input, and the -/// allocators for Session and Store. +/// allocator for Session. pub const Console = struct { host: Host, in: *const Input, session: std.mem.Allocator, - store: std.mem.Allocator, pub fn log(c: *Console, message: []const u8) void { c.host.log(message); @@ -339,7 +334,6 @@ pub fn Glue(comptime App: type, comptime made: Build) type { return struct { pub const State = if (@hasDecl(App, "State")) App.State else struct {}; pub const Session = if (@hasDecl(App, "Session")) App.Session else struct {}; - pub const Store = if (@hasDecl(App, "Store")) App.Store else struct {}; const build: [abi.build.len:0]u8 = abi.build.*; @@ -350,7 +344,6 @@ pub fn Glue(comptime App: type, comptime made: Build) type { .height = if (@hasDecl(App, "height")) App.height else 600, .state = abi.layout.of(State, .state), .session = abi.layout.of(Session, .session), - .store = abi.layout.of(Store, .store), .commands = if (@hasDecl(App, "commands")) &App.commands else &.{}, .source = made.source ++ " " ++ abi.build, .record = made.record, @@ -377,10 +370,10 @@ pub fn Glue(comptime App: type, comptime made: Build) type { return std.mem.eql(u8, name, n); } }; - if (comptime is.named("update") and (T == *Session or T == *Store)) + if (comptime is.named("update") and T == *Session) @compileError("update must be deterministic: it gets State and Input only, so a replay reproduces it"); if (comptime is.named("setup") and (T == *State or T == *const State or T == *const Input)) - @compileError("setup initializes resources: it gets Session, Store, Init and Host only"); + @compileError("setup initializes resources: it gets Session, Init and Host only"); if (comptime (T == *Init or T == *const Init) and !is.named("setup")) @compileError(name ++ " gets no Init: only setup does"); if (comptime is.named("draw") and T == *State) @compileError("draw only reads State: take *const State"); if (comptime T == *Frame and !is.named("draw")) @compileError(name ++ " gets no Frame: only draw does"); @@ -389,8 +382,6 @@ pub fn Glue(comptime App: type, comptime made: Build) type { @ptrCast(@alignCast(offer.state)) else if (T == *Session or T == *const Session) @ptrCast(@alignCast(offer.ctx.session_root)) - else if (T == *Store or T == *const Store) - @ptrCast(@alignCast(offer.ctx.store_root)) else if (T == *const Input) offer.ctx.input else if (T == Host) @@ -416,7 +407,6 @@ pub fn Glue(comptime App: type, comptime made: Build) type { switch (which) { .state => @as(*State, @ptrCast(@alignCast(ptr))).* = .{}, .session => @as(*Session, @ptrCast(@alignCast(ptr))).* = .{}, - .store => @as(*Store, @ptrCast(@alignCast(ptr))).* = .{}, } } pub fn init(ctx: *const abi.Context, s: [*]u8) callconv(.c) void { @@ -424,7 +414,7 @@ pub fn Glue(comptime App: type, comptime made: Build) type { } pub fn setup(ctx: *const abi.Context) callconv(.c) bool { if (!@hasDecl(App, "setup")) return true; - var resource_init: Init = .{ .host = ctx.host, .session = ctx.session, .store = ctx.store }; + var resource_init: Init = .{ .host = ctx.host, .session = ctx.session }; const result = call(App.setup, "setup", .{ .ctx = ctx, .state = undefined, .resource_init = &resource_init }); if (@typeInfo(@TypeOf(result)) == .error_union) result catch |err| { ctx.host.print("session setup failed: {t}", .{err}); @@ -442,7 +432,7 @@ pub fn Glue(comptime App: type, comptime made: Build) type { } pub fn command(ctx: *const abi.Context, s: [*]u8, line: *const []const u8) callconv(.c) abi.Outcome { if (!@hasDecl(App, "command")) return .unknown; - var c: Console = .{ .host = ctx.host, .in = ctx.input, .session = ctx.session, .store = ctx.store }; + var c: Console = .{ .host = ctx.host, .in = ctx.input, .session = ctx.session }; const taken = call(App.command, "command", .{ .ctx = ctx, .state = s, .console = &c, .line = line.* }) catch |err| { c.print("{t}. Type help for usage.", .{err}); return .failed; @@ -488,7 +478,6 @@ test "setup receives session resources without a Frame and reports errors" { ctx.host = .{ .ctx = &seen, .vtable = &vtable }; ctx.session_root = std.mem.asBytes(&session); ctx.session = std.testing.allocator; - ctx.store = std.testing.allocator; const app = sketch(App, .{}); try std.testing.expect(!app.setup(&ctx)); defer std.testing.allocator.free(session.bytes); diff --git a/src/sketch/web.zig b/src/sketch/web.zig index fd8d7bb..9efad0f 100644 --- a/src/sketch/web.zig +++ b/src/sketch/web.zig @@ -2,7 +2,7 @@ //! the imports below: drawing on a 2D canvas, GPU work on WebGPU, sound and //! the microphone on Web Audio, files through the page. State, `update` and //! `draw` are the same Zig as on native. There is no timeline, and nothing -//! persists: Store lasts as long as the page. +//! persists. const std = @import("std"); const abi = @import("abi"); const root = @import("root.zig"); @@ -82,8 +82,6 @@ fn Driver(comptime App: type) type { var ready = false; var state: G.State = undefined; var session: G.Session = undefined; - // No disk on the web: the store lasts as long as the page. - var store: G.Store = undefined; var events: [1024]abi.Event = undefined; var event_count: usize = 0; var input: abi.Input = .{}; @@ -105,9 +103,7 @@ fn Driver(comptime App: type) type { .input = &input, .scratch = scratch.allocator(), .session = gpa, - .store = gpa, .session_root = std.mem.asBytes(&session), - .store_root = std.mem.asBytes(&store), .commands = &commands, }; } @@ -115,7 +111,6 @@ fn Driver(comptime App: type) type { fn init() callconv(.c) void { G.defaults(.state, std.mem.asBytes(&state)); G.defaults(.session, std.mem.asBytes(&session)); - G.defaults(.store, std.mem.asBytes(&store)); input.width = G.description.width; input.height = G.description.height; if (input.window_width == 0) { -- cgit v1.3