From 771aa30bbe46e43dbeff855fe4090c1510d48a60 Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Wed, 13 Sep 2023 09:30:12 +0200 Subject: [PATCH] Fix Miri warnings when running the suballocator tests (#2330) --- vulkano/src/memory/allocator/mod.rs | 19 +++++++++++++++++++ vulkano/src/memory/allocator/suballocator.rs | 8 ++++---- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/vulkano/src/memory/allocator/mod.rs b/vulkano/src/memory/allocator/mod.rs index a1bca1e127..24dc4cc7a4 100644 --- a/vulkano/src/memory/allocator/mod.rs +++ b/vulkano/src/memory/allocator/mod.rs @@ -243,6 +243,7 @@ use parking_lot::Mutex; use std::{ error::Error, fmt::{Debug, Display, Error as FmtError, Formatter}, + mem, ops::BitOr, ptr, sync::Arc, @@ -728,6 +729,24 @@ pub struct AllocationHandle(pub *mut ()); unsafe impl Send for AllocationHandle {} unsafe impl Sync for AllocationHandle {} +impl AllocationHandle { + /// Stores a index inside an `AllocationHandle`. + #[allow(clippy::useless_transmute)] + #[inline] + pub const fn from_index(index: usize) -> Self { + // SAFETY: `usize` and `*mut ()` have the same layout. + AllocationHandle(unsafe { mem::transmute::(index) }) + } + + /// Retrieves a previously-stored index from the `AllocationHandle`. + #[allow(clippy::transmutes_expressible_as_ptr_casts)] + #[inline] + pub const fn into_index(self) -> usize { + // SAFETY: `usize` and `*mut ()` have the same layout. + unsafe { mem::transmute::<*mut (), usize>(self.0) } + } +} + /// Error that can be returned when creating an [allocation] using a [memory allocator]. /// /// [allocation]: MemoryAlloc diff --git a/vulkano/src/memory/allocator/suballocator.rs b/vulkano/src/memory/allocator/suballocator.rs index 05aca577c8..2a3122b20f 100644 --- a/vulkano/src/memory/allocator/suballocator.rs +++ b/vulkano/src/memory/allocator/suballocator.rs @@ -409,7 +409,7 @@ unsafe impl Suballocator for FreeListAllocator { offset, size, allocation_type, - handle: AllocationHandle(id.get() as _), + handle: AllocationHandle::from_index(id.get()), }); } } @@ -431,7 +431,7 @@ unsafe impl Suballocator for FreeListAllocator { unsafe fn deallocate(&self, suballocation: Suballocation) { // SAFETY: The caller must guarantee that `suballocation` refers to a currently allocated // allocation of `self`. - let node_id = SlotId::new(suballocation.handle.0 as _); + let node_id = SlotId::new(suballocation.handle.into_index()); let state = unsafe { &mut *self.state.get() }; let node = state.nodes.get_mut(node_id); @@ -891,7 +891,7 @@ unsafe impl Suballocator for BuddyAllocator { offset, size: layout.size(), allocation_type, - handle: AllocationHandle(min_order as _), + handle: AllocationHandle::from_index(min_order), }); } } @@ -908,7 +908,7 @@ unsafe impl Suballocator for BuddyAllocator { #[inline] unsafe fn deallocate(&self, suballocation: Suballocation) { let mut offset = suballocation.offset; - let order = suballocation.handle.0 as usize; + let order = suballocation.handle.into_index(); let min_order = order; let state = unsafe { &mut *self.state.get() };