diff options
Diffstat (limited to 'std/heap.zig')
| -rw-r--r-- | std/heap.zig | 49 |
1 files changed, 2 insertions, 47 deletions
diff --git a/std/heap.zig b/std/heap.zig index b3dc79b84d..d14f34dd6e 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -8,6 +8,8 @@ const builtin = @import("builtin"); const c = std.c; const maxInt = std.math.maxInt; +pub const LoggingAllocator = @import("heap/logging_allocator.zig").LoggingAllocator; + const Allocator = mem.Allocator; pub const c_allocator = &c_allocator_state; @@ -713,53 +715,6 @@ pub fn StackFallbackAllocator(comptime size: usize) type { }; } -pub const NoErrorOutStream = std.io.OutStream(error{}); -pub const LoggingAllocator = struct { - allocator: Allocator, - parentAllocator: *Allocator, - outStream: *NoErrorOutStream, - - const Self = @This(); - - pub fn init(parentAllocator: *Allocator, outStream: *NoErrorOutStream) Self { - return Self{ - .allocator = Allocator{ - .reallocFn = realloc, - .shrinkFn = shrink, - }, - .parentAllocator = parentAllocator, - .outStream = outStream, - }; - } - - fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { - const self = @fieldParentPtr(Self, "allocator", allocator); - if (old_mem.len == 0) { - self.outStream.print("allocation of {} ", new_size) catch unreachable; - } else { - self.outStream.print("resize from {} to {} ", old_mem.len, new_size) catch unreachable; - } - const result = self.parentAllocator.reallocFn(self.parentAllocator, old_mem, old_align, new_size, new_align); - if (result) |buff| { - self.outStream.print("success!\n") catch unreachable; - } else |err| { - self.outStream.print("failure!\n") catch unreachable; - } - return result; - } - - fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { - const self = @fieldParentPtr(Self, "allocator", allocator); - const result = self.parentAllocator.shrinkFn(self.parentAllocator, old_mem, old_align, new_size, new_align); - if (new_size == 0) { - self.outStream.print("free of {} bytes success!\n", old_mem.len) catch unreachable; - } else { - self.outStream.print("shrink from {} bytes to {} bytes success!\n", old_mem.len, new_size) catch unreachable; - } - return result; - } -}; - test "c_allocator" { if (builtin.link_libc) { var slice = try c_allocator.alloc(u8, 50); |
