Struct mnemos_bitslab::index::IndexAllocWord

source ·
pub struct IndexAllocWord {
    bitmap: AtomicUsize,
    max_mask: usize,
}
Expand description

An allocator for up to $cap unique indices.

Fields§

§bitmap: AtomicUsize§max_mask: usize

Implementations§

source§

impl IndexAllocWord

source

pub const fn new() -> Self

Returns a new allocator for up to $cap unique indices.

source

pub const fn with_capacity(capacity: u8) -> Self

Returns a new allocator for up to capacity unique indices. If capacity indices are allocated, subsequent calls to allocate() will return None until an index is deallocated by a call to free() on this allocator.

A IndexAllocWord can only ever allocate up to Self::MAX_CAPACITY indices. Therefore, if the provided capacity exceeds Self::MAX_CAPACITY, it will be clamped to the maximum capacity.

An allocator’s actual capacity can be returned

source

pub fn allocate(&self) -> Option<u8>

Allocate an index from the pool.

If this method returns Some, the returned u8 index will not be returned again until after it has been freed.

source

pub const MAX_CAPACITY: u8 = 64u8

The maximum number of indices that can be allocated by an allocator of this type.

source

pub fn free(&self, index: u8)

Release an index back to the pool.

The freed index may now be returned by a subsequent call to allocate.

source

pub fn all_allocated(&self) -> bool

Returns true if all indices in the allocator have been allocated.

This is the inverse of any_free.

§Examples
 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert!(!alloc.all_allocated());

// allocate all but one index
 for _ in 1..usize::BITS {
    alloc.allocate().expect("should have free indices");
    assert!(!alloc.all_allocated());
}

// allocate the last index.
let last = alloc.allocate().expect("should have one more index remaining");
assert!(alloc.all_allocated());

// freeing the index should make it available again
alloc.free(last);
assert!(!alloc.all_allocated());
source

pub fn all_free(&self) -> bool

Returns true if none of this allocator’s indices have been allocated.

This is the inverse of any_allocated.

§Examples
 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert!(alloc.all_free());

let idx = alloc.allocate().expect("a fresh allocator should have indices!");
assert!(!alloc.all_free());

// free the last index. now, `all_free` will return `true` again.
alloc.free(idx);
assert!(alloc.all_free());
source

pub fn any_allocated(&self) -> bool

Returns true if any index in the allocator has been allocated.

This is the inverse of all_free.

§Examples
 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert!(!alloc.any_allocated());

// allocate all indices
 for _ in 0..usize::BITS {
    alloc.allocate().expect("should have free indices");
    assert!(alloc.any_allocated());
}

// free all but one index.
 for i in 1..IndexAllocWord::MAX_CAPACITY {
    alloc.free(i);
    assert!(alloc.any_allocated());
}

// free the last index. now, `any_allocated` will return `false`.
alloc.free(0);
assert!(!alloc.any_allocated());
source

pub fn any_free(&self) -> bool

Returns true if any index in the allocator is available.

This is the inverse of all_allocated.

§Examples
 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert!(alloc.any_free());

// allocate all but one index
 for _ in 1..usize::BITS {
    alloc.allocate().expect("should have free indices");
    assert!(alloc.any_free());
}

// allocate the last index.
let last = alloc.allocate().expect("should have one more index remaining");
assert!(!alloc.any_free());

// freeing the index should make it available again
alloc.free(last);
assert!(alloc.any_free());
source

pub fn free_count(&self) -> u8

Returns the current number of free indices in the allocator.

This will always be [self.capacity()] or less.

 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert_eq!(alloc.free_count(), alloc.capacity());

let idx1 = alloc.allocate().expect("all indices should be free");
assert_eq!(alloc.free_count(), alloc.capacity() - 1);

let idx2 = alloc.allocate().expect("most indices should be free");
assert_eq!(alloc.free_count(), alloc.capacity() - 2);

alloc.free(idx1);
assert_eq!(alloc.free_count(), alloc.capacity() - 1);
source

pub fn allocated_count(&self) -> u8

Returns the current number of allocated indices in the allocator.

This will always be [self.capacity()] or less.

§Examples
 use mnemos_bitslab::index::IndexAllocWord;

 let alloc = IndexAllocWord::new();
assert_eq!(alloc.allocated_count(), 0);

let idx1 = alloc.allocate().expect("all indices should be free");
assert_eq!(alloc.allocated_count(), 1);

let idx2 = alloc.allocate().expect("most indices should be free");
assert_eq!(alloc.allocated_count(), 2);

alloc.free(idx1);
assert_eq!(alloc.allocated_count(), 1);
source

pub const fn capacity(&self) -> u8

Returns the total capacity of this allocator, including any allocated indices.

source

pub fn iter_allocated(&self) -> AllocatedIndices

Returns an iterator over the indices that have been allocated at the current point in time.

source

const fn capacity_subtractor(&self) -> u8

source

fn find_zero(u: usize) -> Option<u8>

Trait Implementations§

source§

impl Debug for IndexAllocWord

source§

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

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

impl Default for IndexAllocWord

source§

fn default() -> Self

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

Auto Trait Implementations§

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>,

§

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>,

§

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.