diff --git a/src/metal/mod.rs b/src/metal/mod.rs index 89cf1c5..5ccc5b2 100644 --- a/src/metal/mod.rs +++ b/src/metal/mod.rs @@ -5,7 +5,7 @@ use crate::{ allocator::{self, AllocatorReport, MemoryBlockReport}, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result, }; -use log::{debug, Level}; +use log::debug; fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions { match location { @@ -16,6 +16,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptio } } +#[derive(Debug)] pub struct Allocation { chunk_id: Option, offset: u64, @@ -70,6 +71,7 @@ impl Allocation { } } +#[derive(Clone, Debug)] pub struct AllocationCreateDesc<'a> { /// Name of the allocation, for tracking and debugging purposes pub name: &'a str, @@ -130,21 +132,28 @@ impl<'a> AllocationCreateDesc<'a> { } } } + pub struct Allocator { device: Arc, debug_settings: AllocatorDebugSettings, memory_types: Vec, allocation_sizes: AllocationSizes, } + +#[derive(Debug)] pub struct AllocatorCreateDesc { pub device: Arc, pub debug_settings: AllocatorDebugSettings, pub allocation_sizes: AllocationSizes, } + +#[derive(Debug)] pub struct CommittedAllocationStatistics { pub num_allocations: usize, pub total_size: u64, } + +#[derive(Debug)] struct MemoryBlock { heap: Arc, size: u64, @@ -157,10 +166,12 @@ impl MemoryBlock { size: u64, heap_descriptor: &metal::HeapDescriptor, dedicated: bool, + memory_location: MemoryLocation, ) -> Result { heap_descriptor.set_size(size); let heap = Arc::new(device.new_heap(heap_descriptor)); + heap.set_label(&format!("MemoryBlock {memory_location:?}")); let sub_allocator: Box = if dedicated { Box::new(allocator::DedicatedBlockAllocator::new(size)) @@ -176,6 +187,7 @@ impl MemoryBlock { } } +#[derive(Debug)] struct MemoryType { memory_blocks: Vec>, _committed_allocations: CommittedAllocationStatistics, @@ -207,7 +219,13 @@ impl MemoryType { // Create a dedicated block for large memory allocations if size > memblock_size { - let mem_block = MemoryBlock::new(device, size, &self.heap_properties, true)?; + let mem_block = MemoryBlock::new( + device, + size, + &self.heap_properties, + true, + self.memory_location, + )?; let block_index = self.memory_blocks.iter().position(|block| block.is_none()); let block_index = match block_index { @@ -277,8 +295,13 @@ impl MemoryType { } } - let new_memory_block = - MemoryBlock::new(device, memblock_size, &self.heap_properties, false)?; + let new_memory_block = MemoryBlock::new( + device, + memblock_size, + &self.heap_properties, + false, + self.memory_location, + )?; let new_block_index = if let Some(block_index) = empty_block_index { self.memory_blocks[block_index] = Some(new_memory_block); @@ -356,14 +379,7 @@ impl MemoryType { } } -pub struct ResourceCreateDesc {} -pub struct Resource {} - impl Allocator { - pub fn device(&self) -> &metal::Device { - todo!() - } - pub fn new(desc: &AllocatorCreateDesc) -> Result { let heap_types = [ (MemoryLocation::GpuOnly, { @@ -390,7 +406,7 @@ impl Allocator { ]; let memory_types = heap_types - .iter() + .into_iter() .enumerate() .map(|(i, (memory_location, heap_descriptor))| MemoryType { memory_blocks: vec![], @@ -398,8 +414,8 @@ impl Allocator { num_allocations: 0, total_size: 0, }, - memory_location: *memory_location, - heap_properties: heap_descriptor.clone(), + memory_location, + heap_properties: heap_descriptor, memory_type_index: i, active_general_blocks: 0, }) @@ -480,13 +496,6 @@ impl Allocator { heaps } - pub fn rename_allocation(&mut self, _allocation: &mut Allocation, _name: &str) -> Result<()> { - todo!() - } - pub fn report_memory_leaks(&self, _log_level: Level) { - todo!() - } - pub fn generate_report(&self) -> AllocatorReport { let mut allocations = vec![]; let mut blocks = vec![];