diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2020-06-27 18:21:00 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-06-27 18:21:00 -0400 |
| commit | 0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395 (patch) | |
| tree | d0dd3f43e534528d5c99ae28506c846a7d9063d0 /lib/std/heap/logging_allocator.zig | |
| parent | 626b5eccab7264e579ce58f56be5fbc3aa42efc4 (diff) | |
| parent | a728436992415d1bce44b0c63938f6443a4e9a11 (diff) | |
| download | zig-0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395.tar.gz zig-0cfe8e5d6ff06eed0cde6aed0c009a58ceffc395.zip | |
Merge pull request #5064 from marler8997/newAllocator
new allocator interface
Diffstat (limited to 'lib/std/heap/logging_allocator.zig')
| -rw-r--r-- | lib/std/heap/logging_allocator.zig | 61 |
1 files changed, 37 insertions, 24 deletions
diff --git a/lib/std/heap/logging_allocator.zig b/lib/std/heap/logging_allocator.zig index 0d15986a76..b521515a79 100644 --- a/lib/std/heap/logging_allocator.zig +++ b/lib/std/heap/logging_allocator.zig @@ -15,39 +15,45 @@ pub fn LoggingAllocator(comptime OutStreamType: type) type { pub fn init(parent_allocator: *Allocator, out_stream: OutStreamType) Self { return Self{ .allocator = Allocator{ - .reallocFn = realloc, - .shrinkFn = shrink, + .allocFn = alloc, + .resizeFn = resize, }, .parent_allocator = parent_allocator, .out_stream = out_stream, }; } - fn realloc(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) ![]u8 { + fn alloc(allocator: *Allocator, len: usize, ptr_align: u29, len_align: u29) error{OutOfMemory}![]u8 { const self = @fieldParentPtr(Self, "allocator", allocator); - if (old_mem.len == 0) { - self.out_stream.print("allocation of {} ", .{new_size}) catch {}; - } else { - self.out_stream.print("resize from {} to {} ", .{ old_mem.len, new_size }) catch {}; - } - const result = self.parent_allocator.reallocFn(self.parent_allocator, old_mem, old_align, new_size, new_align); + self.out_stream.print("alloc : {}", .{len}) catch {}; + const result = self.parent_allocator.callAllocFn(len, ptr_align, len_align); if (result) |buff| { - self.out_stream.print("success!\n", .{}) catch {}; + self.out_stream.print(" success!\n", .{}) catch {}; } else |err| { - self.out_stream.print("failure!\n", .{}) catch {}; + self.out_stream.print(" failure!\n", .{}) catch {}; } return result; } - fn shrink(allocator: *Allocator, old_mem: []u8, old_align: u29, new_size: usize, new_align: u29) []u8 { + fn resize(allocator: *Allocator, buf: []u8, new_len: usize, len_align: u29) error{OutOfMemory}!usize { const self = @fieldParentPtr(Self, "allocator", allocator); - const result = self.parent_allocator.shrinkFn(self.parent_allocator, old_mem, old_align, new_size, new_align); - if (new_size == 0) { - self.out_stream.print("free of {} bytes success!\n", .{old_mem.len}) catch {}; + if (new_len == 0) { + self.out_stream.print("free : {}\n", .{buf.len}) catch {}; + } else if (new_len <= buf.len) { + self.out_stream.print("shrink: {} to {}\n", .{buf.len, new_len}) catch {}; } else { - self.out_stream.print("shrink from {} bytes to {} bytes success!\n", .{ old_mem.len, new_size }) catch {}; + self.out_stream.print("expand: {} to {}", .{ buf.len, new_len }) catch {}; + } + if (self.parent_allocator.callResizeFn(buf, new_len, len_align)) |resized_len| { + if (new_len > buf.len) { + self.out_stream.print(" success!\n", .{}) catch {}; + } + return resized_len; + } else |e| { + std.debug.assert(new_len > buf.len); + self.out_stream.print(" failure!\n", .{}) catch {}; + return e; } - return result; } }; } @@ -60,17 +66,24 @@ pub fn loggingAllocator( } test "LoggingAllocator" { - var buf: [255]u8 = undefined; - var fbs = std.io.fixedBufferStream(&buf); + var log_buf: [255]u8 = undefined; + var fbs = std.io.fixedBufferStream(&log_buf); - const allocator = &loggingAllocator(std.testing.allocator, fbs.outStream()).allocator; + var allocator_buf: [10]u8 = undefined; + var fixedBufferAllocator = std.mem.validationWrap(std.heap.FixedBufferAllocator.init(&allocator_buf)); + const allocator = &loggingAllocator(&fixedBufferAllocator.allocator, fbs.outStream()).allocator; - const ptr = try allocator.alloc(u8, 10); - allocator.free(ptr); + var a = try allocator.alloc(u8, 10); + a.len = allocator.shrinkBytes(a, 5, 0); + std.debug.assert(a.len == 5); + std.testing.expectError(error.OutOfMemory, allocator.callResizeFn(a, 20, 0)); + allocator.free(a); std.testing.expectEqualSlices(u8, - \\allocation of 10 success! - \\free of 10 bytes success! + \\alloc : 10 success! + \\shrink: 10 to 5 + \\expand: 5 to 20 failure! + \\free : 5 \\ , fbs.getWritten()); } |
