pub unsafe trait Allocator:
Debug
+ Default
+ Clone {
type SpecificAllocationError: Into<AllocationError> + Debug;
// Required methods
fn allocate(
&self,
layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
// Provided method
unsafe fn resize(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError> { ... }
}
Expand description
Represents an Allocator.
§Safety
This trait is inherently unsafe, since it depends on well managed
raw pointers. For example it is possible to call Allocator::deallocate
twice on the same pointer, which is an Undefined Behaviour.
Required Associated Types§
Sourcetype SpecificAllocationError: Into<AllocationError> + Debug
type SpecificAllocationError: Into<AllocationError> + Debug
A specific Allocator error. Implementor can add additional information to the error, not only generic “allocation failed”.
Required Methods§
Sourcefn allocate(
&self,
layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>
fn allocate( &self, layout: Layout, ) -> Result<NonNull<u8>, Self::SpecificAllocationError>
Allocates new piece of memory. This is the only safe
function here. The returned ptr
is guaranteed to satisfy
layout
requirements (although the implementation is free
to overallocate and strengthen alignment).
§Errors
An error typically means out-of-memory error. But the implemtation is allowed to provide additional info.
Sourceunsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
Deallocates memory.
§Safety
Passed ptr
has to be created with previous call to Allocator::allocate
or Allocator::resize
. Layouts have to match. Using the passed ptr
after the call to this function is an Undefined Behaviour.
Provided Methods§
Sourceunsafe fn resize(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>
unsafe fn resize( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<u8>, Self::SpecificAllocationError>
Resizes given ptr
to the new layout.
§Errors
An error typically means out-of-memory error. But the implemtation is allowed to provide additional info.
§Safety
ptr
has to be a valid pointer created with previous call to
Allocator::allocate
or Allocator::resize
. old_layout
has to match the pointers layout. Using the passed ptr after
the call to this function is an Undefined Behaviour. Use the
return value instead.
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.