std: Do not allocate the result for ChildProcess.init

Instead, just return ChildProcess directly. This structure does not
require a stable address, so we can put it on the stack just fine. If
someone wants it on the heap they should do.

  const proc = try allocator.create(ChildProcess);
  proc.* = ChildProcess.init(args, allocator);
This commit is contained in:
Jimmi Holst Christensen 2022-04-29 17:07:51 +02:00 committed by Andrew Kelley
parent 0e49142ce4
commit a0a2ce92ca
11 changed files with 21 additions and 54 deletions

View File

@ -971,9 +971,7 @@ pub const Builder = struct {
if (!std.process.can_spawn) if (!std.process.can_spawn)
return error.ExecNotSupported; return error.ExecNotSupported;
const child = std.ChildProcess.init(argv, self.allocator) catch unreachable; var child = std.ChildProcess.init(argv, self.allocator);
defer child.deinit();
child.cwd = cwd; child.cwd = cwd;
child.env_map = env_map; child.env_map = env_map;
@ -1187,9 +1185,7 @@ pub const Builder = struct {
return error.ExecNotSupported; return error.ExecNotSupported;
const max_output_size = 400 * 1024; const max_output_size = 400 * 1024;
const child = try std.ChildProcess.init(argv, self.allocator); var child = std.ChildProcess.init(argv, self.allocator);
defer child.deinit();
child.stdin_behavior = .Ignore; child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe; child.stdout_behavior = .Pipe;
child.stderr_behavior = stderr_behavior; child.stderr_behavior = stderr_behavior;

View File

@ -184,9 +184,7 @@ fn make(step: *Step) !void {
return ExecError.ExecNotSupported; return ExecError.ExecNotSupported;
} }
const child = std.ChildProcess.init(argv, self.builder.allocator) catch unreachable; var child = std.ChildProcess.init(argv, self.builder.allocator);
defer child.deinit();
child.cwd = cwd; child.cwd = cwd;
child.env_map = self.env_map orelse self.builder.env_map; child.env_map = self.env_map orelse self.builder.env_map;

View File

@ -98,10 +98,8 @@ pub const ChildProcess = struct {
}; };
/// First argument in argv is the executable. /// First argument in argv is the executable.
/// On success must call deinit. pub fn init(argv: []const []const u8, allocator: mem.Allocator) ChildProcess {
pub fn init(argv: []const []const u8, allocator: mem.Allocator) !*ChildProcess { return .{
const child = try allocator.create(ChildProcess);
child.* = ChildProcess{
.allocator = allocator, .allocator = allocator,
.argv = argv, .argv = argv,
.pid = undefined, .pid = undefined,
@ -121,8 +119,6 @@ pub const ChildProcess = struct {
.stderr_behavior = StdIo.Inherit, .stderr_behavior = StdIo.Inherit,
.expand_arg0 = .no_expand, .expand_arg0 = .no_expand,
}; };
errdefer allocator.destroy(child);
return child;
} }
pub fn setUserName(self: *ChildProcess, name: []const u8) !void { pub fn setUserName(self: *ChildProcess, name: []const u8) !void {
@ -199,7 +195,7 @@ pub const ChildProcess = struct {
}; };
fn collectOutputPosix( fn collectOutputPosix(
child: *const ChildProcess, child: ChildProcess,
stdout: *std.ArrayList(u8), stdout: *std.ArrayList(u8),
stderr: *std.ArrayList(u8), stderr: *std.ArrayList(u8),
max_output_bytes: usize, max_output_bytes: usize,
@ -298,7 +294,7 @@ pub const ChildProcess = struct {
} }
} }
fn collectOutputWindows(child: *const ChildProcess, outs: [2]*std.ArrayList(u8), max_output_bytes: usize) !void { fn collectOutputWindows(child: ChildProcess, outs: [2]*std.ArrayList(u8), max_output_bytes: usize) !void {
const bump_amt = 512; const bump_amt = 512;
const handles = [_]windows.HANDLE{ const handles = [_]windows.HANDLE{
child.stdout.?.handle, child.stdout.?.handle,
@ -383,9 +379,7 @@ pub const ChildProcess = struct {
max_output_bytes: usize = 50 * 1024, max_output_bytes: usize = 50 * 1024,
expand_arg0: Arg0Expand = .no_expand, expand_arg0: Arg0Expand = .no_expand,
}) !ExecResult { }) !ExecResult {
const child = try ChildProcess.init(args.argv, args.allocator); var child = ChildProcess.init(args.argv, args.allocator);
defer child.deinit();
child.stdin_behavior = .Ignore; child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe; child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe; child.stderr_behavior = .Pipe;
@ -452,10 +446,6 @@ pub const ChildProcess = struct {
return self.term.?; return self.term.?;
} }
pub fn deinit(self: *ChildProcess) void {
self.allocator.destroy(self);
}
fn waitUnwrappedWindows(self: *ChildProcess) !void { fn waitUnwrappedWindows(self: *ChildProcess) !void {
const result = windows.WaitForSingleObjectEx(self.handle, windows.INFINITE, false); const result = windows.WaitForSingleObjectEx(self.handle, windows.INFINITE, false);
@ -1374,8 +1364,7 @@ test "build and call child_process" {
// spawn compiled file as child_process with argument 'hello world' + expect success // spawn compiled file as child_process with argument 'hello world' + expect success
const args = [_][]const u8{ child_path, "hello world" }; const args = [_][]const u8{ child_path, "hello world" };
var child_proc = try ChildProcess.init(&args, allocator); var child_proc = ChildProcess.init(&args, allocator);
defer child_proc.deinit();
const ret_val = try child_proc.spawnAndWait(); const ret_val = try child_proc.spawnAndWait();
try testing.expectEqual(ret_val, .{ .Exited = 0 }); try testing.expectEqual(ret_val, .{ .Exited = 0 });
} }
@ -1385,11 +1374,10 @@ test "creating a child process with stdin and stdout behavior set to StdIo.Pipe"
const testing = std.testing; const testing = std.testing;
const allocator = testing.allocator; const allocator = testing.allocator;
var child_process = try std.ChildProcess.init( var child_process = std.ChildProcess.init(
&[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" }, &[_][]const u8{ testing.zig_exe_path, "fmt", "--stdin" },
allocator, allocator,
); );
defer child_process.deinit();
child_process.stdin_behavior = .Pipe; child_process.stdin_behavior = .Pipe;
child_process.stdout_behavior = .Pipe; child_process.stdout_behavior = .Pipe;

View File

@ -442,10 +442,11 @@ pub fn buildExe(zigexec: []const u8, zigfile: []const u8, binfile: []const u8) !
const flag_emit = "-femit-bin="; const flag_emit = "-femit-bin=";
const cmd_emit = try std.mem.concat(allocator, u8, &[_][]const u8{ flag_emit, binfile }); const cmd_emit = try std.mem.concat(allocator, u8, &[_][]const u8{ flag_emit, binfile });
defer allocator.free(cmd_emit); defer allocator.free(cmd_emit);
const args = [_][]const u8{ zigexec, "build-exe", zigfile, cmd_emit }; const args = [_][]const u8{ zigexec, "build-exe", zigfile, cmd_emit };
var procCompileChild = try std.ChildProcess.init(&args, allocator); var procCompileChild = std.ChildProcess.init(&args, allocator);
defer procCompileChild.deinit();
try procCompileChild.spawn(); try procCompileChild.spawn();
const ret_val = try procCompileChild.wait(); const ret_val = try procCompileChild.wait();
try expectEqual(ret_val, .{ .Exited = 0 }); try expectEqual(ret_val, .{ .Exited = 0 });
} }

View File

@ -3611,9 +3611,7 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
} }
if (std.process.can_spawn) { if (std.process.can_spawn) {
const child = try std.ChildProcess.init(argv.items, arena); var child = std.ChildProcess.init(argv.items, arena);
defer child.deinit();
if (comp.clang_passthrough_mode) { if (comp.clang_passthrough_mode) {
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;

View File

@ -1390,9 +1390,7 @@ fn linkWithLLD(self: *Coff, comp: *Compilation, prog_node: *std.Progress.Node) !
// If possible, we run LLD as a child process because it does not always // If possible, we run LLD as a child process because it does not always
// behave properly as a library, unfortunately. // behave properly as a library, unfortunately.
// https://github.com/ziglang/zig/issues/3825 // https://github.com/ziglang/zig/issues/3825
const child = try std.ChildProcess.init(argv.items, arena); var child = std.ChildProcess.init(argv.items, arena);
defer child.deinit();
if (comp.clang_passthrough_mode) { if (comp.clang_passthrough_mode) {
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;

View File

@ -1754,9 +1754,7 @@ fn linkWithLLD(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node) !v
// If possible, we run LLD as a child process because it does not always // If possible, we run LLD as a child process because it does not always
// behave properly as a library, unfortunately. // behave properly as a library, unfortunately.
// https://github.com/ziglang/zig/issues/3825 // https://github.com/ziglang/zig/issues/3825
const child = try std.ChildProcess.init(argv.items, arena); var child = std.ChildProcess.init(argv.items, arena);
defer child.deinit();
if (comp.clang_passthrough_mode) { if (comp.clang_passthrough_mode) {
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;

View File

@ -2405,9 +2405,7 @@ fn linkWithLLD(self: *Wasm, comp: *Compilation, prog_node: *std.Progress.Node) !
// If possible, we run LLD as a child process because it does not always // If possible, we run LLD as a child process because it does not always
// behave properly as a library, unfortunately. // behave properly as a library, unfortunately.
// https://github.com/ziglang/zig/issues/3825 // https://github.com/ziglang/zig/issues/3825
const child = try std.ChildProcess.init(argv.items, arena); var child = std.ChildProcess.init(argv.items, arena);
defer child.deinit();
if (comp.clang_passthrough_mode) { if (comp.clang_passthrough_mode) {
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;

View File

@ -3012,9 +3012,7 @@ fn runOrTest(
const cmd = try std.mem.join(arena, " ", argv.items); const cmd = try std.mem.join(arena, " ", argv.items);
fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd }); fatal("the following command failed to execve with '{s}':\n{s}", .{ @errorName(err), cmd });
} else if (std.process.can_spawn) { } else if (std.process.can_spawn) {
const child = try std.ChildProcess.init(argv.items, gpa); var child = std.ChildProcess.init(argv.items, gpa);
defer child.deinit();
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit; child.stderr_behavior = .Inherit;
@ -3700,9 +3698,7 @@ pub fn cmdBuild(gpa: Allocator, arena: Allocator, args: []const []const u8) !voi
}; };
if (std.process.can_spawn) { if (std.process.can_spawn) {
const child = try std.ChildProcess.init(child_argv, gpa); var child = std.ChildProcess.init(child_argv, gpa);
defer child.deinit();
child.stdin_behavior = .Inherit; child.stdin_behavior = .Inherit;
child.stdout_behavior = .Inherit; child.stdout_behavior = .Inherit;
child.stderr_behavior = .Inherit; child.stderr_behavior = .Inherit;

View File

@ -369,9 +369,7 @@ pub fn buildImportLib(comp: *Compilation, lib_name: []const u8) !void {
} }
if (std.process.can_spawn) { if (std.process.can_spawn) {
const child = try std.ChildProcess.init(&args, arena); var child = std.ChildProcess.init(&args, arena);
defer child.deinit();
child.stdin_behavior = .Ignore; child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe; child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe; child.stderr_behavior = .Pipe;

View File

@ -731,9 +731,7 @@ pub const StackTracesContext = struct {
return ExecError.ExecNotSupported; return ExecError.ExecNotSupported;
} }
const child = std.ChildProcess.init(args.items, b.allocator) catch unreachable; var child = std.ChildProcess.init(args.items, b.allocator);
defer child.deinit();
child.stdin_behavior = .Ignore; child.stdin_behavior = .Ignore;
child.stdout_behavior = .Pipe; child.stdout_behavior = .Pipe;
child.stderr_behavior = .Pipe; child.stderr_behavior = .Pipe;