Skip to main content

Allocator

Trait Allocator 

Source
pub trait Allocator: Clone {
    // Required methods
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
    unsafe fn grow(
        &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>;
    fn allocate_zeroed(
        &self,
        layout: Layout,
    ) -> Result<NonNull<[u8]>, AllocError>;
}
Expand description

Trait for allocators used by Box and other collections.

This trait mirrors the core::alloc::Allocator trait, returning Result<NonNull<[u8]>, AllocError> to provide the actual size allocated.

Implementations of this trait must tolerate being passed zero-sized layouts. For zero-sized allocations, implementations should return a dangling pointer with the appropriate alignment, and deallocate should be a no-op for such pointers.

Required Methods§

Source

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

Allocates memory as described by the given layout.

If the layout has a size of zero, this method returns a dangling pointer.

Source

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

§Safety
  • The pointer must have been allocated by allocate with the same layout.

If the layout has a size of zero, this method is a no-op.

Source

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

§Safety
  • ptr must denote a block of memory currently allocated via this allocator.
  • old_layout must fit that block of memory (The new_layout argument need not fit it.).
  • new_layout.size() must be greater than or equal to old_layout.size().

Note that new_layout.align() need not be the same as old_layout.align().

If the new layout has a size of zero, this method returns a dangling pointer.

Source

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

§Safety
  • ptr must denote a block of memory currently allocated via this allocator.
  • old_layout must fit that block of memory (The new_layout argument need not fit it.).
  • new_layout.size() must be less than or equal to old_layout.size().

Note that new_layout.align() need not be the same as old_layout.align().

If the new layout has a size of zero, this method deallocates the memory and returns a dangling pointer.

Source

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

Allocates zeroed memory as described by the given layout.

If the layout has a size of zero, this method returns a dangling pointer.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§