aboutsummaryrefslogtreecommitdiff
path: root/src/InternPool.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2025-12-12 19:04:35 -0800
committerAndrew Kelley <andrew@ziglang.org>2025-12-23 22:15:09 -0800
commit1925e0319f1337b4856bd5a181bf4f6d3ac7d428 (patch)
treeb38839cca2604368b01b4312119083a5cdf3d224 /src/InternPool.zig
parentec56696503e702e063af8740a77376b2e7694c29 (diff)
downloadzig-1925e0319f1337b4856bd5a181bf4f6d3ac7d428.tar.gz
zig-1925e0319f1337b4856bd5a181bf4f6d3ac7d428.zip
update lockStderrWriter sites
use the application's Io implementation where possible. This correctly makes writing to stderr cancelable, fallible, and participate in the application's event loop. It also removes one more hard-coded dependency on a secondary Io implementation.
Diffstat (limited to 'src/InternPool.zig')
-rw-r--r--src/InternPool.zig63
1 files changed, 32 insertions, 31 deletions
diff --git a/src/InternPool.zig b/src/InternPool.zig
index 5568c493d9..539cb441c5 100644
--- a/src/InternPool.zig
+++ b/src/InternPool.zig
@@ -1,8 +1,11 @@
//! All interned objects have both a value and a type.
//! This data structure is self-contained.
+const InternPool = @This();
const builtin = @import("builtin");
+
const std = @import("std");
+const Io = std.Io;
const Allocator = std.mem.Allocator;
const assert = std.debug.assert;
const BigIntConst = std.math.big.int.Const;
@@ -11,10 +14,9 @@ const Cache = std.Build.Cache;
const Io = std.Io;
const Limb = std.math.big.Limb;
const Hash = std.hash.Wyhash;
+const Zir = std.zig.Zir;
-const InternPool = @This();
const Zcu = @import("Zcu.zig");
-const Zir = std.zig.Zir;
/// One item per thread, indexed by `tid`, which is dense and unique per thread.
locals: []Local,
@@ -11165,12 +11167,16 @@ pub fn mutateVarInit(ip: *InternPool, io: Io, index: Index, init_index: Index) v
@atomicStore(u32, &extra_items[item.data + std.meta.fieldIndex(Tag.Variable, "init").?], @intFromEnum(init_index), .release);
}
-pub fn dump(ip: *const InternPool) void {
- dumpStatsFallible(ip, std.heap.page_allocator) catch return;
- dumpAllFallible(ip) catch return;
+pub fn dump(ip: *const InternPool, io: Io) Io.Cancelable!void {
+ var buffer: [4096]u8 = undefined;
+ const stderr_writer = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr_writer.interface;
+ try dumpStatsFallible(ip, w, std.heap.page_allocator);
+ try dumpAllFallible(ip, w);
}
-fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
+fn dumpStatsFallible(ip: *const InternPool, w: *Io.Writer, arena: Allocator) anyerror!void {
var items_len: usize = 0;
var extra_len: usize = 0;
var limbs_len: usize = 0;
@@ -11423,18 +11429,13 @@ fn dumpStatsFallible(ip: *const InternPool, arena: Allocator) anyerror!void {
};
counts.sort(SortContext{ .map = &counts });
const len = @min(50, counts.count());
- std.debug.print(" top 50 tags:\n", .{});
+ w.print(" top 50 tags:\n", .{});
for (counts.keys()[0..len], counts.values()[0..len]) |tag, stats| {
- std.debug.print(" {s}: {d} occurrences, {d} total bytes\n", .{
- @tagName(tag), stats.count, stats.bytes,
- });
+ w.print(" {t}: {d} occurrences, {d} total bytes\n", .{ tag, stats.count, stats.bytes });
}
}
-fn dumpAllFallible(ip: *const InternPool) anyerror!void {
- var buffer: [4096]u8 = undefined;
- const stderr_bw, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
+fn dumpAllFallible(ip: *const InternPool, w: *Io.Writer) anyerror!void {
for (ip.locals, 0..) |*local, tid| {
const items = local.shared.items.view();
for (
@@ -11443,12 +11444,12 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
0..,
) |tag, data, index| {
const i = Index.Unwrapped.wrap(.{ .tid = @enumFromInt(tid), .index = @intCast(index) }, ip);
- try stderr_bw.print("${d} = {s}(", .{ i, @tagName(tag) });
+ try w.print("${d} = {s}(", .{ i, @tagName(tag) });
switch (tag) {
.removed => {},
- .simple_type => try stderr_bw.print("{s}", .{@tagName(@as(SimpleType, @enumFromInt(@intFromEnum(i))))}),
- .simple_value => try stderr_bw.print("{s}", .{@tagName(@as(SimpleValue, @enumFromInt(@intFromEnum(i))))}),
+ .simple_type => try w.print("{s}", .{@tagName(@as(SimpleType, @enumFromInt(@intFromEnum(i))))}),
+ .simple_value => try w.print("{s}", .{@tagName(@as(SimpleValue, @enumFromInt(@intFromEnum(i))))}),
.type_int_signed,
.type_int_unsigned,
@@ -11521,23 +11522,27 @@ fn dumpAllFallible(ip: *const InternPool) anyerror!void {
.func_coerced,
.union_value,
.memoized_call,
- => try stderr_bw.print("{d}", .{data}),
+ => try w.print("{d}", .{data}),
.opt_null,
.type_slice,
.only_possible_value,
- => try stderr_bw.print("${d}", .{data}),
+ => try w.print("${d}", .{data}),
}
- try stderr_bw.writeAll(")\n");
+ try w.writeAll(")\n");
}
}
}
-pub fn dumpGenericInstances(ip: *const InternPool, allocator: Allocator) void {
- ip.dumpGenericInstancesFallible(allocator) catch return;
+pub fn dumpGenericInstances(ip: *const InternPool, io: Io, allocator: Allocator) Io.Cancelable!void {
+ var buffer: [4096]u8 = undefined;
+ const stderr_writer = try io.lockStderrWriter(&buffer);
+ defer io.unlockStderrWriter();
+ const w = &stderr_writer.interface;
+ try ip.dumpGenericInstancesFallible(allocator, w);
}
-pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator) anyerror!void {
+pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator, w: *Io.Writer) !void {
var arena_allocator = std.heap.ArenaAllocator.init(allocator);
defer arena_allocator.deinit();
const arena = arena_allocator.allocator();
@@ -11564,10 +11569,6 @@ pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator)
}
}
- var buffer: [4096]u8 = undefined;
- const stderr_bw, _ = std.debug.lockStderrWriter(&buffer);
- defer std.debug.unlockStderrWriter();
-
const SortContext = struct {
values: []std.ArrayList(Index),
pub fn lessThan(ctx: @This(), a_index: usize, b_index: usize) bool {
@@ -11579,19 +11580,19 @@ pub fn dumpGenericInstancesFallible(ip: *const InternPool, allocator: Allocator)
var it = instances.iterator();
while (it.next()) |entry| {
const generic_fn_owner_nav = ip.getNav(ip.funcDeclInfo(entry.key_ptr.*).owner_nav);
- try stderr_bw.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
+ try w.print("{f} ({d}): \n", .{ generic_fn_owner_nav.name.fmt(ip), entry.value_ptr.items.len });
for (entry.value_ptr.items) |index| {
const unwrapped_index = index.unwrap(ip);
const func = ip.extraFuncInstance(unwrapped_index.tid, unwrapped_index.getExtra(ip), unwrapped_index.getData(ip));
const owner_nav = ip.getNav(func.owner_nav);
- try stderr_bw.print(" {f}: (", .{owner_nav.name.fmt(ip)});
+ try w.print(" {f}: (", .{owner_nav.name.fmt(ip)});
for (func.comptime_args.get(ip)) |arg| {
if (arg != .none) {
const key = ip.indexToKey(arg);
- try stderr_bw.print(" {} ", .{key});
+ try w.print(" {} ", .{key});
}
}
- try stderr_bw.writeAll(")\n");
+ try w.writeAll(")\n");
}
}
}