你好,我想建立一個測試,讓函式在 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;
回答原問題:你也可以配置自訂後備配置器(請注意,當未使用頁面配置器作為後備時,debug 配置器可能無法在所有情況下達到最佳效果):
var debug_allocator: std.heap.DebugAllocator(.{}) = .init;
debug_allocator.backing_allocater = fixed_buffer_allocator;
下次遇到類似的問題,我也建議你查看原始碼(它其實非常容易閱讀,你可以從中學到很多),或者參考 生成的標準函式庫文件。
0 Comments
Log in to join the conversation.No comments yet. Be the first to share your thoughts.