Skip to content

Commit 49d1ace

Browse files
FlannyHMarijnS95
andauthored
🧼 Clean up Metal implementation (#227)
Co-authored-by: Marijn Suijten <[email protected]>
1 parent 4b5c0d4 commit 49d1ace

File tree

1 file changed

+30
-21
lines changed

1 file changed

+30
-21
lines changed

src/metal/mod.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::{
55
allocator::{self, AllocatorReport, MemoryBlockReport},
66
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
77
};
8-
use log::{debug, Level};
8+
use log::debug;
99

1010
fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptions {
1111
match location {
@@ -16,6 +16,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptio
1616
}
1717
}
1818

19+
#[derive(Debug)]
1920
pub struct Allocation {
2021
chunk_id: Option<std::num::NonZeroU64>,
2122
offset: u64,
@@ -70,6 +71,7 @@ impl Allocation {
7071
}
7172
}
7273

74+
#[derive(Clone, Debug)]
7375
pub struct AllocationCreateDesc<'a> {
7476
/// Name of the allocation, for tracking and debugging purposes
7577
pub name: &'a str,
@@ -130,21 +132,28 @@ impl<'a> AllocationCreateDesc<'a> {
130132
}
131133
}
132134
}
135+
133136
pub struct Allocator {
134137
device: Arc<metal::Device>,
135138
debug_settings: AllocatorDebugSettings,
136139
memory_types: Vec<MemoryType>,
137140
allocation_sizes: AllocationSizes,
138141
}
142+
143+
#[derive(Debug)]
139144
pub struct AllocatorCreateDesc {
140145
pub device: Arc<metal::Device>,
141146
pub debug_settings: AllocatorDebugSettings,
142147
pub allocation_sizes: AllocationSizes,
143148
}
149+
150+
#[derive(Debug)]
144151
pub struct CommittedAllocationStatistics {
145152
pub num_allocations: usize,
146153
pub total_size: u64,
147154
}
155+
156+
#[derive(Debug)]
148157
struct MemoryBlock {
149158
heap: Arc<metal::Heap>,
150159
size: u64,
@@ -157,10 +166,12 @@ impl MemoryBlock {
157166
size: u64,
158167
heap_descriptor: &metal::HeapDescriptor,
159168
dedicated: bool,
169+
memory_location: MemoryLocation,
160170
) -> Result<Self> {
161171
heap_descriptor.set_size(size);
162172

163173
let heap = Arc::new(device.new_heap(heap_descriptor));
174+
heap.set_label(&format!("MemoryBlock {memory_location:?}"));
164175

165176
let sub_allocator: Box<dyn allocator::SubAllocator> = if dedicated {
166177
Box::new(allocator::DedicatedBlockAllocator::new(size))
@@ -176,6 +187,7 @@ impl MemoryBlock {
176187
}
177188
}
178189

190+
#[derive(Debug)]
179191
struct MemoryType {
180192
memory_blocks: Vec<Option<MemoryBlock>>,
181193
_committed_allocations: CommittedAllocationStatistics,
@@ -207,7 +219,13 @@ impl MemoryType {
207219

208220
// Create a dedicated block for large memory allocations
209221
if size > memblock_size {
210-
let mem_block = MemoryBlock::new(device, size, &self.heap_properties, true)?;
222+
let mem_block = MemoryBlock::new(
223+
device,
224+
size,
225+
&self.heap_properties,
226+
true,
227+
self.memory_location,
228+
)?;
211229

212230
let block_index = self.memory_blocks.iter().position(|block| block.is_none());
213231
let block_index = match block_index {
@@ -277,8 +295,13 @@ impl MemoryType {
277295
}
278296
}
279297

280-
let new_memory_block =
281-
MemoryBlock::new(device, memblock_size, &self.heap_properties, false)?;
298+
let new_memory_block = MemoryBlock::new(
299+
device,
300+
memblock_size,
301+
&self.heap_properties,
302+
false,
303+
self.memory_location,
304+
)?;
282305

283306
let new_block_index = if let Some(block_index) = empty_block_index {
284307
self.memory_blocks[block_index] = Some(new_memory_block);
@@ -356,14 +379,7 @@ impl MemoryType {
356379
}
357380
}
358381

359-
pub struct ResourceCreateDesc {}
360-
pub struct Resource {}
361-
362382
impl Allocator {
363-
pub fn device(&self) -> &metal::Device {
364-
todo!()
365-
}
366-
367383
pub fn new(desc: &AllocatorCreateDesc) -> Result<Self> {
368384
let heap_types = [
369385
(MemoryLocation::GpuOnly, {
@@ -390,16 +406,16 @@ impl Allocator {
390406
];
391407

392408
let memory_types = heap_types
393-
.iter()
409+
.into_iter()
394410
.enumerate()
395411
.map(|(i, (memory_location, heap_descriptor))| MemoryType {
396412
memory_blocks: vec![],
397413
_committed_allocations: CommittedAllocationStatistics {
398414
num_allocations: 0,
399415
total_size: 0,
400416
},
401-
memory_location: *memory_location,
402-
heap_properties: heap_descriptor.clone(),
417+
memory_location,
418+
heap_properties: heap_descriptor,
403419
memory_type_index: i,
404420
active_general_blocks: 0,
405421
})
@@ -480,13 +496,6 @@ impl Allocator {
480496
heaps
481497
}
482498

483-
pub fn rename_allocation(&mut self, _allocation: &mut Allocation, _name: &str) -> Result<()> {
484-
todo!()
485-
}
486-
pub fn report_memory_leaks(&self, _log_level: Level) {
487-
todo!()
488-
}
489-
490499
pub fn generate_report(&self) -> AllocatorReport {
491500
let mut allocations = vec![];
492501
let mut blocks = vec![];

0 commit comments

Comments
 (0)