Struct slabmalloc::SCAllocator
source · [−]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
sourceimpl<'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 where
F: FnMut(*mut P),
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.
Auto Trait Implementations
impl<'a, P> RefUnwindSafe for SCAllocator<'a, P> where
P: RefUnwindSafe,
impl<'a, P> Send for SCAllocator<'a, P> where
P: Send,
impl<'a, P> Sync for SCAllocator<'a, P> where
P: Sync,
impl<'a, P> Unpin for SCAllocator<'a, P>
impl<'a, P> !UnwindSafe for SCAllocator<'a, P>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more