SCAllocator

Struct 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§

Source§

impl<'a, P: AllocablePage> SCAllocator<'a, P>

Source

pub const fn new(size: usize) -> SCAllocator<'a, P>

Create a new SCAllocator.

Source

pub fn size(&self) -> usize

Returns the maximum supported object size of this allocator.

Source

pub fn try_reclaim_pages<F>( &mut self, to_reclaim: usize, dealloc: &mut F, ) -> usize
where F: FnMut(*mut P),

Source

pub unsafe fn refill(&mut self, page: &'a mut P)

Refill the SCAllocator

§Safety

ObjectPage needs to be empty etc.

Source

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).

Source

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> Freeze for SCAllocator<'a, P>

§

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§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.