pub struct HybridAllocator<const N: usize, F: Allocator> { /* private fields */ }Expand description
A hybrid allocator that first tries to allocate from a stack‑backed bump allocator and falls back to a user‑provided allocator.
N - size of the stack buffer in bytes.
F - the fallback allocator type (e.g. std::alloc::Global or any custom
allocator that implements Allocator).
Implementations§
Source§impl<const N: usize, F: Allocator> HybridAllocator<N, F>
impl<const N: usize, F: Allocator> HybridAllocator<N, F>
Sourcepub const fn new(fallback: F) -> Self
pub const fn new(fallback: F) -> Self
Create a new hybrid allocator.
The caller supplies the fallback allocator that will be used when the stack buffer cannot satisfy a request.
Sourcepub unsafe fn reset(&mut self)
pub unsafe fn reset(&mut self)
Reset the allocator, discarding all previously allocated memory.
§Safety
the caller must guarantee that no live allocation created by this allocator is still in use.
Sourcepub fn current_offset(&self) -> usize
pub fn current_offset(&self) -> usize
Get the current offset of the stack allocator.
Sourcepub fn is_stack_exausted(&self) -> bool
pub fn is_stack_exausted(&self) -> bool
Check if the last allocation used the fallback allocator. if true, the stack buffer is exhausted and all further allocations will go to the fallback until reset is called.
Trait Implementations§
Source§impl<const N: usize, F: Allocator> Allocator for HybridAllocator<N, F>
impl<const N: usize, F: Allocator> Allocator for HybridAllocator<N, F>
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout)
allocator_api)ptr. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
allocator_api)Source§fn by_ref(&self) -> &Selfwhere
Self: Sized,
fn by_ref(&self) -> &Selfwhere
Self: Sized,
allocator_api)Allocator. Read moreSource§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocator_api)allocate, but also ensures that the returned memory is zero-initialized. Read more