Skip to content

Commit

Permalink
Make free and destroy functions take mutable reference to Allocation (
Browse files Browse the repository at this point in the history
#2)

* made `destroy_image` take a reference to `Allocation` because `Allocation` doesnt impliment copy leading to problems where you cant move out a struct member in `drop`

* made free/destroy functions take mut reference to allocation
  • Loading branch information
lilly-lizard authored May 27, 2023
1 parent 999637d commit 7ade79e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Allocator {

/// Frees memory previously allocated using `Allocator::allocate_memory`,
/// `Allocator::allocate_memory_for_buffer`, or `Allocator::allocate_memory_for_image`.
pub unsafe fn free_memory(&self, allocation: Allocation) {
pub unsafe fn free_memory(&self, allocation: &mut Allocation) {
ffi::vmaFreeMemory(self.internal, allocation.0);
}

Expand All @@ -202,7 +202,7 @@ impl Allocator {
/// It may be internally optimized to be more efficient than calling 'Allocator::free_memory` `allocations.len()` times.
///
/// Allocations in 'allocations' slice can come from any memory pools and types.
pub unsafe fn free_memory_pages(&self, allocations: &[Allocation]) {
pub unsafe fn free_memory_pages(&self, allocations: &mut [Allocation]) {
ffi::vmaFreeMemoryPages(
self.internal,
allocations.len(),
Expand Down
16 changes: 8 additions & 8 deletions tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ fn create_gpu_buffer() {
};

unsafe {
let (buffer, allocation) = allocator
let (buffer, mut allocation) = allocator
.create_buffer(
&ash::vk::BufferCreateInfo::builder()
.size(16 * 1024)
Expand All @@ -179,7 +179,7 @@ fn create_gpu_buffer() {
.unwrap();
let allocation_info = allocator.get_allocation_info(&allocation);
assert_eq!(allocation_info.mapped_data, std::ptr::null_mut());
allocator.destroy_buffer(buffer, allocation);
allocator.destroy_buffer(buffer, &mut allocation);
}
}

Expand All @@ -195,7 +195,7 @@ fn create_cpu_buffer_preferred() {
..Default::default()
};
unsafe {
let (buffer, allocation) = allocator
let (buffer, mut allocation) = allocator
.create_buffer(
&ash::vk::BufferCreateInfo::builder()
.size(16 * 1024)
Expand All @@ -209,7 +209,7 @@ fn create_cpu_buffer_preferred() {
.unwrap();
let allocation_info = allocator.get_allocation_info(&allocation);
assert_ne!(allocation_info.mapped_data, std::ptr::null_mut());
allocator.destroy_buffer(buffer, allocation);
allocator.destroy_buffer(buffer, &mut allocation);
}
}

Expand Down Expand Up @@ -245,10 +245,10 @@ fn create_gpu_buffer_pool() {

let pool = allocator.create_pool(&pool_info).unwrap();

let (buffer, allocation) = pool.create_buffer(&buffer_info, &allocation_info).unwrap();
let (buffer, mut allocation) = pool.create_buffer(&buffer_info, &allocation_info).unwrap();
let allocation_info = allocator.get_allocation_info(&allocation);
assert_ne!(allocation_info.mapped_data, std::ptr::null_mut());
allocator.destroy_buffer(buffer, allocation);
allocator.destroy_buffer(buffer, &mut allocation);
}
}

Expand All @@ -267,7 +267,7 @@ fn test_gpu_stats() {
assert_eq!(stats_1.total.statistics.allocationCount, 0);
assert_eq!(stats_1.total.statistics.allocationBytes, 0);

let (buffer, allocation) = allocator
let (buffer, mut allocation) = allocator
.create_buffer(
&ash::vk::BufferCreateInfo::builder()
.size(16 * 1024)
Expand All @@ -285,7 +285,7 @@ fn test_gpu_stats() {
assert_eq!(stats_2.total.statistics.allocationCount, 1);
assert_eq!(stats_2.total.statistics.allocationBytes, 16 * 1024);

allocator.destroy_buffer(buffer, allocation);
allocator.destroy_buffer(buffer, &mut allocation);

let stats_3 = allocator.calculate_statistics().unwrap();
assert_eq!(stats_3.total.statistics.blockCount, 1);
Expand Down

0 comments on commit 7ade79e

Please sign in to comment.