-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds a custom Allocator wrapper which keeps track of the allocated memory, then record the maximum allocation readings in the Runner alongside timing readings and carry them into the Result.
- Loading branch information
Showing
6 changed files
with
227 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
const std = @import("std"); | ||
const Allocator = std.mem.Allocator; | ||
|
||
const TrackingAllocator = @This(); | ||
|
||
parent_allocator: Allocator, | ||
current_allocated: usize = 0, | ||
max_allocated: usize = 0, | ||
|
||
pub fn init(parent_allocator: Allocator) TrackingAllocator { | ||
return .{ | ||
.parent_allocator = parent_allocator, | ||
}; | ||
} | ||
|
||
pub fn allocator(self: *TrackingAllocator) Allocator { | ||
return .{ | ||
.ptr = self, | ||
.vtable = &.{ | ||
.alloc = alloc, | ||
.resize = resize, | ||
.free = free, | ||
}, | ||
}; | ||
} | ||
|
||
pub fn maxAllocated(self: TrackingAllocator) usize { | ||
return self.max_allocated; | ||
} | ||
|
||
fn alloc( | ||
ctx: *anyopaque, | ||
len: usize, | ||
log2_ptr_align: u8, | ||
ra: usize, | ||
) ?[*]u8 { | ||
const self: *TrackingAllocator = @ptrCast(@alignCast(ctx)); | ||
const result = self.parent_allocator.rawAlloc(len, log2_ptr_align, ra); | ||
if (result) |_| { | ||
self.current_allocated += len; | ||
if (self.max_allocated < self.current_allocated) | ||
self.max_allocated = self.current_allocated; | ||
} | ||
return result; | ||
} | ||
|
||
fn resize( | ||
ctx: *anyopaque, | ||
buf: []u8, | ||
log2_buf_align: u8, | ||
new_len: usize, | ||
ra: usize, | ||
) bool { | ||
const self: *TrackingAllocator = @ptrCast(@alignCast(ctx)); | ||
const result = self.parent_allocator.rawResize(buf, log2_buf_align, new_len, ra); | ||
if (result) { | ||
if (buf.len < new_len) { | ||
self.current_allocated += new_len - buf.len; | ||
if (self.max_allocated < self.current_allocated) | ||
self.max_allocated = self.current_allocated; | ||
} else self.current_allocated -= buf.len - new_len; | ||
} | ||
return result; | ||
} | ||
|
||
fn free( | ||
ctx: *anyopaque, | ||
buf: []u8, | ||
log2_buf_align: u8, | ||
ra: usize, | ||
) void { | ||
const self: *TrackingAllocator = @ptrCast(@alignCast(ctx)); | ||
self.parent_allocator.rawFree(buf, log2_buf_align, ra); | ||
self.current_allocated -= buf.len; | ||
} | ||
|
||
/// This allocator is used in front of another allocator and tracks the maximum | ||
/// memory usage on every call to the allocator. | ||
pub fn trackingAllocator(parent_allocator: Allocator) TrackingAllocator { | ||
return TrackingAllocator.init(parent_allocator); | ||
} |
Oops, something went wrong.