Struct acid_alloc::Slab
source · [−]pub struct Slab<A: BackingAllocator> { /* private fields */ }
Expand description
A slab allocator.
For a general discussion of slab allocation, see the module-level documentation.
Configuration
Each constructor takes the following parameters:
block_size
is the size in bytes of each allocation.num_blocks
is the maximum number of allocations that aSlab
may make at once.
Blocks are packed as tightly as possible. As such, the minimum guaranteed alignment of a block is
1 << block_size.trailing_zeros()
If a higher guaranteed alignment is required, the block size must be rounded up to the proper
alignment when creating the allocator. Calls to Slab::allocate
with a Layout
alignment
greater than the minimum guaranteed alignment will result in an error.
Implementations
sourceimpl Slab<Raw>
impl Slab<Raw>
sourcepub unsafe fn new_raw(
region: NonNull<u8>,
block_size: usize,
num_blocks: usize
) -> Result<Slab<Raw>, AllocInitError>
pub unsafe fn new_raw(
region: NonNull<u8>,
block_size: usize,
num_blocks: usize
) -> Result<Slab<Raw>, AllocInitError>
Constructs a new Slab
from a raw pointer to a region of memory.
Errors
Returns an error if Slab::region_layout(block_size, num_blocks)
would return an error
due to overflow.
Safety
The caller must uphold the following invariants:
region
must be a pointer to a region that fits theLayout
returned bySlab::region_layout(block_size, num_blocks)
, and it must be valid for reads and writes for the entire size indicated by thatLayout
.- No references to the memory at
region
may exist when this function is called. - As long as the returned
Slab
exists, no accesses may be made to the memory atregion
except by way of methods on the returnedSlab
.
sourceimpl Slab<Global>
impl Slab<Global>
sourcepub fn try_new(
block_size: usize,
num_blocks: usize
) -> Result<Slab<Global>, AllocInitError>
Available on crate feature alloc
only.
pub fn try_new(
block_size: usize,
num_blocks: usize
) -> Result<Slab<Global>, AllocInitError>
alloc
only.Attempts to construct a new Slab
backed by the global allocator.
The memory managed by this Slab
is allocated from the global allocator according to the
layout indicated by Slab::region_layout(block_size, num_blocks)
.
Errors
Returns an error if sufficient memory could not be allocated from the global allocator.
sourceimpl<A> Slab<A> where
A: Allocator,
impl<A> Slab<A> where
A: Allocator,
sourcepub fn try_new_in(
block_size: usize,
num_blocks: usize,
backing_allocator: A
) -> Result<Slab<A>, AllocInitError>
Available on crate feature unstable
only.
pub fn try_new_in(
block_size: usize,
num_blocks: usize,
backing_allocator: A
) -> Result<Slab<A>, AllocInitError>
unstable
only.Attempts to construct a new Slab
backed by backing_allocator
.
The memory managed by this Slab
is allocated from backing_allocator
according to the
layout indicated by Slab::region_layout(block_size, num_blocks)
.
Errors
Returns an error if sufficient memory could not be allocated from backing_allocator
.
sourceimpl<A> Slab<A> where
A: BackingAllocator,
impl<A> Slab<A> where
A: BackingAllocator,
sourcepub fn region_layout(
block_size: usize,
num_blocks: usize
) -> Result<Layout, AllocInitError>
pub fn region_layout(
block_size: usize,
num_blocks: usize
) -> Result<Layout, AllocInitError>
sourcepub fn allocate(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
pub fn allocate(&mut self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Attempts to allocate a block of memory with the specified layout.
The returned block is guaranteed to be aligned to self.block_align()
bytes.
Errors
Returns Err
if any of the following are true:
- No blocks are available.
layout.size()
is greater than the value returned byself.block_size()
.layout.align()
is greater than the value returned byself.block_align()
.
sourcepub fn allocate_block(&mut self) -> Result<NonNull<[u8]>, AllocError>
pub fn allocate_block(&mut self) -> Result<NonNull<[u8]>, AllocError>
Attempts to allocate a block of memory from the slab.
The returned block has a size of self.block_size()
and an alignment of
self.block_align()
.
Errors
Returns Err
if no blocks are available.
sourcepub unsafe fn deallocate(&mut self, ptr: NonNull<u8>)
pub unsafe fn deallocate(&mut self, ptr: NonNull<u8>)
Deallocates the memory referenced by ptr
.
Safety
ptr
must denote a block of memory currently allocated via this allocator.
sourcepub fn block_size(&self) -> usize
pub fn block_size(&self) -> usize
Returns the size in bytes of blocks allocated by this Slab
.
Example
use acid_alloc::Slab;
let slab = Slab::try_new(96, 8).unwrap();
assert_eq!(slab.block_size(), 96);
sourcepub fn block_align(&self) -> usize
pub fn block_align(&self) -> usize
Returns the minimum alignment of blocks allocated by this Slab
.
Example
use acid_alloc::Slab;
let slab = Slab::try_new(48, 12).unwrap();
assert_eq!(slab.block_align(), 16);
sourcepub fn num_blocks(&self) -> usize
pub fn num_blocks(&self) -> usize
Returns the number of blocks managed by this allocator.
Example
use acid_alloc::Slab;
let slab = Slab::try_new(64, 7).unwrap();
assert_eq!(slab.num_blocks(), 7);
sourcepub fn size(&self) -> usize
pub fn size(&self) -> usize
Returns the size in bytes of the managed region.
Example
use acid_alloc::Slab;
let slab = Slab::try_new(128, 4).unwrap();
assert_eq!(slab.size(), 512);
sourcepub fn limit(&self) -> Option<NonZeroUsize>
pub fn limit(&self) -> Option<NonZeroUsize>
Returns the first address above the managed region.
If the managed region ends at the end of the address space, returns None
.
sourcepub fn contains_ptr(&self, ptr: NonNull<u8>) -> bool
pub fn contains_ptr(&self, ptr: NonNull<u8>) -> bool
Returns true
iff ptr
is within this allocator’s managed region.
Note that a return value of true
does not indicate whether or not ptr
points into an
outstanding allocation.
sourcepub fn can_allocate(&self) -> bool
pub fn can_allocate(&self) -> bool
Returns true
iff this allocator can make at least one additional allocation.
sourcepub fn outstanding(&self) -> usize
pub fn outstanding(&self) -> usize
Returns the number of outstanding allocations.
Trait Implementations
sourceimpl<A> Debug for Slab<A> where
A: BackingAllocator,
impl<A> Debug for Slab<A> where
A: BackingAllocator,
sourceimpl<A> Drop for Slab<A> where
A: BackingAllocator,
impl<A> Drop for Slab<A> where
A: BackingAllocator,
impl<A> Send for Slab<A> where
A: BackingAllocator + Send,
impl<A> Sync for Slab<A> where
A: BackingAllocator + Sync,
Auto Trait Implementations
impl<A> RefUnwindSafe for Slab<A> where
A: RefUnwindSafe,
impl<A> Unpin for Slab<A> where
A: Unpin,
impl<A> UnwindSafe for Slab<A> where
A: UnwindSafe,
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