Hello, I wanted to create test where function would run against FixedBufferAllocator to check if it conforms to my memory constrainsts, but at the same time I wanted to track allocations like with DebugAllocator, so I thought that maybe I can limit usable adress space by allocator by constructing it myself, or add backing fixedBufferAllocator, but that seemed to not be the case (at least in 0.16)
So what would be best way to approach running a test with limited memory?
Running test twice, once with Debug and once with FixedBuffer allocators? Using FixedBuffer, but backed by Debug one, writing wrapper around Debug, that would track used address space?
You can pass a Config struct into the DebugAllocator:
pub fn DebugAllocator(comptime config: Config) type {
One of the options sounds exactly like what you are looking for:
/// If true, the allocator will have two fields:
/// * `total_requested_bytes` which tracks the total allocated bytes of memory requested.
/// * `requested_memory_limit` which causes allocations to return `error.OutOfMemory`
/// when the `total_requested_bytes` exceeds this limit.
/// If false, these fields will be `void`.
enable_memory_limit: bool = false,
Putting it all together:
var debug_allocator: std.heap.DebugAllocator(.{.enable_memory_limit = true}) = .init;
debug_allocator.requested_memory_limit = 1234;
To answer the original question: You can also configure a custom backing allocator (though note that the debug allocator may not work optimally under all circumstances when not backed by the page allocator):
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
debug_allocator.backing_allocater = fixed_buffer_allocator;
For the next time you have a question like this, I’d also suggest looking at the source code (it’s surprisingly easy to read and you can learn a lot from it) or checking out the generated stdlib documentation.
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.