pub unsafe trait Allocator {
    fn allocate(&self, layout: Layout) -> Option<NonNull<u8>>;
    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);

    fn allocate_zeroed(&self, layout: Layout) -> Option<NonNull<u8>> { ... }
    unsafe fn grow(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout
    ) -> Option<NonNull<u8>> { ... } unsafe fn grow_zeroed(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout
    ) -> Option<NonNull<u8>> { ... } unsafe fn shrink(
        &self,
        ptr: NonNull<u8>,
        old_layout: Layout,
        new_layout: Layout
    ) -> Option<NonNull<u8>> { ... } fn as_ref(&self) -> &Self
    where
        Self: Sized
, { ... } }
Expand description

The Allocator trait provides an interface for collections and smart pointers to obtain memory

Safety

Pointers returned from Allocator::allocate must belong to logically different allocations. That is, deallocating one pointer returned from Allocator::allocate must not invalidate other pointers returned from other calls.

Further, all pointers returned from Allocator::allocate, Allocator::allocate_zeroed, Allocator::grow, Allocator::grow_zeroed, and Allocator::shrink must be aligned to at least the alignment given by layout, and be valid for both reads and writes of at least the size given by the layout.

The pointers returned from Allocator::allocate_zeroed and Allocator::grow_zeroed must contain size 0 bytes.

Allocators must be trivial or cheap to Copy or Clone. Further, Allocators obtained by Clone::cloneing a particular allocator must be mutually able to deallocate memory obtained from other clones of the same allocator

Required Methods

Allocates memory suitable for layout, and returns a pointer to it, or None if an error occurs. The value of the memory accessed via the pointer is uninitialized

Deallocates Memory obtained from a call to Allocator::allocate with the given layout

Safety

ptr must have been allocated by this allocator, a Clone::clone of this allocator, or a reference to this allocator, with the given layout

Provided Methods

Allocates memory suitable for layout, and returns a pointer to it, or None if an error occurs. Each byte pointed to by the return value up to layout.size() is 0

Grows an allocation given by ptr and old_layout, copying the data from the old allocation into the new allocation. ptr may not be used to access memory after a call to this function. It is unsound to call grow on core::pin::Pinned memory

Safety

ptr must have been allocated by this allocator, a Clone::clone of this allocator, or a reference to this allocator, with old_layout

The size of new_layout must be at least the size of old_layout

Grows an allocation given by ptr and old_layout, copying the data from the old allocation into the new allocation. ptr may not be used to access memory after a call to this function. It is unsound to call grow_zeroed on core::pin::Pinned memory

Any bytes between old_layout.size() and new_layout.size() are set to 0

Safety

ptr must have been allocated by this allocator, a Clone::clone of this allocator, or a reference to this allocator, with old_layout

The size of new_layout must be at least the size of old_layout

Grows an allocation given by ptr and old_layout, copying the data from the old allocation into the new allocation. ptr may not be used to access memory after a call to this function. It is unsound to call shrink on core::pin::Pinned memory

Safety

ptr must have been allocated by this allocator, a Clone::clone of this allocator, or a reference to this allocator, with old_layout

The size of new_layout must be at most the size of old_layout

Returns exactly self

Trait Implementations

The VTable for Self

The VTable for Self

The VTable for Self

The VTable for Self

Returns a reference to the vtable for T

Returns a reference to the vtable for T

Returns a reference to the vtable for T

Returns a reference to the vtable for T

Returns the size field of the VTable

Returns the align field of the VTable

Invokes the destructor (if any) on ptr Read more

Returns the size field of the VTable

Returns the align field of the VTable

Invokes the destructor (if any) on ptr Read more

Returns the size field of the VTable

Returns the align field of the VTable

Invokes the destructor (if any) on ptr Read more

Returns the size field of the VTable

Returns the align field of the VTable

Invokes the destructor (if any) on ptr Read more

Implementations on Foreign Types

Implementors