Skip to content

Commit d72f3d3

Browse files
authored
Merge pull request #22691 from squeek502/child-internal-array-list
Document that the `ptr` field of Allocator/Random should not be compared and remove existing comparison
2 parents 3a4bb47 + 4041cc0 commit d72f3d3

File tree

3 files changed

+31
-30
lines changed

3 files changed

+31
-30
lines changed

lib/std/Random.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ pub const RomuTrio = @import("Random/RomuTrio.zig");
2929
pub const SplitMix64 = @import("Random/SplitMix64.zig");
3030
pub const ziggurat = @import("Random/ziggurat.zig");
3131

32+
/// Any comparison of this field may result in illegal behavior, since it may be set to
33+
/// `undefined` in cases where the random implementation does not have any associated
34+
/// state.
3235
ptr: *anyopaque,
3336
fillFn: *const fn (ptr: *anyopaque, buf: []u8) void,
3437

lib/std/mem/Allocator.zig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ const builtin = @import("builtin");
1010
pub const Error = error{OutOfMemory};
1111
pub const Log2Align = math.Log2Int(usize);
1212

13-
// The type erased pointer to the allocator implementation
13+
/// The type erased pointer to the allocator implementation.
14+
/// Any comparison of this field may result in illegal behavior, since it may be set to
15+
/// `undefined` in cases where the allocator implementation does not have any associated
16+
/// state.
1417
ptr: *anyopaque,
1518
vtable: *const VTable,
1619

lib/std/process/Child.zig

Lines changed: 24 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -344,15 +344,17 @@ pub const RunResult = struct {
344344
stderr: []u8,
345345
};
346346

347-
fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) {
347+
fn writeFifoDataToArrayList(allocator: Allocator, list: *std.ArrayListUnmanaged(u8), fifo: *std.io.PollFifo) !void {
348348
if (fifo.head != 0) fifo.realign();
349-
const result = std.ArrayList(u8){
350-
.items = fifo.buf[0..fifo.count],
351-
.capacity = fifo.buf.len,
352-
.allocator = fifo.allocator,
353-
};
354-
fifo.* = std.io.PollFifo.init(fifo.allocator);
355-
return result;
349+
if (list.capacity == 0) {
350+
list.* = .{
351+
.items = fifo.buf[0..fifo.count],
352+
.capacity = fifo.buf.len,
353+
};
354+
fifo.* = std.io.PollFifo.init(fifo.allocator);
355+
} else {
356+
try list.appendSlice(allocator, fifo.buf[0..fifo.count]);
357+
}
356358
}
357359

358360
/// Collect the output from the process's stdout and stderr. Will return once all output
@@ -362,21 +364,16 @@ fn fifoToOwnedArrayList(fifo: *std.io.PollFifo) std.ArrayList(u8) {
362364
/// The process must be started with stdout_behavior and stderr_behavior == .Pipe
363365
pub fn collectOutput(
364366
child: ChildProcess,
365-
stdout: *std.ArrayList(u8),
366-
stderr: *std.ArrayList(u8),
367+
/// Used for `stdout` and `stderr`.
368+
allocator: Allocator,
369+
stdout: *std.ArrayListUnmanaged(u8),
370+
stderr: *std.ArrayListUnmanaged(u8),
367371
max_output_bytes: usize,
368372
) !void {
369373
assert(child.stdout_behavior == .Pipe);
370374
assert(child.stderr_behavior == .Pipe);
371375

372-
// we could make this work with multiple allocators but YAGNI
373-
if (stdout.allocator.ptr != stderr.allocator.ptr or
374-
stdout.allocator.vtable != stderr.allocator.vtable)
375-
{
376-
unreachable; // ChildProcess.collectOutput only supports 1 allocator
377-
}
378-
379-
var poller = std.io.poll(stdout.allocator, enum { stdout, stderr }, .{
376+
var poller = std.io.poll(allocator, enum { stdout, stderr }, .{
380377
.stdout = child.stdout.?,
381378
.stderr = child.stderr.?,
382379
});
@@ -389,8 +386,8 @@ pub fn collectOutput(
389386
return error.StderrStreamTooLong;
390387
}
391388

392-
stdout.* = fifoToOwnedArrayList(poller.fifo(.stdout));
393-
stderr.* = fifoToOwnedArrayList(poller.fifo(.stderr));
389+
try writeFifoDataToArrayList(allocator, stdout, poller.fifo(.stdout));
390+
try writeFifoDataToArrayList(allocator, stderr, poller.fifo(.stderr));
394391
}
395392

396393
pub const RunError = posix.GetCwdError || posix.ReadError || SpawnError || posix.PollError || error{
@@ -420,22 +417,20 @@ pub fn run(args: struct {
420417
child.expand_arg0 = args.expand_arg0;
421418
child.progress_node = args.progress_node;
422419

423-
var stdout = std.ArrayList(u8).init(args.allocator);
424-
var stderr = std.ArrayList(u8).init(args.allocator);
425-
errdefer {
426-
stdout.deinit();
427-
stderr.deinit();
428-
}
420+
var stdout: std.ArrayListUnmanaged(u8) = .empty;
421+
errdefer stdout.deinit(args.allocator);
422+
var stderr: std.ArrayListUnmanaged(u8) = .empty;
423+
errdefer stderr.deinit(args.allocator);
429424

430425
try child.spawn();
431426
errdefer {
432427
_ = child.kill() catch {};
433428
}
434-
try child.collectOutput(&stdout, &stderr, args.max_output_bytes);
429+
try child.collectOutput(args.allocator, &stdout, &stderr, args.max_output_bytes);
435430

436431
return RunResult{
437-
.stdout = try stdout.toOwnedSlice(),
438-
.stderr = try stderr.toOwnedSlice(),
432+
.stdout = try stdout.toOwnedSlice(args.allocator),
433+
.stderr = try stderr.toOwnedSlice(args.allocator),
439434
.term = try child.wait(),
440435
};
441436
}

0 commit comments

Comments
 (0)