Skip to content

Commit

Permalink
Add metal
Browse files Browse the repository at this point in the history
  • Loading branch information
nical committed May 22, 2024
1 parent 4f7317e commit b3ea0c3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ pub struct MemoryBlockReport {
pub allocations: Range<usize>,
}

/// A report that can be generated for debugging purposes using `Allocator::generate_report()`.
/// A report that can be generated for informational purposes using `Allocator::generate_report()`.
#[derive(Clone)]
pub struct AllocatorReport {
/// All live allocations.
/// All live allocations, sub-allocated from memory blocks.
pub allocations: Vec<AllocationReport>,
/// All memory blocks.
pub blocks: Vec<MemoryBlockReport>,
Expand Down
34 changes: 31 additions & 3 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
use std::{backtrace::Backtrace, sync::Arc};

use crate::{
allocator, AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
allocator::{self, AllocatorReport, MemoryBlockReport},
AllocationError, AllocationSizes, AllocatorDebugSettings, MemoryLocation, Result,
};
use log::{debug, Level};

Expand Down Expand Up @@ -146,7 +147,7 @@ pub struct CommittedAllocationStatistics {
}
struct MemoryBlock {
heap: Arc<metal::Heap>,
_size: u64,
size: u64,
sub_allocator: Box<dyn allocator::SubAllocator>,
}

Expand All @@ -169,7 +170,7 @@ impl MemoryBlock {

Ok(Self {
heap,
_size: size,
size,
sub_allocator,
})
}
Expand Down Expand Up @@ -485,4 +486,31 @@ impl Allocator {
pub fn report_memory_leaks(&self, _log_level: Level) {
todo!()
}

pub fn generate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
size: block.size,
allocations: first_allocation..allocations.len(),
});
}
}

let total_allocated_bytes = allocations.iter().map(|report| report.size).sum();

AllocatorReport {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
}
}
}

0 comments on commit b3ea0c3

Please sign in to comment.