From 93d3561f32f78013edd4fa039a944356a28bcbb4 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Wed, 4 Nov 2026 23:20:10 -0300 Subject: Pipe: a pending pipe's command, directory and inputs live in one arena (as edit_cmd.Pending's do), so its three failure paths and its deinit stop freeing four allocations by hand Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- src/Pipe.zig | 57 ++++++++++++++++++++------------------------------------- 1 file changed, 20 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/src/Pipe.zig b/src/Pipe.zig index c8fb83db..f53a353d 100644 --- a/src/Pipe.zig +++ b/src/Pipe.zig @@ -30,7 +30,7 @@ edit_run: ?*edit_cmd.Pending = null, writes: std.ArrayList(edit_cmd.Write) = .empty, pub fn deinit(pipe: *Pipe, gpa: std.mem.Allocator) void { - if (pipe.wait) |*wait| wait.deinit(gpa); + if (pipe.wait) |*wait| wait.deinit(); if (pipe.edit_run) |pending| pending.deinit(gpa); for (pipe.writes.items) |*w| w.deinit(gpa); pipe.writes.deinit(gpa); @@ -70,6 +70,8 @@ pub const PendingPipe = struct { /// file revision, any other text's contents. revision: u32, sum: u64, + /// What command, cwd and inputs are made in. + arena: std.heap.ArenaAllocator, command: []u8, cwd: []u8, inputs: []selection_pipe.Input, @@ -79,11 +81,8 @@ pub const PendingPipe = struct { how: modal.Normal.PipeBehavior, nranges: u16, - pub fn deinit(wait: *PendingPipe, gpa: std.mem.Allocator) void { - gpa.free(wait.command); - gpa.free(wait.cwd); - for (wait.inputs) |input| gpa.free(@constCast(input.bytes)); - gpa.free(wait.inputs); + pub fn deinit(wait: *PendingPipe) void { + wait.arena.deinit(); wait.* = undefined; } @@ -119,44 +118,27 @@ pub fn submitPipe(p: *Pardes, id: usize) void { const got = t.ranges(content, 0, &ranges); // `!`/`A-!` take no stdin and run ONCE — see `PendingPipe.nranges`. const ninputs = if (how.pipes()) got.n else 1; - const inputs = p.gpa.alloc(selection_pipe.Input, ninputs) catch return; - var made: usize = 0; - for (ranges[0..ninputs], 0..) |range, i| { - const bytes = if (how.pipes()) bytes: { - const lo = @min(range.anchor, range.head); - const hi = @max(range.anchor, range.head); - if (hi > content.len) break; - break :bytes content[lo..hi]; - } else ""; - const copy = p.gpa.dupe(u8, bytes) catch break; - inputs[i] = .{ .bytes = copy }; - made += 1; - } - if (made != ninputs) { - for (inputs[0..made]) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; + var arena_state: std.heap.ArenaAllocator = .init(p.gpa); + const arena = arena_state.allocator(); + const inputs = arena.alloc(selection_pipe.Input, ninputs) catch return arena_state.deinit(); + for (ranges[0..ninputs], inputs) |range, *input| { + const lo = @min(range.anchor, range.head); + const hi = @max(range.anchor, range.head); + if (how.pipes() and hi > content.len) return arena_state.deinit(); + const bytes = if (how.pipes()) content[lo..hi] else ""; + input.* = .{ .bytes = arena.dupe(u8, bytes) catch return arena_state.deinit() }; } // `$` wants a verdict per selection where the runner's is all or // nothing: the command runs in a subshell whose exit status becomes the // output, so the runner always succeeds and the answer says which kept const command_copy = (if (how == .keep) - std.fmt.allocPrint(p.gpa, "(\n{s}\n) >/dev/null; echo $?", .{command}) + std.fmt.allocPrint(arena, "(\n{s}\n) >/dev/null; echo $?", .{command}) else - p.gpa.dupe(u8, command)) catch { - for (inputs) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; - }; - const cwd_copy = p.gpa.dupe(u8, Pardes.paneDir(pane)) catch { - p.gpa.free(command_copy); - for (inputs) |input| p.gpa.free(@constCast(input.bytes)); - p.gpa.free(inputs); - return; - }; + arena.dupe(u8, command)) catch return arena_state.deinit(); + const cwd_copy = arena.dupe(u8, Pardes.paneDir(pane)) catch return arena_state.deinit(); p.pipe.seq +%= 1; - if (p.pipe.wait) |*old| old.deinit(p.gpa); + if (p.pipe.wait) |*old| old.deinit(); p.pipe.wait = .{ .id = p.pipe.seq, .pane = id, @@ -165,6 +147,7 @@ pub fn submitPipe(p: *Pardes, id: usize) void { .column = p.header_column orelse 0, .revision = if (pane.file) |f| f.revision else 0, .sum = std.hash.Wyhash.hash(0, content), + .arena = arena_state, .command = command_copy, .cwd = cwd_copy, .inputs = inputs, @@ -271,7 +254,7 @@ pub fn pipeResponse( if (p.pipe.wait == null or p.pipe.wait.?.id != id) return; var wait = p.pipe.wait.?; p.pipe.wait = null; - defer wait.deinit(p.gpa); + defer wait.deinit(); if (!success or outputs.len != wait.inputs.len) return pipeFailed(p, &wait, failure); -- cgit v1.3