Skip to content

Commit

Permalink
Add reserved_bytes method for Allocator (#266)
Browse files Browse the repository at this point in the history
* Change total_reserved_bytes to total_capacity_bytes in AllocatorReport

The use of "reserved" may be confused with the concept of reserved
memory in Dx12. Capacity is in line with the similar concept from (e.g.)
std::vec::Vec.

* Add capacity method for Allocator

This fills the use case where one needs the total capacity given by the
sum of sizes of all device-allocated memory blocks, but compiling a
complete report (in particular due to the calls to report_allocations
from the SubAllocator) would be too costly.
  • Loading branch information
ftilde authored Feb 27, 2025
1 parent 955b13a commit 642dc91
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@ pub struct AllocatorReport {
pub blocks: Vec<MemoryBlockReport>,
/// Sum of the memory used by all allocations, in bytes.
pub total_allocated_bytes: u64,
/// Sum of the memory reserved by all memory blocks including unallocated regions, in bytes.
pub total_reserved_bytes: u64,
/// Sum of the memory capacity of all memory blocks including unallocated regions, in bytes.
pub total_capacity_bytes: u64,
}

impl fmt::Debug for AllocationReport {
Expand Down Expand Up @@ -90,7 +90,7 @@ impl fmt::Debug for AllocatorReport {
&std::format_args!(
"{} / {}",
fmt_bytes(self.total_allocated_bytes),
fmt_bytes(self.total_reserved_bytes)
fmt_bytes(self.total_capacity_bytes)
),
)
.field("blocks", &self.blocks.len())
Expand Down
19 changes: 16 additions & 3 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1126,11 +1126,11 @@ impl Allocator {
pub fn generate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
total_capacity_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
Expand All @@ -1146,9 +1146,22 @@ impl Allocator {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
total_capacity_bytes,
}
}

/// Current total capacity of memory blocks allocated on the device, in bytes
pub fn capacity(&self) -> u64 {
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_capacity_bytes += block.size;
}
}

total_capacity_bytes
}
}

impl fmt::Debug for Allocator {
Expand Down
19 changes: 16 additions & 3 deletions src/metal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,11 @@ impl Allocator {
pub fn generate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
total_capacity_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
Expand All @@ -538,7 +538,20 @@ impl Allocator {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
total_capacity_bytes,
}
}

/// Current total capacity of memory blocks allocated on the device, in bytes
pub fn capacity(&self) -> u64 {
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_capacity_bytes += block.size;
}
}

total_capacity_bytes
}
}
19 changes: 16 additions & 3 deletions src/vulkan/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -934,11 +934,11 @@ impl Allocator {
pub fn generate_report(&self) -> AllocatorReport {
let mut allocations = vec![];
let mut blocks = vec![];
let mut total_reserved_bytes = 0;
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_reserved_bytes += block.size;
total_capacity_bytes += block.size;
let first_allocation = allocations.len();
allocations.extend(block.sub_allocator.report_allocations());
blocks.push(MemoryBlockReport {
Expand All @@ -954,9 +954,22 @@ impl Allocator {
allocations,
blocks,
total_allocated_bytes,
total_reserved_bytes,
total_capacity_bytes,
}
}

/// Current total capacity of memory blocks allocated on the device, in bytes
pub fn capacity(&self) -> u64 {
let mut total_capacity_bytes = 0;

for memory_type in &self.memory_types {
for block in memory_type.memory_blocks.iter().flatten() {
total_capacity_bytes += block.size;
}
}

total_capacity_bytes
}
}

impl Drop for Allocator {
Expand Down

0 comments on commit 642dc91

Please sign in to comment.