SyncStalloc

Struct SyncStalloc 

Source
pub struct SyncStalloc<const L: usize, const B: usize>(/* private fields */)
where
    Align<B>: Alignment;
Expand description

A wrapper around UnsafeStalloc that is safe to create because it prevents data races using a Mutex. In comparison to UnsafeStalloc, the mutex may cause a slight overhead.

Implementations§

Source§

impl<const L: usize, const B: usize> SyncStalloc<L, B>
where Align<B>: Alignment,

Source

pub const fn new() -> Self

Initializes a new empty SyncStalloc instance.

§Examples
use stalloc::SyncStalloc;

let alloc = SyncStalloc::<200, 8>::new();
Examples found in repository?
examples/chained.rs (line 9)
9static GLOBAL: AllocChain<SyncStalloc<1024, 8>, System> = SyncStalloc::new().chain(&System);
Source

pub fn is_oom(&self) -> bool

Checks if the allocator is completely out of memory. If this is false, then you are guaranteed to be able to allocate a layout with a size and alignment of B bytes. This runs in O(1).

Source

pub fn is_empty(&self) -> bool

Checks if the allocator is empty. If this is true, then you are guaranteed to be able to allocate a layout with a size of B * L bytes and an alignment of B bytes. If this is false, then this is guaranteed to be impossible. This runs in O(1).

Source

pub unsafe fn clear(&self)

§Safety

Calling this function immediately invalidates all pointers into the allocator. Calling deallocate_blocks() with an invalidated pointer will result in the free list being corrupted.

Source

pub unsafe fn allocate_blocks( &self, size: usize, align: usize, ) -> Result<NonNull<u8>, AllocError>

Tries to allocate count blocks. If the allocation succeed, a pointer is returned. This function never allocates more than necessary.

§Safety

size must be nonzero, and align must be a power of 2 in the range 1..=2^29 / B.

§Errors

Will return AllocError if the allocation was unsuccessful, in which case this function was a no-op.

Source

pub unsafe fn deallocate_blocks(&self, ptr: NonNull<u8>, size: usize)

Deallocates a pointer.

§Safety

ptr must point to an allocation, and size must be the number of blocks in the allocation. That is, size is always in 1..=L.

Source

pub unsafe fn shrink_in_place( &self, ptr: NonNull<u8>, old_size: usize, new_size: usize, )

Shrinks the allocation. This function always succeeds and never reallocates.

§Safety

ptr must point to a valid allocation of old_size blocks, and new_size must be in 1..old_size.

Source

pub unsafe fn grow_in_place( &self, ptr: NonNull<u8>, old_size: usize, new_size: usize, ) -> Result<(), AllocError>

Tries to grow the current allocation in-place. If that isn’t possible, this function is a no-op.

§Safety

ptr must point to a valid allocation of old_size blocks. Also, new_size > old_size.

§Errors

Will return AllocError if the grow was unsuccessful, in which case this function was a no-op.

Source

pub unsafe fn grow_up_to( &self, ptr: NonNull<u8>, old_size: usize, new_size: usize, ) -> usize

Tries to grow the current allocation in-place. If that isn’t possible, the allocator grows by as much as it is able to, and the new length of the allocation is returned. The new length is guaranteed to be in the range old_size..=new_size.

§Safety

ptr must point to a valid allocation of old_size blocks. Also, new_size > old_size.

Source

pub fn acquire_locked(&self) -> StallocGuard<'_, L, B>

Acquires an exclusive lock for the allocator. This can be used to chain multiple operations on the allocator without having to repeatedly acquire locks for each one.

§Example
use stalloc::SyncStalloc;

let alloc = SyncStalloc::<100, 4>::new();

let lock = alloc.acquire_locked();
for _ in 0..20 {
    // make multiple allocations in a row
    unsafe { lock.allocate_blocks(5, 1) }.unwrap();
}
drop(lock); // until we drop the lock, all accesses to `alloc` will block

assert!(alloc.is_oom());
Source§

impl<const L: usize, const B: usize> SyncStalloc<L, B>
where Align<B>: Alignment,

Source

pub const fn chain<T>(self, next: &T) -> AllocChain<'_, Self, T>
where Self: Sized,

Creates a new AllocChain containing this allocator and next.

Examples found in repository?
examples/chained.rs (line 9)
9static GLOBAL: AllocChain<SyncStalloc<1024, 8>, System> = SyncStalloc::new().chain(&System);

Trait Implementations§

Source§

impl<const L: usize, const B: usize> ChainableAlloc for SyncStalloc<L, B>
where Align<B>: Alignment,

Source§

fn claims(&self, ptr: *mut u8, layout: Layout) -> bool

Tests whether a certain allocation belongs to this allocator. This is called when using deallocate() and related functions in order to determine which allocator needs to free the pointer.
Source§

impl<const L: usize, const B: usize> Debug for SyncStalloc<L, B>
where Align<B>: Alignment,

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<const L: usize, const B: usize> Default for SyncStalloc<L, B>
where Align<B>: Alignment,

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<const L: usize, const B: usize> GlobalAlloc for SyncStalloc<L, B>
where Align<B>: Alignment,

Source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocates memory as described by the given layout. Read more
Source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more
Source§

unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout)

Deallocates the block of memory at the given ptr pointer with the given layout. Read more
Source§

unsafe fn realloc( &self, ptr: *mut u8, old_layout: Layout, new_size: usize, ) -> *mut u8

Shrinks or grows a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more

Auto Trait Implementations§

§

impl<const L: usize, const B: usize> !Freeze for SyncStalloc<L, B>

§

impl<const L: usize, const B: usize> !RefUnwindSafe for SyncStalloc<L, B>

§

impl<const L: usize, const B: usize> !Send for SyncStalloc<L, B>

§

impl<const L: usize, const B: usize> Sync for SyncStalloc<L, B>

§

impl<const L: usize, const B: usize> !Unpin for SyncStalloc<L, B>

§

impl<const L: usize, const B: usize> !UnwindSafe for SyncStalloc<L, B>

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.