Skip to main content

BitmapVec

Struct BitmapVec 

Source
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

  • nbits is <= MAX_LEN.
  • if nbits <= MAX_INLINE_LEN, then repr is a usize.
  • otherwise, repr holds a non-null pointer to an initialized array of unsigned long that is large enough to hold nbits bits.

Implementations§

Source§

impl BitmapVec

Source

pub const MAX_LEN: usize

The maximum possible length of a BitmapVec.

Source

pub const MAX_INLINE_LEN: usize

The maximum length that uses the inline representation.

Source

pub fn new_inline() -> Self

Construct a longest possible inline BitmapVec.

Source

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.

Source

pub fn len(&self) -> usize

Returns length of this Bitmap.

Source

pub fn fill_random(&mut self)

Fills this Bitmap with random bits.

Methods from Deref<Target = Bitmap>§

Source

pub fn as_ptr(&self) -> *const usize

Returns a raw pointer to the backing Bitmap.

Source

pub fn as_mut_ptr(&mut self) -> *mut usize

Returns a mutable raw pointer to the backing Bitmap.

Source

pub fn len(&self) -> usize

Returns length of this Bitmap.

Source

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.

Source

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().

Source

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().

Source

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().

Source

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());
Source

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");
    }
}
Source

pub fn next_bit(&self, start: usize) -> Option<usize>

Finds next set bit, starting from start.

Returns None if no bits are set on or after the given index. The index start must be in bounds.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and start is out of bounds.

Source

pub fn next_zero_bit(&self, start: usize) -> Option<usize>

Finds next zero bit, starting from start.

Returns None if no bits are zero on or after the given index. The index start must be in bounds.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and start is out of bounds.

Source

pub fn next_zero_area_off( &self, start: usize, nbits: NonZero<usize>, align: Alignment, align_offset: usize, ) -> Option<usize>

Finds a contiguous area of nbits zero bits at or after start, where the area plus align_offset is aligned to align.

Returns the bit index of the start of the area, or None if no such area fitting in the bitmap exists.

The returned index plus align_offset is a multiple of align.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and start is out of bounds.

Source

pub fn next_zero_area( &self, start: usize, nbits: NonZero<usize>, align: Alignment, ) -> Option<usize>

Finds a contiguous area of nbits zero bits at or after start, aligned to align.

Returns the bit index of the start of the area, or None if no such area fitting in the bitmap exists.

The returned index is a multiple of align.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and start is out of bounds.

§Examples
use kernel::{
    alloc::{AllocError, flags::GFP_KERNEL},
    bitmap::BitmapVec,
    ptr::Alignment,
    sizes::SizeConstants, //
};

let mut b = BitmapVec::new(64, GFP_KERNEL)?;

assert_eq!(Some(0), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
b.set(0, cv!(5));
assert_eq!(Some(5), b.next_zero_area(0, cv!(8), Alignment::SZ_1));
assert_eq!(Some(8), b.next_zero_area(0, cv!(8), Alignment::SZ_8));
assert_eq!(None, b.next_zero_area(0, cv!(65), Alignment::SZ_1));
Source

pub fn set(&mut self, start: usize, nbits: NonZero<usize>)

Sets a contiguous area of nbits bits starting at start.

If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area start..start + nbits is out of bounds, does nothing.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area start..start + nbits is out of bounds.

Source

pub fn clear(&mut self, start: usize, nbits: NonZero<usize>)

Clears a contiguous area of nbits bits starting at start.

If CONFIG_RUST_BITMAP_HARDENED is not enabled and the area start..start + nbits is out of bounds, does nothing.

§Panics

Panics if CONFIG_RUST_BITMAP_HARDENED is enabled and the area start..start + nbits is out of bounds.

Trait Implementations§

Source§

impl Deref for BitmapVec

Source§

type Target = Bitmap

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Bitmap

Dereferences the value.
Source§

impl DerefMut for BitmapVec

Source§

fn deref_mut(&mut self) -> &mut Bitmap

Mutably dereferences the value.
Source§

impl Drop for BitmapVec

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for BitmapVec

Enable ownership transfer to other threads.

SAFETY: We own the underlying bitmap representation.

Source§

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§

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Init<T> for T

Source§

fn chain<F>(self, f: F) -> ChainInit<Self, F, T, E>
where F: FnOnce(&mut T) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
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<S, T> IntoSafeCast<T> for S
where T: FromSafeCast<S>,

Source§

fn into_safe_cast(self) -> T

Convert self into a T. This operation is guaranteed to be lossless.
Source§

impl<S, T> IntoSafeCastArch<T> for S
where T: FromSafeCastArch<S>,

Source§

fn into_safe_cast_arch(self) -> T

Convert self into a T. This operation is guaranteed to be lossless.
Source§

impl<T> KnownSize for T

Source§

const MIN_SIZE: usize = const MIN_SIZE: usize = size_of::<T>();

Minimum size of this type known at compile-time.
Source§

const MIN_ALIGN: Alignment = const MIN_ALIGN: Alignment = Alignment::of::<T>();

Minimum alignment of this type known at compile-time.
Source§

fn size(_: *const T) -> usize

Get the size of an object of this type in bytes, with the metadata of the given pointer.
Source§

impl<T> PinInit<T> for T

Source§

unsafe fn __init(self, slot: *mut T) -> Result<(), !>

Initializes slot. Read more
Source§

fn pin_chain<F>(self, f: F) -> ChainPinInit<Self, F, T, E>
where F: FnOnce(Pin<&mut T>) -> Result<(), E>,

First initializes the value using self then calls the function f with the initialized value. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<T, U, const N: u32> TryIntoBounded<T, N> for U
where T: Integer, U: TryInto<T>,

Source§

fn try_into_bounded(self) -> Option<Bounded<T, N>>

Attempts to convert self into a Bounded using N bits. Read more