pub struct BitmapVec { /* private fields */ }Expand description
Represents an owned bitmap.
Wraps underlying C bitmap API. See Bitmap for available
methods.
§Examples
Basic usage
use kernel::alloc::flags::GFP_KERNEL;
use kernel::bitmap::BitmapVec;
let mut b = BitmapVec::new(16, GFP_KERNEL)?;
assert_eq!(16, b.len());
for i in 0..16 {
if i % 4 == 0 {
b.set_bit(i);
}
}
assert_eq!(Some(0), b.next_bit(0));
assert_eq!(Some(1), b.next_zero_bit(0));
assert_eq!(Some(4), b.next_bit(1));
assert_eq!(Some(5), b.next_zero_bit(4));
assert_eq!(Some(12), b.last_bit());§Invariants
nbitsis<= MAX_LEN.- if
nbits <= MAX_INLINE_LEN, thenrepris ausize. - otherwise,
reprholds a non-null pointer to an initialized array ofunsigned longthat is large enough to holdnbitsbits.
Implementations§
Source§impl BitmapVec
impl BitmapVec
Sourcepub const MAX_INLINE_LEN: usize
pub const MAX_INLINE_LEN: usize
The maximum length that uses the inline representation.
Sourcepub fn new_inline() -> Self
pub fn new_inline() -> Self
Construct a longest possible inline BitmapVec.
Sourcepub fn new(nbits: usize, flags: Flags) -> Result<Self, AllocError>
pub fn new(nbits: usize, flags: Flags) -> Result<Self, AllocError>
Constructs a new BitmapVec.
Fails with AllocError when the BitmapVec could not be allocated. This
includes the case when nbits is greater than MAX_LEN.
Methods from Deref<Target = Bitmap>§
Sourcepub fn as_mut_ptr(&mut self) -> *mut usize
pub fn as_mut_ptr(&mut self) -> *mut usize
Returns a mutable raw pointer to the backing Bitmap.
Sourcepub fn set_bit(&mut self, index: usize)
pub fn set_bit(&mut self, index: usize)
Set bit with index index.
ATTENTION: set_bit is non-atomic, which differs from the naming
convention in C code. The corresponding C function is __set_bit.
If CONFIG_RUST_BITMAP_HARDENED is not enabled and index is greater than
or equal to self.nbits, does nothing.
§Panics
Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and index is greater than
or equal to self.nbits.
Sourcepub fn set_bit_atomic(&self, index: usize)
pub fn set_bit_atomic(&self, index: usize)
Set bit with index index, atomically.
This is a relaxed atomic operation (no implied memory barriers).
ATTENTION: The naming convention differs from C, where the corresponding
function is called set_bit.
If CONFIG_RUST_BITMAP_HARDENED is not enabled and index is greater than
or equal to self.len(), does nothing.
§Panics
Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and index is greater than
or equal to self.len().
Sourcepub fn clear_bit(&mut self, index: usize)
pub fn clear_bit(&mut self, index: usize)
Clear index bit.
ATTENTION: clear_bit is non-atomic, which differs from the naming
convention in C code. The corresponding C function is __clear_bit.
If CONFIG_RUST_BITMAP_HARDENED is not enabled and index is greater than
or equal to self.len(), does nothing.
§Panics
Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and index is greater than
or equal to self.len().
Sourcepub fn clear_bit_atomic(&self, index: usize)
pub fn clear_bit_atomic(&self, index: usize)
Clear index bit, atomically.
This is a relaxed atomic operation (no implied memory barriers).
ATTENTION: The naming convention differs from C, where the corresponding
function is called clear_bit.
If CONFIG_RUST_BITMAP_HARDENED is not enabled and index is greater than
or equal to self.len(), does nothing.
§Panics
Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and index is greater than
or equal to self.len().
Sourcepub fn copy_and_extend(&mut self, src: &Bitmap)
pub fn copy_and_extend(&mut self, src: &Bitmap)
Copy src into this Bitmap and set any remaining bits to zero.
§Examples
use kernel::alloc::{AllocError, flags::GFP_KERNEL};
use kernel::bitmap::BitmapVec;
let mut long_bitmap = BitmapVec::new(256, GFP_KERNEL)?;
assert_eq!(None, long_bitmap.last_bit());
let mut short_bitmap = BitmapVec::new(16, GFP_KERNEL)?;
short_bitmap.set_bit(7);
long_bitmap.copy_and_extend(&short_bitmap);
assert_eq!(Some(7), long_bitmap.last_bit());
Sourcepub fn last_bit(&self) -> Option<usize>
pub fn last_bit(&self) -> Option<usize>
Finds last set bit.
§Examples
use kernel::alloc::{AllocError, flags::GFP_KERNEL};
use kernel::bitmap::BitmapVec;
let bitmap = BitmapVec::new(64, GFP_KERNEL)?;
match bitmap.last_bit() {
Some(idx) => {
pr_info!("The last bit has index {idx}.\n");
}
None => {
pr_info!("All bits in this bitmap are 0.\n");
}
}Trait Implementations§
impl Send for BitmapVec
Enable ownership transfer to other threads.
SAFETY: We own the underlying bitmap representation.
impl Sync for BitmapVec
Enable unsynchronized concurrent access to BitmapVec through shared references.
SAFETY: deref() will return a reference to a Bitmap. Its methods
take immutable references are either atomic or read-only.
Auto Trait Implementations§
impl Freeze for BitmapVec
impl RefUnwindSafe for BitmapVec
impl Unpin for BitmapVec
impl UnsafeUnpin for BitmapVec
impl UnwindSafe for BitmapVec
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<S, T> IntoSafeCast<T> for Swhere
T: FromSafeCast<S>,
impl<S, T> IntoSafeCast<T> for Swhere
T: FromSafeCast<S>,
Source§fn into_safe_cast(self) -> T
fn into_safe_cast(self) -> T
self into a T. This operation is guaranteed to be lossless.Source§impl<S, T> IntoSafeCastArch<T> for Swhere
T: FromSafeCastArch<S>,
impl<S, T> IntoSafeCastArch<T> for Swhere
T: FromSafeCastArch<S>,
Source§fn into_safe_cast_arch(self) -> T
fn into_safe_cast_arch(self) -> T
self into a T. This operation is guaranteed to be lossless.