authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-30 20:15:26-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-01-30 20:15:26-07:00
log766b315b3888f0f9ac1ece69131cdf23f98b2c14
tree806237a7b9710f0ce1467fff3301bc2d2094c447
parent0808d98e10c5fea27cebf912c6296b760c2b837b

std.GeneralPurposeAllocator: logging improvements

It now uses the log scope "gpa" instead of "std". Additionally, there is a new config option `verbose_log` which enables info log messages for every allocation. Can be useful when debugging. This option is off by default.

1 files changed, 27 insertions(+), 1 deletions(-)

lib/std/heap/general_purpose_allocator.zig+27-1
......@@ -98,7 +98,7 @@
9898//! in a `std.HashMap` using the backing allocator.
9999
100100const std = @import("std");
101const log = std.log.scoped(.std);
101const log = std.log.scoped(.gpa);
102102const math = std.math;
103103const assert = std.debug.assert;
104104const mem = std.mem;
......@@ -162,6 +162,9 @@ pub const Config = struct {
162162 /// logged error messages with stack trace details. The downside is that every allocation
163163 /// will be leaked!
164164 never_unmap: bool = false,
165
166 /// Enables emitting info messages with the size and address of every allocation.
167 verbose_log: bool = false,
165168};
166169
167170pub fn GeneralPurposeAllocator(comptime config: Config) type {
......@@ -454,10 +457,19 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
454457 const result_len = try self.backing_allocator.resizeFn(self.backing_allocator, old_mem, old_align, new_size, len_align, ret_addr);
455458
456459 if (result_len == 0) {
460 if (config.verbose_log) {
461 log.info("large free {d} bytes at {*}", .{ old_mem.len, old_mem.ptr });
462 }
463
457464 self.large_allocations.removeAssertDiscard(@ptrToInt(old_mem.ptr));
458465 return 0;
459466 }
460467
468 if (config.verbose_log) {
469 log.info("large resize {d} bytes at {*} to {d}", .{
470 old_mem.len, old_mem.ptr, new_size,
471 });
472 }
461473 entry.value.bytes = old_mem.ptr[0..result_len];
462474 collectStackTrace(ret_addr, &entry.value.stack_addresses);
463475 return result_len;
......@@ -568,6 +580,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
568580 } else {
569581 @memset(old_mem.ptr, undefined, old_mem.len);
570582 }
583 if (config.verbose_log) {
584 log.info("small free {d} bytes at {*}", .{ old_mem.len, old_mem.ptr });
585 }
571586 return @as(usize, 0);
572587 }
573588 const new_aligned_size = math.max(new_size, old_align);
......@@ -576,6 +591,11 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
576591 if (old_mem.len > new_size) {
577592 @memset(old_mem.ptr + new_size, undefined, old_mem.len - new_size);
578593 }
594 if (config.verbose_log) {
595 log.info("small resize {d} bytes at {*} to {d}", .{
596 old_mem.len, old_mem.ptr, new_size,
597 });
598 }
579599 return new_size;
580600 }
581601 return error.OutOfMemory;
......@@ -623,6 +643,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
623643 gop.entry.value.bytes = slice;
624644 collectStackTrace(ret_addr, &gop.entry.value.stack_addresses);
625645
646 if (config.verbose_log) {
647 log.info("large alloc {d} bytes at {*}", .{ slice.len, slice.ptr });
648 }
626649 return slice;
627650 }
628651
......@@ -632,6 +655,9 @@ pub fn GeneralPurposeAllocator(comptime config: Config) type {
632655
633656 const new_size_class = math.ceilPowerOfTwoAssert(usize, new_aligned_size);
634657 const ptr = try self.allocSlot(new_size_class, ret_addr);
658 if (config.verbose_log) {
659 log.info("small alloc {d} bytes at {*}", .{ len, ptr });
660 }
635661 return ptr[0..len];
636662 }
637663