Struct mnemos_bitslab::index::alloc32::IndexAlloc32
source · pub struct IndexAlloc32 {
bitmap: AtomicU32,
max_mask: u32,
}
Expand description
An allocator for up to $cap unique indices.
Fields§
§bitmap: AtomicU32
§max_mask: u32
Implementations§
source§impl IndexAlloc32
impl IndexAlloc32
sourcepub const fn with_capacity(capacity: u8) -> Self
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 IndexAlloc32
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
sourcepub const MAX_CAPACITY: u8 = 32u8
pub const MAX_CAPACITY: u8 = 32u8
The maximum number of indices that can be allocated by an allocator of this type.
sourcepub fn free(&self, index: u8)
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
.
sourcepub fn all_allocated(&self) -> bool
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::IndexAlloc32;
let alloc = IndexAlloc32::new();
assert!(!alloc.all_allocated());
// allocate all but one index
for _ in 1..32 {
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());
sourcepub fn all_free(&self) -> bool
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::IndexAlloc32;
let alloc = IndexAlloc32::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());
sourcepub fn any_allocated(&self) -> bool
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::IndexAlloc32;
let alloc = IndexAlloc32::new();
assert!(!alloc.any_allocated());
// allocate all indices
for _ in 0..32 {
alloc.allocate().expect("should have free indices");
assert!(alloc.any_allocated());
}
// free all but one index.
for i in 1..IndexAlloc32::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());
sourcepub fn any_free(&self) -> bool
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::IndexAlloc32;
let alloc = IndexAlloc32::new();
assert!(alloc.any_free());
// allocate all but one index
for _ in 1..32 {
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());
sourcepub fn free_count(&self) -> u8
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::IndexAlloc32;
let alloc = IndexAlloc32::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);
sourcepub fn allocated_count(&self) -> u8
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::IndexAlloc32;
let alloc = IndexAlloc32::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);
sourcepub const fn capacity(&self) -> u8
pub const fn capacity(&self) -> u8
Returns the total capacity of this allocator, including any allocated indices.
sourcepub fn iter_allocated(&self) -> AllocatedIndices ⓘ
pub fn iter_allocated(&self) -> AllocatedIndices ⓘ
Returns an iterator over the indices that have been allocated at the current point in time.