[PATCH v2 2/5] rust: bitmap: add BitmapVec::new_small()

Alice Ryhl posted 5 patches 3 months, 2 weeks ago
There is a newer version of this series
[PATCH v2 2/5] rust: bitmap: add BitmapVec::new_small()
Posted by Alice Ryhl 3 months, 2 weeks ago
This constructor is useful when you just want to create a BitmapVec
without allocating but don't care how large it is.

Signed-off-by: Alice Ryhl <aliceryhl@google.com>
---
 rust/kernel/bitmap.rs | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
index 15fa23b45054b9272415fcc000e3e3b52c74d7c1..4ffe9eb0f208a3d62016e00297f5a0800aa33336 100644
--- a/rust/kernel/bitmap.rs
+++ b/rust/kernel/bitmap.rs
@@ -232,6 +232,16 @@ impl BitmapVec {
     /// The maximum length that avoids allocating.
     pub const NO_ALLOC_MAX_LEN: usize = BITS_PER_LONG;
 
+    /// Constructs a new [`BitmapVec`] without allocating.
+    #[inline]
+    pub fn new_small() -> Self {
+        // INVARIANT: `nbits <= NO_ALLOC_MAX_LEN`, so an inline bitmap is the right repr.
+        BitmapVec {
+            repr: BitmapRepr { bitmap: 0 },
+            nbits: BitmapVec::NO_ALLOC_MAX_LEN,
+        }
+    }
+
     /// Constructs a new [`BitmapVec`].
     ///
     /// Fails with [`AllocError`] when the [`BitmapVec`] could not be allocated. This

-- 
2.51.0.869.ge66316f041-goog
Re: [PATCH v2 2/5] rust: bitmap: add BitmapVec::new_small()
Posted by Yury Norov 3 months, 2 weeks ago
On Tue, Oct 21, 2025 at 01:32:44PM +0000, Alice Ryhl wrote:
> This constructor is useful when you just want to create a BitmapVec
> without allocating but don't care how large it is.
> 
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> ---
>  rust/kernel/bitmap.rs | 10 ++++++++++
>  1 file changed, 10 insertions(+)
> 
> diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
> index 15fa23b45054b9272415fcc000e3e3b52c74d7c1..4ffe9eb0f208a3d62016e00297f5a0800aa33336 100644
> --- a/rust/kernel/bitmap.rs
> +++ b/rust/kernel/bitmap.rs
> @@ -232,6 +232,16 @@ impl BitmapVec {
>      /// The maximum length that avoids allocating.
>      pub const NO_ALLOC_MAX_LEN: usize = BITS_PER_LONG;
>  
> +    /// Constructs a new [`BitmapVec`] without allocating.
> +    #[inline]
> +    pub fn new_small() -> Self {

Nit: maybe:

        /// Construct a longest possible inline [`BitmapVec`].
        #[inline]
        pub fn new_inline() ...

This 'small vs large' lingo is internal to bitmaps. I don't think it
is worth to expose it in the interfaces. 'Inline' or 'inplace' sounds
better to me.

With that,

Acked-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>

> +        // INVARIANT: `nbits <= NO_ALLOC_MAX_LEN`, so an inline bitmap is the right repr.
> +        BitmapVec {
> +            repr: BitmapRepr { bitmap: 0 },
> +            nbits: BitmapVec::NO_ALLOC_MAX_LEN,

A side note: after merging bitfields, we may switch inline bitmaps to to

        bitfield!() {
                0:31    nbits;
                32:64   bitmap;
        }

Thanks,
Yury

> +        }
> +    }
> +
>      /// Constructs a new [`BitmapVec`].
>      ///
>      /// Fails with [`AllocError`] when the [`BitmapVec`] could not be allocated. This
> 
> -- 
> 2.51.0.869.ge66316f041-goog
Re: [PATCH v2 2/5] rust: bitmap: add BitmapVec::new_small()
Posted by Alice Ryhl 3 months, 2 weeks ago
On Thu, Oct 23, 2025 at 01:33:03PM -0400, Yury Norov wrote:
> On Tue, Oct 21, 2025 at 01:32:44PM +0000, Alice Ryhl wrote:
> > This constructor is useful when you just want to create a BitmapVec
> > without allocating but don't care how large it is.
> > 
> > Signed-off-by: Alice Ryhl <aliceryhl@google.com>
> > ---
> >  rust/kernel/bitmap.rs | 10 ++++++++++
> >  1 file changed, 10 insertions(+)
> > 
> > diff --git a/rust/kernel/bitmap.rs b/rust/kernel/bitmap.rs
> > index 15fa23b45054b9272415fcc000e3e3b52c74d7c1..4ffe9eb0f208a3d62016e00297f5a0800aa33336 100644
> > --- a/rust/kernel/bitmap.rs
> > +++ b/rust/kernel/bitmap.rs
> > @@ -232,6 +232,16 @@ impl BitmapVec {
> >      /// The maximum length that avoids allocating.
> >      pub const NO_ALLOC_MAX_LEN: usize = BITS_PER_LONG;
> >  
> > +    /// Constructs a new [`BitmapVec`] without allocating.
> > +    #[inline]
> > +    pub fn new_small() -> Self {
> 
> Nit: maybe:
> 
>         /// Construct a longest possible inline [`BitmapVec`].
>         #[inline]
>         pub fn new_inline() ...
> 
> This 'small vs large' lingo is internal to bitmaps. I don't think it
> is worth to expose it in the interfaces. 'Inline' or 'inplace' sounds
> better to me.
> 
> With that,
> 
> Acked-by: Yury Norov (NVIDIA) <yury.norov@gmail.com>

Makes sense. Will reword to 'inline', thanks!

> > +        // INVARIANT: `nbits <= NO_ALLOC_MAX_LEN`, so an inline bitmap is the right repr.
> > +        BitmapVec {
> > +            repr: BitmapRepr { bitmap: 0 },
> > +            nbits: BitmapVec::NO_ALLOC_MAX_LEN,
> 
> A side note: after merging bitfields, we may switch inline bitmaps to to
> 
>         bitfield!() {
>                 0:31    nbits;
>                 32:64   bitmap;
>         }

Personally I think I would prefer to keep the union only on the `repr`
field like it is now.

Alice
Re: [PATCH v2 2/5] rust: bitmap: add BitmapVec::new_small()
Posted by Burak Emir 3 months, 2 weeks ago
On Tue, Oct 21, 2025 at 3:33 PM Alice Ryhl <aliceryhl@google.com> wrote:
>
> This constructor is useful when you just want to create a BitmapVec
> without allocating but don't care how large it is.
>
> Signed-off-by: Alice Ryhl <aliceryhl@google.com>
Reviewed-by: Burak Emir <bqe@google.com>