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::clone
ing 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
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
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::Pin
ned 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::Pin
ned 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::Pin
ned 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
Trait Implementations
sourceimpl AbiSafeTrait for dyn Allocator
impl AbiSafeTrait for dyn Allocator
type VTable = AllocatorVTable
type VTable = AllocatorVTable
The VTable
for Self
sourceimpl AbiSafeTrait for dyn Allocator + Send
impl AbiSafeTrait for dyn Allocator + Send
type VTable = AllocatorVTable
type VTable = AllocatorVTable
The VTable
for Self
sourceimpl AbiSafeTrait for dyn Allocator + Sync
impl AbiSafeTrait for dyn Allocator + Sync
type VTable = AllocatorVTable
type VTable = AllocatorVTable
The VTable
for Self
sourceimpl AbiSafeTrait for dyn Allocator + Send + Sync
impl AbiSafeTrait for dyn Allocator + Send + Sync
type VTable = AllocatorVTable
type VTable = AllocatorVTable
The VTable
for Self
sourceimpl<T: Allocator> AbiSafeUnsize<T> for dyn Allocator
impl<T: Allocator> AbiSafeUnsize<T> for dyn Allocator
sourcefn construct_vtable_for() -> &'static Self::VTable
fn construct_vtable_for() -> &'static Self::VTable
Returns a reference to the vtable for T
sourceimpl<T: Allocator + Send> AbiSafeUnsize<T> for dyn Allocator + Send
impl<T: Allocator + Send> AbiSafeUnsize<T> for dyn Allocator + Send
sourcefn construct_vtable_for() -> &'static Self::VTable
fn construct_vtable_for() -> &'static Self::VTable
Returns a reference to the vtable for T
sourceimpl<T: Allocator + Sync> AbiSafeUnsize<T> for dyn Allocator + Sync
impl<T: Allocator + Sync> AbiSafeUnsize<T> for dyn Allocator + Sync
sourcefn construct_vtable_for() -> &'static Self::VTable
fn construct_vtable_for() -> &'static Self::VTable
Returns a reference to the vtable for T
sourceimpl<T: Allocator + Send + Sync> AbiSafeUnsize<T> for dyn Allocator + Send + Sync
impl<T: Allocator + Send + Sync> AbiSafeUnsize<T> for dyn Allocator + Send + Sync
sourcefn construct_vtable_for() -> &'static Self::VTable
fn construct_vtable_for() -> &'static Self::VTable
Returns a reference to the vtable for T