こんにちは。FixedBufferAllocatorに対して関数を実行し、メモリ制約に適合しているかを確認するテストを作成したいのですが、同時にDebugAllocatorのようにアロケーションを追跡したかったので、自分で構築したり、バックアップのFixedBufferAllocatorを追加したりする方法を考えましたが、少なくとも0.16ではうまくいかないようでした。
そこで、メモリを制限した状態でテストを実行するにはどのようなアプローチが最適でしょうか?
DebugとFixedBufferアロケータでテストを2回実行する?FixedBufferを使用しつつ、Debugをバックアップにする?Debugをラップして使用アドレス空間を追跡する?
DebugAllocatorにはConfig構造体を渡すことができます:
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.