Fix Miri warnings when running the suballocator tests (#2330)

This commit is contained in:
marc0246 2023-09-13 09:30:12 +02:00 committed by GitHub
parent 7d63f8e697
commit 771aa30bbe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 4 deletions

View File

@ -243,6 +243,7 @@ use parking_lot::Mutex;
use std::{ use std::{
error::Error, error::Error,
fmt::{Debug, Display, Error as FmtError, Formatter}, fmt::{Debug, Display, Error as FmtError, Formatter},
mem,
ops::BitOr, ops::BitOr,
ptr, ptr,
sync::Arc, sync::Arc,
@ -728,6 +729,24 @@ pub struct AllocationHandle(pub *mut ());
unsafe impl Send for AllocationHandle {} unsafe impl Send for AllocationHandle {}
unsafe impl Sync 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]. /// Error that can be returned when creating an [allocation] using a [memory allocator].
/// ///
/// [allocation]: MemoryAlloc /// [allocation]: MemoryAlloc

View File

@ -409,7 +409,7 @@ unsafe impl Suballocator for FreeListAllocator {
offset, offset,
size, size,
allocation_type, 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) { unsafe fn deallocate(&self, suballocation: Suballocation) {
// SAFETY: The caller must guarantee that `suballocation` refers to a currently allocated // SAFETY: The caller must guarantee that `suballocation` refers to a currently allocated
// allocation of `self`. // 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 state = unsafe { &mut *self.state.get() };
let node = state.nodes.get_mut(node_id); let node = state.nodes.get_mut(node_id);
@ -891,7 +891,7 @@ unsafe impl Suballocator for BuddyAllocator {
offset, offset,
size: layout.size(), size: layout.size(),
allocation_type, allocation_type,
handle: AllocationHandle(min_order as _), handle: AllocationHandle::from_index(min_order),
}); });
} }
} }
@ -908,7 +908,7 @@ unsafe impl Suballocator for BuddyAllocator {
#[inline] #[inline]
unsafe fn deallocate(&self, suballocation: Suballocation) { unsafe fn deallocate(&self, suballocation: Suballocation) {
let mut offset = suballocation.offset; let mut offset = suballocation.offset;
let order = suballocation.handle.0 as usize; let order = suballocation.handle.into_index();
let min_order = order; let min_order = order;
let state = unsafe { &mut *self.state.get() }; let state = unsafe { &mut *self.state.get() };