Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,13 @@ By default (`null`/`auto`), ZigZag:
- probes kitty text-sizing support,
- applies terminal/multiplexer heuristics (e.g. tmux/screen/zellij favor legacy width).

### Allocator Lifetimes

`ctx.allocator` is a frame allocator that is reset before each `tick()`.
Use it for temporary values (render strings, per-frame buffers).

For model state that must live across frames, allocate with `ctx.persistent_allocator`.

### Custom Event Loop

For applications that need to do other work between frames (network polling, background processing, etc.), use `start()` + `tick()` instead of `run()`:
Expand Down
1 change: 1 addition & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub fn build(b: *std.Build) void {
"tests/input_tests.zig",
"tests/layout_tests.zig",
"tests/unicode_tests.zig",
"tests/program_tests.zig",
};

const test_step = b.step("test", "Run unit tests");
Expand Down
18 changes: 12 additions & 6 deletions src/core/program.zig
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,15 @@ pub fn Program(comptime Model: type) type {

/// Initialize with custom options
pub fn initWithOptions(allocator: std.mem.Allocator, options: Options) !Self {
var arena = std.heap.ArenaAllocator.init(allocator);

const arena = std.heap.ArenaAllocator.init(allocator);
const self = Self{
.allocator = allocator,
.arena = arena,
.model = undefined,
.terminal = null,
.context = Context.init(arena.allocator(), allocator),
// `self` is returned by value, so don't capture an arena allocator here.
// It would point at this function's stack copy and dangle after return.
.context = Context.init(allocator, allocator),
.options = options,
.running = false,
.start_time = std.time.nanoTimestamp(),
Expand Down Expand Up @@ -172,6 +173,8 @@ pub fn Program(comptime Model: type) type {
self.context.kitty_text_sizing = width_caps.kitty_text_sizing;
unicode.setWidthStrategy(effective_width_strategy);

self.resetFrameAllocator();

// Initialize the model
const init_cmd = self.model.init(&self.context);
try self.processCommand(init_cmd);
Expand Down Expand Up @@ -206,9 +209,7 @@ pub fn Program(comptime Model: type) type {
self.context.elapsed = @intCast(self.last_frame_time - self.start_time);
self.context.frame += 1;

// Reset arena for this frame
_ = self.arena.reset(.retain_capacity);
self.context.allocator = self.arena.allocator();
self.resetFrameAllocator();

// Check for resize
if (self.terminal.?.checkResize()) {
Expand Down Expand Up @@ -483,6 +484,11 @@ pub fn Program(comptime Model: type) type {
}
}

fn resetFrameAllocator(self: *Self) void {
_ = self.arena.reset(.retain_capacity);
self.context.allocator = self.arena.allocator();
}

fn render(self: *Self) !void {
const view_output = self.model.view(&self.context);

Expand Down
35 changes: 35 additions & 0 deletions tests/program_tests.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const std = @import("std");
const testing = std.testing;
const zz = @import("zigzag");

const DummyModel = struct {
pub const Msg = union(enum) {
nop: void,
};

pub fn init(_: *DummyModel, _: *zz.Context) zz.Cmd(Msg) {
return .none;
}

pub fn update(_: *DummyModel, _: Msg, _: *zz.Context) zz.Cmd(Msg) {
return .none;
}

pub fn view(_: *const DummyModel, _: *const zz.Context) []const u8 {
return "";
}
};

test "Program.init context allocator is stable before start and can be rebound to arena" {
var program = try zz.Program(DummyModel).init(testing.allocator);
defer program.deinit();

const backing_ptr = @intFromPtr(testing.allocator.ptr);
const init_context_allocator_ptr = @intFromPtr(program.context.allocator.ptr);
try testing.expectEqual(backing_ptr, init_context_allocator_ptr);

program.context.allocator = program.arena.allocator();
const arena_ptr = @intFromPtr(&program.arena);
const rebound_context_allocator_ptr = @intFromPtr(program.context.allocator.ptr);
try testing.expectEqual(arena_ptr, rebound_context_allocator_ptr);
}