| ... | ... | @@ -27,15 +27,15 @@ pub const LoggingAllocator = struct { |
| 27 | 27 | fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { |
| 28 | 28 | const self = @fieldParentPtr(Self, "allocator", allocator); |
| 29 | 29 | if (old_mem.len == 0) { |
| 30 | | self.out_stream.print("allocation of {} ", new_size) catch {}; |
| 30 | self.out_stream.print("allocation of {} ", .{new_size}) catch {}; |
| 31 | 31 | } else { |
| 32 | | self.out_stream.print("resize from {} to {} ", old_mem.len, new_size) catch {}; |
| 32 | self.out_stream.print("resize from {} to {} ", .{ old_mem.len, new_size }) catch {}; |
| 33 | 33 | } |
| 34 | 34 | const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align); |
| 35 | 35 | if (result) |buff| { |
| 36 | | self.out_stream.print("success!\n") catch {}; |
| 36 | self.out_stream.print("success!\n", .{}) catch {}; |
| 37 | 37 | } else |err| { |
| 38 | | self.out_stream.print("failure!\n") catch {}; |
| 38 | self.out_stream.print("failure!\n", .{}) catch {}; |
| 39 | 39 | } |
| 40 | 40 | return result; |
| 41 | 41 | } |
| ... | ... | @@ -44,10 +44,27 @@ pub const LoggingAllocator = struct { |
| 44 | 44 | const self = @fieldParentPtr(Self, "allocator", allocator); |
| 45 | 45 | const result = self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align); |
| 46 | 46 | if (new_size == 0) { |
| 47 | | self.out_stream.print("free of {} bytes success!\n", old_mem.len) catch {}; |
| 47 | self.out_stream.print("free of {} bytes success!\n", .{old_mem.len}) catch {}; |
| 48 | 48 | } else { |
| 49 | | self.out_stream.print("shrink from {} bytes to {} bytes success!\n", old_mem.len, new_size) catch {}; |
| 49 | self.out_stream.print("shrink from {} bytes to {} bytes success!\n", .{ old_mem.len, new_size }) catch {}; |
| 50 | 50 | } |
| 51 | 51 | return result; |
| 52 | 52 | } |
| 53 | 53 | }; |
| 54 | |
| 55 | test "LoggingAllocator" { |
| 56 | var buf: [255]u8 = undefined; |
| 57 | var slice_stream = std.io.SliceOutStream.init(buf[0..]); |
| 58 | const stream = &slice_stream.stream; |
| 59 | |
| 60 | const allocator = &LoggingAllocator.init(std.heap.page_allocator, @ptrCast(*AnyErrorOutStream, stream)).allocator; |
| 61 | |
| 62 | const ptr = try allocator.alloc(u8, 10); |
| 63 | allocator.free(ptr); |
| 64 | |
| 65 | std.testing.expectEqualSlices(u8, |
| 66 | \\allocation of 10 success! |
| 67 | \\free of 10 bytes success! |
| 68 | \\ |
| 69 | , slice_stream.getWritten()); |
| 70 | } |