aboutsummaryrefslogtreecommitdiff
path: root/lib/std/heap/debug_allocator.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrewrk@noreply.codeberg.org>2025-12-27 14:10:46 +0100
committerAndrew Kelley <andrewrk@noreply.codeberg.org>2025-12-27 14:10:46 +0100
commite55e6b5528bb2f01de242fcf32b172e244e98e74 (patch)
tree3a5eb3193d3d192c54ab0c2b7295a7f21861c27e /lib/std/heap/debug_allocator.zig
parentc3f2de5e519926eb0029062fe8e782a6f9df9c05 (diff)
parent60a1ba0a8f3517356fa2941462f002a7f580545b (diff)
downloadzig-e55e6b5528bb2f01de242fcf32b172e244e98e74.tar.gz
zig-e55e6b5528bb2f01de242fcf32b172e244e98e74.zip
Merge pull request 'std: migrate all `fs` APIs to `Io`' (#30232) from std.Io-fs into master
Reviewed-on: https://codeberg.org/ziglang/zig/pulls/30232
Diffstat (limited to 'lib/std/heap/debug_allocator.zig')
-rw-r--r--lib/std/heap/debug_allocator.zig55
1 files changed, 26 insertions, 29 deletions
diff --git a/lib/std/heap/debug_allocator.zig b/lib/std/heap/debug_allocator.zig
index 3183becd82..24f1554544 100644
--- a/lib/std/heap/debug_allocator.zig
+++ b/lib/std/heap/debug_allocator.zig
@@ -84,7 +84,7 @@ const builtin = @import("builtin");
const StackTrace = std.builtin.StackTrace;
const std = @import("std");
-const log = std.log.scoped(.gpa);
+const log = std.log.scoped(.DebugAllocator);
const math = std.math;
const assert = std.debug.assert;
const mem = std.mem;
@@ -425,7 +425,6 @@ pub fn DebugAllocator(comptime config: Config) type {
bucket: *BucketHeader,
size_class_index: usize,
used_bits_count: usize,
- tty_config: std.Io.tty.Config,
) usize {
const size_class = @as(usize, 1) << @as(Log2USize, @intCast(size_class_index));
const slot_count = slot_counts[size_class_index];
@@ -445,7 +444,7 @@ pub fn DebugAllocator(comptime config: Config) type {
addr,
std.debug.FormatStackTrace{
.stack_trace = stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
leaks += 1;
@@ -460,14 +459,12 @@ pub fn DebugAllocator(comptime config: Config) type {
pub fn detectLeaks(self: *Self) usize {
var leaks: usize = 0;
- const tty_config: std.Io.tty.Config = .detect(.stderr());
-
for (self.buckets, 0..) |init_optional_bucket, size_class_index| {
var optional_bucket = init_optional_bucket;
const slot_count = slot_counts[size_class_index];
const used_bits_count = usedBitsCount(slot_count);
while (optional_bucket) |bucket| {
- leaks += detectLeaksInBucket(bucket, size_class_index, used_bits_count, tty_config);
+ leaks += detectLeaksInBucket(bucket, size_class_index, used_bits_count);
optional_bucket = bucket.prev;
}
}
@@ -480,7 +477,7 @@ pub fn DebugAllocator(comptime config: Config) type {
@intFromPtr(large_alloc.bytes.ptr),
std.debug.FormatStackTrace{
.stack_trace = stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
leaks += 1;
@@ -534,21 +531,21 @@ pub fn DebugAllocator(comptime config: Config) type {
}
fn reportDoubleFree(ret_addr: usize, alloc_stack_trace: StackTrace, free_stack_trace: StackTrace) void {
+ @branchHint(.cold);
var addr_buf: [stack_n]usize = undefined;
const second_free_stack_trace = std.debug.captureCurrentStackTrace(.{ .first_address = ret_addr }, &addr_buf);
- const tty_config: std.Io.tty.Config = .detect(.stderr());
log.err("Double free detected. Allocation: {f} First free: {f} Second free: {f}", .{
std.debug.FormatStackTrace{
.stack_trace = alloc_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = second_free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
@@ -588,19 +585,19 @@ pub fn DebugAllocator(comptime config: Config) type {
}
if (config.safety and old_mem.len != entry.value_ptr.bytes.len) {
+ @branchHint(.cold);
var addr_buf: [stack_n]usize = undefined;
const free_stack_trace = std.debug.captureCurrentStackTrace(.{ .first_address = ret_addr }, &addr_buf);
- const tty_config: std.Io.tty.Config = .detect(.stderr());
log.err("Allocation size {d} bytes does not match free size {d}. Allocation: {f} Free: {f}", .{
entry.value_ptr.bytes.len,
old_mem.len,
std.debug.FormatStackTrace{
.stack_trace = entry.value_ptr.getStackTrace(.alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
@@ -701,19 +698,19 @@ pub fn DebugAllocator(comptime config: Config) type {
}
if (config.safety and old_mem.len != entry.value_ptr.bytes.len) {
+ @branchHint(.cold);
var addr_buf: [stack_n]usize = undefined;
const free_stack_trace = std.debug.captureCurrentStackTrace(.{ .first_address = ret_addr }, &addr_buf);
- const tty_config: std.Io.tty.Config = .detect(.stderr());
log.err("Allocation size {d} bytes does not match free size {d}. Allocation: {f} Free: {f}", .{
entry.value_ptr.bytes.len,
old_mem.len,
std.debug.FormatStackTrace{
.stack_trace = entry.value_ptr.getStackTrace(.alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
@@ -935,32 +932,32 @@ pub fn DebugAllocator(comptime config: Config) type {
var addr_buf: [stack_n]usize = undefined;
const free_stack_trace = std.debug.captureCurrentStackTrace(.{ .first_address = return_address }, &addr_buf);
if (old_memory.len != requested_size) {
- const tty_config: std.Io.tty.Config = .detect(.stderr());
+ @branchHint(.cold);
log.err("Allocation size {d} bytes does not match free size {d}. Allocation: {f} Free: {f}", .{
requested_size,
old_memory.len,
std.debug.FormatStackTrace{
.stack_trace = bucketStackTrace(bucket, slot_count, slot_index, .alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
if (alignment != slot_alignment) {
- const tty_config: std.Io.tty.Config = .detect(.stderr());
+ @branchHint(.cold);
log.err("Allocation alignment {d} does not match free alignment {d}. Allocation: {f} Free: {f}", .{
slot_alignment.toByteUnits(),
alignment.toByteUnits(),
std.debug.FormatStackTrace{
.stack_trace = bucketStackTrace(bucket, slot_count, slot_index, .alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
@@ -1044,32 +1041,32 @@ pub fn DebugAllocator(comptime config: Config) type {
var addr_buf: [stack_n]usize = undefined;
const free_stack_trace = std.debug.captureCurrentStackTrace(.{ .first_address = return_address }, &addr_buf);
if (memory.len != requested_size) {
- const tty_config: std.Io.tty.Config = .detect(.stderr());
+ @branchHint(.cold);
log.err("Allocation size {d} bytes does not match free size {d}. Allocation: {f} Free: {f}", .{
requested_size,
memory.len,
std.debug.FormatStackTrace{
.stack_trace = bucketStackTrace(bucket, slot_count, slot_index, .alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}
if (alignment != slot_alignment) {
- const tty_config: std.Io.tty.Config = .detect(.stderr());
+ @branchHint(.cold);
log.err("Allocation alignment {d} does not match free alignment {d}. Allocation: {f} Free: {f}", .{
slot_alignment.toByteUnits(),
alignment.toByteUnits(),
std.debug.FormatStackTrace{
.stack_trace = bucketStackTrace(bucket, slot_count, slot_index, .alloc),
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
std.debug.FormatStackTrace{
.stack_trace = free_stack_trace,
- .tty_config = tty_config,
+ .terminal_mode = std.log.terminalMode(),
},
});
}