[PATCH v2 1/2] rust: bitmap: add BitArray
From: Vitaly Wool
Date: Fri Dec 04 2026 - 05:05:25 EST
A `BitmapVec` keeps up to `usize::BITS` bits inline and allocates its
storage with `bitmap_zalloc()` beyond that. A bitmap embedded in a
structure that is allocated anyway, with a length known at compile
time, then needs a second allocation and a pointer to follow, or
`unsafe` code that wraps an array with `Bitmap::from_raw()`.
Add `BitArray`, an owned bitmap stored inline, which never allocates
and dereferences to `Bitmap` like `BitmapVec`. Like `DECLARE_BITMAP()`
in C, it is declared by its number of bits, with `bit_array!(nbits)`,
and the number of words that store them is computed with
`bits_to_longs()`, the equivalent of `BITS_TO_LONGS()`, so users don't
depend on the size of `usize`.
The type has the number of words as a second const parameter because
computing it from the first needs `generic_const_exprs`; `new()` checks
at compile time that it matches the number of bits.
Signed-off-by: Vitaly Wool <vitaly.wool@xxxxxxxxxxx>
Tested-by: Igor Belousov <igor.b@xxxxxxxxx>
Assisted-By: Claude Opus 5.5 <noreply@xxxxxxxxxxxxx>
---
rust/kernel/bitmap.rs | 129 ++++++++++++++++++++++++++++++++++++++++++
1 file changed, 129 insertions(+)
diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index 610d565535cc..35b1f542830d 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -320,6 +320,135 @@ pub fn fill_random(&mut self) {
}
}
+/// Returns the number of `usize` words needed to hold `nbits` bits.
+///
+/// This is the Rust equivalent of the C `BITS_TO_LONGS()` macro.
+#[inline]
+pub const fn bits_to_longs(nbits: usize) -> usize {
+ nbits.div_ceil(usize::BITS as usize)
+}
+
+/// Declares the type of a [`BitArray`] of `$nbits` bits.
+///
+/// This is the Rust equivalent of the C `DECLARE_BITMAP()` macro: the bitmap is declared by its
+/// number of bits, and the number of words that store them is computed with [`bits_to_longs`].
+///
+/// # Examples
+///
+/// ```
+/// use kernel::bitmap::{bit_array, BitArray};
+///
+/// struct Foo {
+/// map: bit_array!(100),
+/// }
+///
+/// let foo = Foo {
+/// map: BitArray::new(),
+/// };
+/// assert_eq!(100, foo.map.len());
+/// ```
+#[macro_export]
+macro_rules! bit_array {
+ ($nbits:expr) => {
+ $crate::bitmap::BitArray<{ $nbits }, { $crate::bitmap::bits_to_longs($nbits) }>
+ };
+}
+pub use crate::bit_array;
+
+/// An owned bitmap of `NBITS` bits, stored inline in `LONGS` words.
+///
+/// Unlike [`BitmapVec`], it never allocates, so it suits bitmaps whose length is known at compile
+/// time and that are embedded in other structures. See [`Bitmap`] for available methods.
+///
+/// Declare it with [`bit_array!`], which takes the number of bits only and computes `LONGS`,
+/// rather than by naming `LONGS`, which depends on the size of `usize`.
+///
+/// # Examples
+///
+/// ```
+/// use kernel::bitmap::{bit_array, BitArray};
+///
+/// let mut b: bit_array!(100) = BitArray::new();
+///
+/// assert_eq!(100, b.len());
+/// b.set_bit(3);
+/// b.set_bit(99);
+/// assert_eq!(Some(3), b.next_bit(0));
+/// assert_eq!(Some(99), b.next_bit(4));
+/// assert_eq!(Some(99), b.last_bit());
+///
+/// b.clear_all();
+/// assert_eq!(None, b.next_bit(0));
+/// ```
+///
+/// # Invariants
+///
+/// `LONGS` is `bits_to_longs(NBITS)` and `NBITS` is at most [`BitmapVec::MAX_LEN`]; both are
+/// checked at compile time by [`BitArray::new`].
+pub struct BitArray<const NBITS: usize, const LONGS: usize> {
+ /// Storage of the bitmap.
+ words: [usize; LONGS],
+}
+
+impl<const NBITS: usize, const LONGS: usize> BitArray<NBITS, LONGS> {
+ /// Constructs a new [`BitArray`] with all bits clear.
+ ///
+ /// Fails to build if `LONGS` is not `bits_to_longs(NBITS)`, or if `NBITS` is greater than
+ /// [`BitmapVec::MAX_LEN`].
+ #[inline]
+ pub const fn new() -> Self {
+ const {
+ assert!(
+ LONGS == bits_to_longs(NBITS),
+ "LONGS must be bits_to_longs(NBITS); use bit_array!()"
+ );
+ assert!(NBITS <= BitmapVec::MAX_LEN, "NBITS is greater than MAX_LEN");
+ }
+ // INVARIANT: Checked above.
+ Self { words: [0; LONGS] }
+ }
+
+ /// Returns length of this [`BitArray`].
+ #[allow(clippy::len_without_is_empty)]
+ #[inline]
+ pub const fn len(&self) -> usize {
+ NBITS
+ }
+
+ /// Clears all bits of this [`BitArray`].
+ #[inline]
+ pub fn clear_all(&mut self) {
+ self.words = [0; LONGS];
+ }
+}
+
+impl<const NBITS: usize, const LONGS: usize> Default for BitArray<NBITS, LONGS> {
+ #[inline]
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl<const NBITS: usize, const LONGS: usize> core::ops::Deref for BitArray<NBITS, LONGS> {
+ type Target = Bitmap;
+
+ #[inline]
+ fn deref(&self) -> &Bitmap {
+ // SAFETY: `self.words` is an initialized array of `bits_to_longs(NBITS)` words, so it
+ // holds `NBITS` bits, `NBITS <= MAX_LEN` by the type invariants, and the array lives as
+ // long as the returned shared borrow of `self`.
+ unsafe { Bitmap::from_raw(self.words.as_ptr(), NBITS) }
+ }
+}
+
+impl<const NBITS: usize, const LONGS: usize> core::ops::DerefMut for BitArray<NBITS, LONGS> {
+ #[inline]
+ fn deref_mut(&mut self) -> &mut Bitmap {
+ // SAFETY: As in `deref`, and the returned borrow of `self` is exclusive.
+ unsafe { Bitmap::from_raw_mut(self.words.as_mut_ptr(), NBITS) }
+ }
+}
+
impl Bitmap {
/// Set bit with index `index`.
///
--
2.39.5