diff options
Diffstat (limited to 'lib/std/heap/logging_allocator.zig')
| -rw-r--r-- | lib/std/heap/logging_allocator.zig | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/lib/std/heap/logging_allocator.zig b/lib/std/heap/logging_allocator.zig index 7920138e9b..7885571ab3 100644 --- a/lib/std/heap/logging_allocator.zig +++ b/lib/std/heap/logging_allocator.zig @@ -9,22 +9,22 @@ const Allocator = std.mem.Allocator; /// This allocator is used in front of another allocator and logs to the provided stream /// on every call to the allocator. Stream errors are ignored. /// If https://github.com/ziglang/zig/issues/2586 is implemented, this API can be improved. -pub fn LoggingAllocator(comptime OutStreamType: type) type { +pub fn LoggingAllocator(comptime Writer: type) type { return struct { allocator: Allocator, parent_allocator: *Allocator, - out_stream: OutStreamType, + writer: Writer, const Self = @This(); - pub fn init(parent_allocator: *Allocator, out_stream: OutStreamType) Self { + pub fn init(parent_allocator: *Allocator, writer: Writer) Self { return Self{ .allocator = Allocator{ .allocFn = alloc, .resizeFn = resize, }, .parent_allocator = parent_allocator, - .out_stream = out_stream, + .writer = writer, }; } @@ -36,12 +36,12 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type { ra: usize, ) error{OutOfMemory}![]u8 { const self = @fieldParentPtr(Self, "allocator", allocator); - self.out_stream.print("alloc : {}", .{len}) catch {}; + self.writer.print("alloc : {}", .{len}) catch {}; const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ra); if (result) |buff| { - self.out_stream.print(" success!\n", .{}) catch {}; + self.writer.print(" success!\n", .{}) catch {}; } else |err| { - self.out_stream.print(" failure!\n", .{}) catch {}; + self.writer.print(" failure!\n", .{}) catch {}; } return result; } @@ -56,20 +56,20 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type { ) error{OutOfMemory}!usize { const self = @fieldParentPtr(Self, "allocator", allocator); if (new_len == 0) { - self.out_stream.print("free : {}\n", .{buf.len}) catch {}; + self.writer.print("free : {}\n", .{buf.len}) catch {}; } else if (new_len <= buf.len) { - self.out_stream.print("shrink: {} to {}\n", .{ buf.len, new_len }) catch {}; + self.writer.print("shrink: {} to {}\n", .{ buf.len, new_len }) catch {}; } else { - self.out_stream.print("expand: {} to {}", .{ buf.len, new_len }) catch {}; + self.writer.print("expand: {} to {}", .{ buf.len, new_len }) catch {}; } if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ra)) |resized_len| { if (new_len > buf.len) { - self.out_stream.print(" success!\n", .{}) catch {}; + self.writer.print(" success!\n", .{}) catch {}; } return resized_len; } else |e| { std.debug.assert(new_len > buf.len); - self.out_stream.print(" failure!\n", .{}) catch {}; + self.writer.print(" failure!\n", .{}) catch {}; return e; } } @@ -78,9 +78,9 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type { pub fn loggingAllocator( parent_allocator: *Allocator, - out_stream: anytype, -) LoggingAllocator(@TypeOf(out_stream)) { - return LoggingAllocator(@TypeOf(out_stream)).init(parent_allocator, out_stream); + writer: anytype, +) LoggingAllocator(@TypeOf(writer)) { + return LoggingAllocator(@TypeOf(writer)).init(parent_allocator, writer); } test "LoggingAllocator" { |
