Struct Bitmap

Source
#[repr(align(8))]
pub struct Bitmap { /* private fields */ }
Expand description

Represents a C bitmap. Wraps underlying C bitmap API.

§Invariants

Must reference a [c_ulong] long enough to fit data.len() bits.

Implementations§

Source§

impl Bitmap

Source

pub unsafe fn from_raw<'a>(ptr: *const usize, nbits: usize) -> &'a Bitmap

Borrows a C bitmap.

§Safety
  • ptr holds a non-null address of an initialized array of unsigned long that is large enough to hold nbits bits.
  • the array must not be freed for the lifetime of this Bitmap
  • concurrent access only happens through atomic operations
Source

pub unsafe fn from_raw_mut<'a>(ptr: *mut usize, nbits: usize) -> &'a mut Bitmap

Borrows a C bitmap exclusively.

§Safety
  • ptr holds a non-null address of an initialized array of unsigned long that is large enough to hold nbits bits.
  • the array must not be freed for the lifetime of this Bitmap
  • no concurrent access may happen.
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§

impl 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 start is greater or equal to self.nbits.

Source

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

Finds next zero bit, starting from start. Returns None if start is greater than or equal to self.len().

Auto Trait Implementations§

§

impl Freeze for Bitmap

§

impl RefUnwindSafe for Bitmap

§

impl Send for Bitmap

§

impl !Sized for Bitmap

§

impl Sync for Bitmap

§

impl Unpin for Bitmap

§

impl UnwindSafe for Bitmap

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