Skip to content

Commit 642dc91

Browse files
authored
Add reserved_bytes method for Allocator (#266)
* 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.
1 parent 955b13a commit 642dc91

File tree

4 files changed

+51
-12
lines changed

4 files changed

+51
-12
lines changed

src/allocator/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ pub struct AllocatorReport {
6161
pub blocks: Vec<MemoryBlockReport>,
6262
/// Sum of the memory used by all allocations, in bytes.
6363
pub total_allocated_bytes: u64,
64-
/// Sum of the memory reserved by all memory blocks including unallocated regions, in bytes.
65-
pub total_reserved_bytes: u64,
64+
/// Sum of the memory capacity of all memory blocks including unallocated regions, in bytes.
65+
pub total_capacity_bytes: u64,
6666
}
6767

6868
impl fmt::Debug for AllocationReport {
@@ -90,7 +90,7 @@ impl fmt::Debug for AllocatorReport {
9090
&std::format_args!(
9191
"{} / {}",
9292
fmt_bytes(self.total_allocated_bytes),
93-
fmt_bytes(self.total_reserved_bytes)
93+
fmt_bytes(self.total_capacity_bytes)
9494
),
9595
)
9696
.field("blocks", &self.blocks.len())

src/d3d12/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -1126,11 +1126,11 @@ impl Allocator {
11261126
pub fn generate_report(&self) -> AllocatorReport {
11271127
let mut allocations = vec![];
11281128
let mut blocks = vec![];
1129-
let mut total_reserved_bytes = 0;
1129+
let mut total_capacity_bytes = 0;
11301130

11311131
for memory_type in &self.memory_types {
11321132
for block in memory_type.memory_blocks.iter().flatten() {
1133-
total_reserved_bytes += block.size;
1133+
total_capacity_bytes += block.size;
11341134
let first_allocation = allocations.len();
11351135
allocations.extend(block.sub_allocator.report_allocations());
11361136
blocks.push(MemoryBlockReport {
@@ -1146,9 +1146,22 @@ impl Allocator {
11461146
allocations,
11471147
blocks,
11481148
total_allocated_bytes,
1149-
total_reserved_bytes,
1149+
total_capacity_bytes,
11501150
}
11511151
}
1152+
1153+
/// Current total capacity of memory blocks allocated on the device, in bytes
1154+
pub fn capacity(&self) -> u64 {
1155+
let mut total_capacity_bytes = 0;
1156+
1157+
for memory_type in &self.memory_types {
1158+
for block in memory_type.memory_blocks.iter().flatten() {
1159+
total_capacity_bytes += block.size;
1160+
}
1161+
}
1162+
1163+
total_capacity_bytes
1164+
}
11521165
}
11531166

11541167
impl fmt::Debug for Allocator {

src/metal/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -518,11 +518,11 @@ impl Allocator {
518518
pub fn generate_report(&self) -> AllocatorReport {
519519
let mut allocations = vec![];
520520
let mut blocks = vec![];
521-
let mut total_reserved_bytes = 0;
521+
let mut total_capacity_bytes = 0;
522522

523523
for memory_type in &self.memory_types {
524524
for block in memory_type.memory_blocks.iter().flatten() {
525-
total_reserved_bytes += block.size;
525+
total_capacity_bytes += block.size;
526526
let first_allocation = allocations.len();
527527
allocations.extend(block.sub_allocator.report_allocations());
528528
blocks.push(MemoryBlockReport {
@@ -538,7 +538,20 @@ impl Allocator {
538538
allocations,
539539
blocks,
540540
total_allocated_bytes,
541-
total_reserved_bytes,
541+
total_capacity_bytes,
542542
}
543543
}
544+
545+
/// Current total capacity of memory blocks allocated on the device, in bytes
546+
pub fn capacity(&self) -> u64 {
547+
let mut total_capacity_bytes = 0;
548+
549+
for memory_type in &self.memory_types {
550+
for block in memory_type.memory_blocks.iter().flatten() {
551+
total_capacity_bytes += block.size;
552+
}
553+
}
554+
555+
total_capacity_bytes
556+
}
544557
}

src/vulkan/mod.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -934,11 +934,11 @@ impl Allocator {
934934
pub fn generate_report(&self) -> AllocatorReport {
935935
let mut allocations = vec![];
936936
let mut blocks = vec![];
937-
let mut total_reserved_bytes = 0;
937+
let mut total_capacity_bytes = 0;
938938

939939
for memory_type in &self.memory_types {
940940
for block in memory_type.memory_blocks.iter().flatten() {
941-
total_reserved_bytes += block.size;
941+
total_capacity_bytes += block.size;
942942
let first_allocation = allocations.len();
943943
allocations.extend(block.sub_allocator.report_allocations());
944944
blocks.push(MemoryBlockReport {
@@ -954,9 +954,22 @@ impl Allocator {
954954
allocations,
955955
blocks,
956956
total_allocated_bytes,
957-
total_reserved_bytes,
957+
total_capacity_bytes,
958958
}
959959
}
960+
961+
/// Current total capacity of memory blocks allocated on the device, in bytes
962+
pub fn capacity(&self) -> u64 {
963+
let mut total_capacity_bytes = 0;
964+
965+
for memory_type in &self.memory_types {
966+
for block in memory_type.memory_blocks.iter().flatten() {
967+
total_capacity_bytes += block.size;
968+
}
969+
}
970+
971+
total_capacity_bytes
972+
}
960973
}
961974

962975
impl Drop for Allocator {

0 commit comments

Comments
 (0)