Alloc

Trait Alloc 

Source
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§

Source

fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Attempts to allocate a block of memory fitting the given Layout.

§Errors
Source

unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)

Deallocates a previously allocated block.

This is a noop if layout.size() == 0.

§Safety
  • ptr must point to a block of memory allocated using this allocator.
  • layout must describe exactly the same block.
§Panics

Some implementations may choose to panic if ptr or layout are invalid.

Provided Methods§

Source

fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Attempts to allocate a zeroed block of memory fitting the given Layout.

§Errors
Source

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
  • ptr must point to a block of memory allocated using this allocator.
  • old_layout must describe exactly the same block.
§Errors

On failure, the original memory won’t be deallocated or modified.

Source

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
  • ptr must point to a block of memory allocated using this allocator.
  • old_layout must describe exactly the same block.
§Errors

On failure, the original memory won’t be deallocated or modified.

Source

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
  • ptr must point to a block of memory allocated using this allocator.
  • old_layout must describe exactly the same block.
§Errors

On failure, the original memory won’t be deallocated or modified.

Source

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
  • ptr must point to a block previously allocated with this allocator.
  • old_layout must describe exactly that block.
§Errors

On failure, the original memory won’t be deallocated or modified.

Source

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
  • ptr must point to a block previously allocated with this allocator.
  • old_layout must describe exactly that block.
§Errors

On failure, the original memory won’t be deallocated or modified.

Implementations on Foreign Types§

Source§

impl<A: Alloc + ?Sized> Alloc for &A

Source§

fn alloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Source§

fn zalloc(&self, layout: Layout) -> Result<NonNull<u8>, AllocError>

Source§

unsafe fn dealloc(&self, ptr: NonNull<u8>, layout: Layout)

Implementors§