pub unsafe trait Allocator: Debug + ReprC {
type SpecificAllocationError: Debug + ReprC + WithMessage;
// Required methods
fn allocate(
&mut self,
layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>;
unsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout);
// Provided method
unsafe fn resize(
&mut 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: Debug + ReprC + WithMessage
type SpecificAllocationError: Debug + ReprC + WithMessage
A specific Allocator error. Implementor can add additional information to the error, not only generic “allocation failed”.
Required Methods§
Sourcefn allocate(
&mut self,
layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>
fn allocate( &mut 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 implementation is allowed to provide additional info.
Sourceunsafe fn deallocate(&mut self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&mut 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(
&mut self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<u8>, Self::SpecificAllocationError>
unsafe fn resize( &mut 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 implementation 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".
Implementors§
Source§impl Allocator for StdAllocator
Available on crate feature std only.
impl Allocator for StdAllocator
std only.