pub trait Alloc {
// Required methods
fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>;
unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout);
// Provided methods
fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError> { ... }
unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError> { ... }
unsafe fn zgrow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError> { ... }
unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError> { ... }
unsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError> { ... }
unsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError> { ... }
}Expand description
A memory allocation interface.
This trait does not require Self: Allocator and is no_std-compatible.
Required Methods§
Sourcefn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
Attempts to allocate a block of memory fitting the given Layout.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::ZeroSizedLayoutiflayouthas a size of zero.
Provided Methods§
Sourcefn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>
Attempts to allocate a zeroed block of memory fitting the given Layout.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::ZeroSizedLayoutiflayouthas a size of zero.
Sourceunsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, AllocError>
Grow the given block to a new, larger layout.
Returns the new pointer, possibly reallocated elsewhere.
§Safety
ptrmust point to a block of memory allocated using this allocator.old_layoutmust describe exactly the same block.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::GrowSmallerNewLayoutifnew_layout.size() < old_layout.size().AllocError::ZeroSizedLayoutifnew_layouthas a size of zero.
On failure, the original memory won’t be deallocated or modified.
Sourceunsafe fn zgrow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError>
unsafe fn zgrow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, AllocError>
Grows the given block to a new, larger layout, zeroing any newly allocated bytes.
Returns the new pointer, possibly reallocated elsewhere.
§Safety
ptrmust point to a block of memory allocated using this allocator.old_layoutmust describe exactly the same block.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::GrowSmallerNewLayoutinnew_layout.size() < old_layout.size().AllocError::ZeroSizedLayoutifnew_layouthas a size of zero.
On failure, the original memory won’t be deallocated or modified.
Sourceunsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, AllocError>
Shrink the given block to a new, smaller layout.
§Safety
ptrmust point to a block of memory allocated using this allocator.old_layoutmust describe exactly the same block.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::ShrinkLargerNewLayoutifnew_layout.size() > old_layout.size().AllocError::ZeroSizedLayoutifnew_layouthas a size of zero.
On failure, the original memory won’t be deallocated or modified.
Sourceunsafe fn realloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError>
unsafe fn realloc( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, AllocError>
Reallocates a block, growing or shrinking as needed.
On grow, preserves existing contents up to old_layout.size(), and
on shrink, truncates to new_layout.size().
§Safety
ptrmust point to a block previously allocated with this allocator.old_layoutmust describe exactly that block.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::ZeroSizedLayoutifnew_layouthas a size of zero.
On failure, the original memory won’t be deallocated or modified.
Sourceunsafe fn rezalloc(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, AllocError>
unsafe fn rezalloc( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, AllocError>
Reallocates a block, growing or shrinking as needed, zeroing any newly allocated bytes.
§Safety
ptrmust point to a block previously allocated with this allocator.old_layoutmust describe exactly that block.
§Errors
AllocError::AllocFailedif allocation fails.AllocError::ZeroSizedLayoutifnew_layouthas a size of zero.
On failure, the original memory won’t be deallocated or modified.