Trait Storage

Source
pub unsafe trait Storage {
    type Handle: StorageHandle;

    // Required methods
    unsafe fn resolve(&self, handle: Self::Handle) -> NonNull<()>;
    fn allocate(
        &self,
        layout: Layout,
    ) -> Result<(Self::Handle, usize), StorageAllocError>;
    unsafe fn deallocate(&self, layout: Layout, handle: Self::Handle);
    unsafe fn grow(
        &self,
        old_layout: Layout,
        new_layout: Layout,
        handle: Self::Handle,
    ) -> Result<(Self::Handle, usize), StorageAllocError>;
    unsafe fn shrink(
        &self,
        old_layout: Layout,
        new_layout: Layout,
        handle: Self::Handle,
    ) -> Result<(Self::Handle, usize), StorageAllocError>;
}
Expand description

The trait for allocating memory in a storage

§Safety

Required Associated Types§

Source

type Handle: StorageHandle

The StorageHandle type that represents an allocation by this Storage

Required Methods§

Source

unsafe fn resolve(&self, handle: Self::Handle) -> NonNull<()>

Returns a pointer to the allocation represented by handle

§Safety

handle must be valid

Source

fn allocate( &self, layout: Layout, ) -> Result<(Self::Handle, usize), StorageAllocError>

Allocates memory with a layout specified by layout

Also returns the total amount of bytes actually allocated, which may be more than requested by layout

Unless Self implements MultipleStorage this will invalidate any previous allocations

Source

unsafe fn deallocate(&self, layout: Layout, handle: Self::Handle)

Deallocates (and invalidates) a StorageHandle that was allocated with this Storage

§Safety
  • layout must be the same layout that was used to allocate it, though the size may by greater as long as its less than the available capacity returned by any of the allocation methods (Storage::allocate/Storage::grow/Storage::shrink)
  • handle must be valid
Source

unsafe fn grow( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Grows (increases the size of) an allocation

Similar to Storage::allocate this method also returns the number of bytes actually allocated, which may be more than requested with new_layout

§Safety
  • new_layout.size() >= old_layout.size()
  • handle must be valid
  • if this method succeeds, handle is now invalid and cannot be used
Source

unsafe fn shrink( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Shrinks (decreases the size of) an allocation

Similar to Storage::allocate this method also returns the number of bytes actually allocated, which may be more than requested with new_layout

§Safety
  • new_layout.size() <= old_layout.size()
  • handle must be valid
  • if this method succeeds, handle is now invalid and cannot be used

Implementations on Foreign Types§

Source§

impl<T: MultipleStorage + ?Sized> Storage for &T

Source§

type Handle = <T as Storage>::Handle

Source§

unsafe fn resolve(&self, handle: Self::Handle) -> NonNull<()>

Source§

fn allocate( &self, layout: Layout, ) -> Result<(Self::Handle, usize), StorageAllocError>

Source§

unsafe fn deallocate(&self, layout: Layout, handle: Self::Handle)

Source§

unsafe fn grow( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Source§

unsafe fn shrink( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Source§

impl<T: Storage + ?Sized> Storage for &mut T

Source§

type Handle = <T as Storage>::Handle

Source§

unsafe fn resolve(&self, handle: Self::Handle) -> NonNull<()>

Source§

fn allocate( &self, layout: Layout, ) -> Result<(Self::Handle, usize), StorageAllocError>

Source§

unsafe fn deallocate(&self, layout: Layout, handle: Self::Handle)

Source§

unsafe fn grow( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Source§

unsafe fn shrink( &self, old_layout: Layout, new_layout: Layout, handle: Self::Handle, ) -> Result<(Self::Handle, usize), StorageAllocError>

Implementors§