rust: alloc: add Vec::zeroed method

Add a constructor for kernel Vec that allocates a vector of a given length
with all elements zero-initialized. Memory is allocated with the __GFP_ZERO
flag, matching the existing KBox::zeroed() pattern.

Signed-off-by: Timur Tabi <ttabi@nvidia.com>
Reviewed-by: Alexandre Courbot <acourbot@nvidia.com>
Tested-by: Alexandre Courbot <acourbot@nvidia.com>
Link: https://patch.msgid.link/20260731201017.2580713-2-ttabi@nvidia.com
Co-developed-by: Danilo Krummrich <dakr@kernel.org>
Signed-off-by: Danilo Krummrich <dakr@kernel.org>
This commit is contained in:
Timur Tabi 2026-07-31 15:10:10 -05:00 committed by Danilo Krummrich
parent 528aef3a4b
commit 60782beb11

View File

@ -9,6 +9,7 @@
Vmalloc,
VmallocPageIter, //
},
flags::__GFP_ZERO,
layout::ArrayLayout,
AllocError,
Allocator,
@ -51,6 +52,8 @@
}, //
};
use pin_init::Zeroable;
mod errors;
pub use self::errors::{InsertError, PushError, RemoveError};
@ -532,6 +535,30 @@ pub fn with_capacity(capacity: usize, flags: Flags) -> Result<Self, AllocError>
Ok(v)
}
/// Creates a new [`Vec`] with `n` zero-initialized elements.
///
/// # Examples
///
/// ```
/// let v = KVec::<u32>::zeroed(20, GFP_KERNEL)?;
///
/// assert!(v.iter().all(|&x| x == 0));
/// # Ok::<(), Error>(())
/// ```
pub fn zeroed(n: usize, flags: Flags) -> Result<Self, AllocError>
where
T: Zeroable,
{
let mut v = Self::with_capacity(n, flags | __GFP_ZERO)?;
// SAFETY:
// - `n <= capacity - len`: `with_capacity(n)` guarantees capacity >= n, len is 0.
// - All elements in `[0, n)` are initialized: `__GFP_ZERO` zeroes the allocation,
// and `T: Zeroable` guarantees all-zeroes is a valid bit pattern.
unsafe { v.inc_len(n) };
Ok(v)
}
/// Creates a `Vec<T, A>` from a pointer, a length and a capacity using the allocator `A`.
///
/// # Examples