Skip to content

Commit

Permalink
Fix Miri warnings when running the suballocator tests (vulkano-rs#2330)
Browse files Browse the repository at this point in the history
  • Loading branch information
marc0246 authored and hakolao committed Feb 20, 2024
1 parent 8561529 commit c1b8848
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
19 changes: 19 additions & 0 deletions vulkano/src/memory/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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::<usize, *mut ()>(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
Expand Down
8 changes: 4 additions & 4 deletions vulkano/src/memory/allocator/suballocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ unsafe impl Suballocator for FreeListAllocator {
offset,
size,
allocation_type,
handle: AllocationHandle(id.get() as _),
handle: AllocationHandle::from_index(id.get()),
});
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -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),
});
}
}
Expand All @@ -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() };
Expand Down

0 comments on commit c1b8848

Please # to comment.