From e9790c1fc3735303d1bff7db86098666e565152c Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Sat, 16 Sep 2023 10:10:32 +0200 Subject: [PATCH] Make the insides of `AllocationHandle` private (#2332) * Make the insides of `AllocationHandle` private * Fix naming --- vulkano/src/memory/allocator/mod.rs | 48 +++++++++++++++++--- vulkano/src/memory/allocator/suballocator.rs | 7 ++- vulkano/src/memory/mod.rs | 4 +- 3 files changed, 46 insertions(+), 13 deletions(-) diff --git a/vulkano/src/memory/allocator/mod.rs b/vulkano/src/memory/allocator/mod.rs index 80f1d5c2b6..812d7c35cd 100644 --- a/vulkano/src/memory/allocator/mod.rs +++ b/vulkano/src/memory/allocator/mod.rs @@ -717,15 +717,33 @@ pub struct MemoryAlloc { } /// An opaque handle identifying an allocation inside an allocator. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] -#[repr(transparent)] -pub struct AllocationHandle(pub *mut ()); +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +#[cfg_attr(not(doc), repr(transparent))] +pub struct AllocationHandle(*mut ()); unsafe impl Send for AllocationHandle {} unsafe impl Sync for AllocationHandle {} impl AllocationHandle { - /// Stores a index inside an `AllocationHandle`. + /// Creates a null `AllocationHandle`. + /// + /// Use this if you don't have anything that you need to associate with the allocation. + #[inline] + pub const fn null() -> Self { + AllocationHandle(ptr::null_mut()) + } + + /// Stores a pointer in an `AllocationHandle`. + /// + /// Use this if you want to associate an allocation with some (host) heap allocation. + #[inline] + pub const fn from_ptr(ptr: *mut ()) -> Self { + AllocationHandle(ptr) + } + + /// Stores an index inside an `AllocationHandle`. + /// + /// Use this if you want to associate an allocation with some index. #[allow(clippy::useless_transmute)] #[inline] pub const fn from_index(index: usize) -> Self { @@ -733,10 +751,26 @@ impl AllocationHandle { AllocationHandle(unsafe { mem::transmute::(index) }) } + /// Retrieves a previously-stored pointer from the `AllocationHandle`. + /// + /// If this handle hasn't been created using [`from_ptr`] then this will return an invalid + /// pointer, dereferencing which is undefined behavior. + /// + /// [`from_ptr`]: Self::from_ptr + #[inline] + pub const fn as_ptr(self) -> *mut () { + self.0 + } + /// Retrieves a previously-stored index from the `AllocationHandle`. + /// + /// If this handle hasn't been created using [`from_index`] then this will return a bogus + /// result. + /// + /// [`from_index`]: Self::from_index #[allow(clippy::transmutes_expressible_as_ptr_casts)] #[inline] - pub const fn into_index(self) -> usize { + pub const fn as_index(self) -> usize { // SAFETY: `usize` and `*mut ()` have the same layout. unsafe { mem::transmute::<*mut (), usize>(self.0) } } @@ -1414,7 +1448,7 @@ unsafe impl MemoryAllocator for GenericMemoryA Ok(MemoryAlloc { device_memory, suballocation: None, - allocation_handle: AllocationHandle(ptr::null_mut()), + allocation_handle: AllocationHandle::null(), }) } @@ -1544,7 +1578,7 @@ impl Block { Ok(MemoryAlloc { device_memory: self.device_memory.clone(), suballocation: Some(suballocation), - allocation_handle: AllocationHandle(self as *mut Block as _), + allocation_handle: AllocationHandle::from_ptr(self as *mut Block as _), }) } diff --git a/vulkano/src/memory/allocator/suballocator.rs b/vulkano/src/memory/allocator/suballocator.rs index 35a02cb0d7..bc13405781 100644 --- a/vulkano/src/memory/allocator/suballocator.rs +++ b/vulkano/src/memory/allocator/suballocator.rs @@ -24,7 +24,6 @@ use std::{ cmp, error::Error, fmt::{self, Debug, Display}, - ptr, }; /// Suballocators are used to divide a *region* into smaller *suballocations*. @@ -499,7 +498,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.into_index()); + let node_id = SlotId::new(suballocation.handle.as_index()); let state = unsafe { &mut *self.state.get() }; let node = state.nodes.get_mut(node_id); @@ -974,7 +973,7 @@ unsafe impl Suballocator for BuddyAllocator { #[inline] unsafe fn deallocate(&self, suballocation: Suballocation) { let mut offset = suballocation.offset; - let order = suballocation.handle.into_index(); + let order = suballocation.handle.as_index(); let min_order = order; let state = unsafe { &mut *self.state.get() }; @@ -1159,7 +1158,7 @@ unsafe impl Suballocator for BumpAllocator { offset, size, allocation_type, - handle: AllocationHandle(ptr::null_mut()), + handle: AllocationHandle::null(), }) } diff --git a/vulkano/src/memory/mod.rs b/vulkano/src/memory/mod.rs index 5ddccf8051..4fd9c872bd 100644 --- a/vulkano/src/memory/mod.rs +++ b/vulkano/src/memory/mod.rs @@ -109,7 +109,7 @@ use std::{ mem::ManuallyDrop, num::NonZeroU64, ops::{Bound, Range, RangeBounds, RangeTo}, - ptr::{self, NonNull}, + ptr::NonNull, sync::Arc, }; @@ -155,7 +155,7 @@ impl ResourceMemory { offset: 0, size: device_memory.allocation_size(), allocation_type: AllocationType::Unknown, - allocation_handle: AllocationHandle(ptr::null_mut()), + allocation_handle: AllocationHandle::null(), suballocation_handle: None, allocator: None, device_memory: ManuallyDrop::new(DeviceOwnedDebugWrapper(device_memory)),