Skip to content

Commit

Permalink
Remove unneeded panic in GenericMemoryAllocator
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 committed Oct 30, 2022
1 parent ffee3d8 commit 73d886b
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions vulkano/src/memory/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ const G: DeviceSize = 1024 * M;

/// General-purpose memory allocators which allocate from any memory type dynamically as needed.
pub unsafe trait MemoryAllocator: DeviceOwned {
/// Finds the most suitable memory type index in `memory_type_bits` using on a filter. Returns
/// Finds the most suitable memory type index in `memory_type_bits` using a filter. Returns
/// [`None`] if the requirements are too strict and no memory type is able to satisfy them.
fn find_memory_type_index(
&self,
Expand Down Expand Up @@ -1005,8 +1005,6 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {
/// and the [`protected_memory`] feature is not enabled on the device.
/// - Panics if `memory_type_index` refers to a memory type which has the [`device_coherent`]
/// flag set and the [`device_coherent_memory`] feature is not enabled on the device.
/// - Panics if `create_info.size` is greater than the block size corresponding to the heap that
/// the memory type corresponding to `memory_type_index` resides in.
/// - Panics if `create_info.size` is zero.
/// - Panics if `create_info.alignment` is zero.
/// - Panics if `create_info.alignment` is not a power of two.
Expand All @@ -1015,6 +1013,9 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {
///
/// - Returns an error if allocating a new block is required and failed. This can be one of the
/// OOM errors or [`TooManyObjects`].
/// - Returns [`BlockSizeExceeded`] if `create_info.size` is greater than the block size
/// corresponding to the heap that the memory type corresponding to `memory_type_index`
/// resides in.
/// - Returns [`SuballocatorBlockSizeExceeded`] if `S` is `PoolAllocator<BLOCK_SIZE>` and
/// `create_info.size` is greater than `BLOCK_SIZE`.
///
Expand All @@ -1023,6 +1024,7 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {
/// [`device_coherent`]: MemoryPropertyFlags::device_coherent
/// [`device_coherent_memory`]: crate::device::Features::device_coherent_memory
/// [`TooManyObjects`]: VulkanError::TooManyObjects
/// [`BlockSizeExceeded`]: AllocationCreationError::BlockSizeExceeded
/// [`SuballocatorBlockSizeExceeded`]: AllocationCreationError::SuballocatorBlockSizeExceeded
fn allocate_from_type(
&self,
Expand Down Expand Up @@ -1067,6 +1069,11 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {
} = create_info;

let pool = &self.pools[memory_type_index as usize];
let block_size = self.block_sizes[pool.memory_type.heap_index as usize];

if size > block_size {
return Err(AllocationCreationError::BlockSizeExceeded);
}

let mut blocks = if S::IS_BLOCKING {
// If the allocation algorithm needs to block, then there's no point in trying to avoid
Expand Down Expand Up @@ -1152,7 +1159,6 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {

// The pool doesn't have enough real estate, so we need a new block.
let block = {
let block_size = self.block_sizes[pool.memory_type.heap_index as usize];
let export_handle_types = if !self.export_handle_types.is_empty() {
self.export_handle_types[memory_type_index as usize]
} else {
Expand Down Expand Up @@ -1357,15 +1363,8 @@ unsafe impl<S: Suballocator> MemoryAllocator for GenericMemoryAllocator<S> {
if requires_dedicated_allocation {
return Err(AllocationCreationError::DedicatedAllocationRequired);
}
if size <= block_size {
self.allocate_from_type_unchecked(
memory_type_index,
create_info.clone(),
true,
)
} else {
Err(AllocationCreationError::BlockSizeExceeded)
}

self.allocate_from_type_unchecked(memory_type_index, create_info.clone(), true)
}
MemoryAllocatePreference::AlwaysAllocate => self.allocate_dedicated_unchecked(
memory_type_index,
Expand Down

0 comments on commit 73d886b

Please # to comment.