pub struct SCAllocator<'a, P: AllocablePage> { /* private fields */ }Expand description
A slab allocator allocates elements of a fixed size.
It maintains three internal lists of objects that implement AllocablePage
from which it can allocate memory.
empty_slabs: Is a list of pages that the SCAllocator maintains, but has 0 allocations in them, these can be given back to a requestor in case of reclamation.slabs: A list of pages partially allocated and still have room for more.full_slabs: A list of pages that are completely allocated.
On allocation we allocate memory from slabs, however if the list is empty
we try to reclaim a page from empty_slabs before we return with an out-of-memory
error. If a page becomes full after the allocation we move it from slabs to
full_slabs.
Similarly, on dealloaction we might move a page from full_slabs to slabs
or from slabs to empty_slabs after we deallocated an object.
If an allocation returns OutOfMemory a client using SCAllocator can refill
it using the refill function.
Implementations§
Source§impl<'a, P: AllocablePage> SCAllocator<'a, P>
impl<'a, P: AllocablePage> SCAllocator<'a, P>
Sourcepub const fn new(size: usize) -> SCAllocator<'a, P>
pub const fn new(size: usize) -> SCAllocator<'a, P>
Create a new SCAllocator.
pub fn try_reclaim_pages<F>( &mut self, to_reclaim: usize, dealloc: &mut F, ) -> usize
Sourcepub fn allocate(
&mut self,
layout: Layout,
) -> Result<NonNull<u8>, AllocationError>
pub fn allocate( &mut self, layout: Layout, ) -> Result<NonNull<u8>, AllocationError>
Allocates a block of memory descriped by layout.
Returns a pointer to a valid region of memory or an AllocationError.
The function may also move around pages between lists (empty -> partial or partial -> full).
Sourcepub fn deallocate(
&self,
ptr: NonNull<u8>,
layout: Layout,
) -> Result<(), AllocationError>
pub fn deallocate( &self, ptr: NonNull<u8>, layout: Layout, ) -> Result<(), AllocationError>
Deallocates a previously allocated ptr described by Layout.
May return an error in case an invalid layout is provided.
The function may also move internal slab pages between lists partial -> empty
or full -> partial lists.