你好,我想创建一个测试,让函数在 FixedBufferAllocator 上运行,以检查它是否符合我的内存约束,同时我还想像 DebugAllocator 那样跟踪分配,所以我认为也许我可以通过自己构造分配器来限制可用的地址空间,或者添加后备 fixedBufferAllocator,但似乎情况并非如此(至少在 0.16 中)
那么运行内存受限测试的最佳方法是什么?
使用 Debug 和 FixedBuffer 分配器各运行一次测试?使用 FixedBuffer,但由 Debug 提供后备,编写 Debug 的包装器来跟踪已用地址空间?
你可以将 Config 结构体传入 DebugAllocator:
pub fn DebugAllocator(comptime config: Config) type {
其中一个选项听起来正是你想要的:
/// 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,
综合起来:
var debug_allocator: std.heap.DebugAllocator(.{.enable_memory_limit = true}) = .init;
debug_allocator.requested_memory_limit = 1234;
回答原问题:你也可以配置自定义后备分配器(但请注意,当不以后备页分配器为后备时,调试分配器可能无法在所有情况下都能发挥最佳效果):
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
debug_allocator.backing_allocater = fixed_buffer_allocator;
下次遇到类似问题时,我也建议查看源代码(它出奇地易于阅读,你可以从中学习到很多东西),或者查看 生成的 stdlib 文档。
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.