allocgate: update code to use new interface

This commit is contained in:
Lee Cannon 2021-11-03 12:49:31 +00:00
parent 02e5e0ba1f
commit 23866b1f81
No known key found for this signature in database
GPG Key ID: 91F1CB2A0464E7B0
2 changed files with 7 additions and 7 deletions

View File

@ -1192,7 +1192,7 @@ test "bug 9995 fix, large allocs count requested size not backing size" {
// with AtLeast, buffer likely to be larger than requested, especially when shrinking // with AtLeast, buffer likely to be larger than requested, especially when shrinking
var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){}; var gpa = GeneralPurposeAllocator(.{ .enable_memory_limit = true }){};
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var buf = try allocator.allocAdvanced(u8, 1, page_size + 1, .at_least); var buf = try allocator.allocAdvanced(u8, 1, page_size + 1, .at_least);
try std.testing.expect(gpa.total_requested_bytes == page_size + 1); try std.testing.expect(gpa.total_requested_bytes == page_size + 1);
buf = try allocator.reallocAtLeast(buf, 1); buf = try allocator.reallocAtLeast(buf, 1);

View File

@ -109,11 +109,14 @@ pub fn tracyAllocator(allocator: std.mem.Allocator) TracyAllocator(null) {
pub fn TracyAllocator(comptime name: ?[:0]const u8) type { pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
return struct { return struct {
allocator: std.mem.Allocator,
parent_allocator: std.mem.Allocator, parent_allocator: std.mem.Allocator,
const Self = @This(); const Self = @This();
pub fn allocator(self: *Self) std.mem.Allocator {
return std.mem.Allocator.init(self, allocFn, resizeFn);
}
pub fn init(allocator: std.mem.Allocator) Self { pub fn init(allocator: std.mem.Allocator) Self {
return .{ return .{
.parent_allocator = allocator, .parent_allocator = allocator,
@ -124,8 +127,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
}; };
} }
fn allocFn(allocator: std.mem.Allocator, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 { fn allocFn(self: *Self, len: usize, ptr_align: u29, len_align: u29, ret_addr: usize) std.mem.Allocator.Error![]u8 {
const self = @fieldParentPtr(Self, "allocator", allocator);
const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr); const result = self.parent_allocator.allocFn(self.parent_allocator, len, ptr_align, len_align, ret_addr);
if (result) |data| { if (result) |data| {
if (data.len != 0) { if (data.len != 0) {
@ -141,9 +143,7 @@ pub fn TracyAllocator(comptime name: ?[:0]const u8) type {
return result; return result;
} }
fn resizeFn(allocator: std.mem.Allocator, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize { fn resizeFn(self: *Self, buf: []u8, buf_align: u29, new_len: usize, len_align: u29, ret_addr: usize) std.mem.Allocator.Error!usize {
const self = @fieldParentPtr(Self, "allocator", allocator);
if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| { if (self.parent_allocator.resizeFn(self.parent_allocator, buf, buf_align, new_len, len_align, ret_addr)) |resized_len| {
// this condition is to handle free being called on an empty slice that was never even allocated // this condition is to handle free being called on an empty slice that was never even allocated
// example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}` // example case: `std.process.getSelfExeSharedLibPaths` can return `&[_][:0]u8{}`