pub unsafe trait AltAllocator {
// Required methods
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>;
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout);
// Provided methods
fn allocate_zeroed(
&self,
layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> { ... }
unsafe fn grow(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> { ... }
unsafe fn grow_zeroed(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> { ... }
unsafe fn shrink(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> { ... }
}
Expand description
The rust allocator API is not stable yet. Therefore, this trait can be used to implement/wrap a custom allocator in a no_std environment. It mirrors the unstable allocator API at the moment.
This mirrors the safety requirements of the allocator API: https://doc.rust-lang.org/std/alloc/trait.Allocator.html
If the allocator api is ever marked as stable this trait probably can be removed.
Required Methods§
Sourcefn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Allocates a chunk of memory with the given layout.
On success it returns a pointer to the allocated memory.
If the allocation fails or has some kinda of error it will return
an AllocError
.
Sourceunsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
Deallocates the chunk of memory pointed at byptr
This memory must have only been allocated by this allocator. The layout must match the layout provided when the chunk was allocated.
Provided Methods§
Sourcefn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Allocates just like allocate
but also zeroes the memory.
Sourceunsafe fn grow(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, old_ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Grows the memory pointed at by old_ptr
to the new layout.
The new layout must be larger than the old layout.
If this fails the old ptr must still will be valid. If it succeeds the old ptr is not longer valid, and the ptr returned must be used instead.
Sourceunsafe fn grow_zeroed(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, old_ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Behaves just like grow
but the new memory will be zeroed.
Sourceunsafe fn shrink(
&self,
old_ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, old_ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
Shrinks the memory pointed at by old_ptr
to the new layout.
The new layout must be smaller than the old layout.
If this fails the old ptr will still be valid. If it succeeds the old ptr is not longer valid, and the ptr returned must be used instead.