@@ -5,7 +5,7 @@ use crate::{
5
5
allocator:: { self , AllocatorReport , MemoryBlockReport } ,
6
6
AllocationError , AllocationSizes , AllocatorDebugSettings , MemoryLocation , Result ,
7
7
} ;
8
- use log:: { debug, Level } ;
8
+ use log:: debug;
9
9
10
10
fn memory_location_to_metal ( location : MemoryLocation ) -> metal:: MTLResourceOptions {
11
11
match location {
@@ -16,6 +16,7 @@ fn memory_location_to_metal(location: MemoryLocation) -> metal::MTLResourceOptio
16
16
}
17
17
}
18
18
19
+ #[ derive( Debug ) ]
19
20
pub struct Allocation {
20
21
chunk_id : Option < std:: num:: NonZeroU64 > ,
21
22
offset : u64 ,
@@ -70,6 +71,7 @@ impl Allocation {
70
71
}
71
72
}
72
73
74
+ #[ derive( Clone , Debug ) ]
73
75
pub struct AllocationCreateDesc < ' a > {
74
76
/// Name of the allocation, for tracking and debugging purposes
75
77
pub name : & ' a str ,
@@ -130,21 +132,28 @@ impl<'a> AllocationCreateDesc<'a> {
130
132
}
131
133
}
132
134
}
135
+
133
136
pub struct Allocator {
134
137
device : Arc < metal:: Device > ,
135
138
debug_settings : AllocatorDebugSettings ,
136
139
memory_types : Vec < MemoryType > ,
137
140
allocation_sizes : AllocationSizes ,
138
141
}
142
+
143
+ #[ derive( Debug ) ]
139
144
pub struct AllocatorCreateDesc {
140
145
pub device : Arc < metal:: Device > ,
141
146
pub debug_settings : AllocatorDebugSettings ,
142
147
pub allocation_sizes : AllocationSizes ,
143
148
}
149
+
150
+ #[ derive( Debug ) ]
144
151
pub struct CommittedAllocationStatistics {
145
152
pub num_allocations : usize ,
146
153
pub total_size : u64 ,
147
154
}
155
+
156
+ #[ derive( Debug ) ]
148
157
struct MemoryBlock {
149
158
heap : Arc < metal:: Heap > ,
150
159
size : u64 ,
@@ -157,10 +166,12 @@ impl MemoryBlock {
157
166
size : u64 ,
158
167
heap_descriptor : & metal:: HeapDescriptor ,
159
168
dedicated : bool ,
169
+ memory_location : MemoryLocation ,
160
170
) -> Result < Self > {
161
171
heap_descriptor. set_size ( size) ;
162
172
163
173
let heap = Arc :: new ( device. new_heap ( heap_descriptor) ) ;
174
+ heap. set_label ( & format ! ( "MemoryBlock {memory_location:?}" ) ) ;
164
175
165
176
let sub_allocator: Box < dyn allocator:: SubAllocator > = if dedicated {
166
177
Box :: new ( allocator:: DedicatedBlockAllocator :: new ( size) )
@@ -176,6 +187,7 @@ impl MemoryBlock {
176
187
}
177
188
}
178
189
190
+ #[ derive( Debug ) ]
179
191
struct MemoryType {
180
192
memory_blocks : Vec < Option < MemoryBlock > > ,
181
193
_committed_allocations : CommittedAllocationStatistics ,
@@ -207,7 +219,13 @@ impl MemoryType {
207
219
208
220
// Create a dedicated block for large memory allocations
209
221
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
+ ) ?;
211
229
212
230
let block_index = self . memory_blocks . iter ( ) . position ( |block| block. is_none ( ) ) ;
213
231
let block_index = match block_index {
@@ -277,8 +295,13 @@ impl MemoryType {
277
295
}
278
296
}
279
297
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
+ ) ?;
282
305
283
306
let new_block_index = if let Some ( block_index) = empty_block_index {
284
307
self . memory_blocks [ block_index] = Some ( new_memory_block) ;
@@ -356,14 +379,7 @@ impl MemoryType {
356
379
}
357
380
}
358
381
359
- pub struct ResourceCreateDesc { }
360
- pub struct Resource { }
361
-
362
382
impl Allocator {
363
- pub fn device ( & self ) -> & metal:: Device {
364
- todo ! ( )
365
- }
366
-
367
383
pub fn new ( desc : & AllocatorCreateDesc ) -> Result < Self > {
368
384
let heap_types = [
369
385
( MemoryLocation :: GpuOnly , {
@@ -390,16 +406,16 @@ impl Allocator {
390
406
] ;
391
407
392
408
let memory_types = heap_types
393
- . iter ( )
409
+ . into_iter ( )
394
410
. enumerate ( )
395
411
. map ( |( i, ( memory_location, heap_descriptor) ) | MemoryType {
396
412
memory_blocks : vec ! [ ] ,
397
413
_committed_allocations : CommittedAllocationStatistics {
398
414
num_allocations : 0 ,
399
415
total_size : 0 ,
400
416
} ,
401
- memory_location : * memory_location ,
402
- heap_properties : heap_descriptor. clone ( ) ,
417
+ memory_location,
418
+ heap_properties : heap_descriptor,
403
419
memory_type_index : i,
404
420
active_general_blocks : 0 ,
405
421
} )
@@ -480,13 +496,6 @@ impl Allocator {
480
496
heaps
481
497
}
482
498
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
-
490
499
pub fn generate_report ( & self ) -> AllocatorReport {
491
500
let mut allocations = vec ! [ ] ;
492
501
let mut blocks = vec ! [ ] ;
0 commit comments