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
Storage::resolvemust return a valid pointer to the allocation when passed a validStorage::Handle
Required Associated Types§
Sourcetype Handle: StorageHandle
type Handle: StorageHandle
The StorageHandle type that represents an allocation by this Storage
Required Methods§
Sourcefn allocate(
&self,
layout: Layout,
) -> Result<(Self::Handle, usize), StorageAllocError>
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
Sourceunsafe fn deallocate(&self, layout: Layout, handle: Self::Handle)
unsafe fn deallocate(&self, layout: Layout, handle: Self::Handle)
Deallocates (and invalidates) a StorageHandle that was allocated with this Storage
§Safety
layoutmust 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)handlemust be valid
Sourceunsafe fn grow(
&self,
old_layout: Layout,
new_layout: Layout,
handle: Self::Handle,
) -> Result<(Self::Handle, usize), StorageAllocError>
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()handlemust be valid- if this method succeeds,
handleis now invalid and cannot be used
Sourceunsafe fn shrink(
&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>
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()handlemust be valid- if this method succeeds,
handleis now invalid and cannot be used