summaryrefslogtreecommitdiff
path: root/rust/kernel/alloc
diff options
context:
space:
mode:
Diffstat (limited to 'rust/kernel/alloc')
-rw-r--r--rust/kernel/alloc/kbox.rs8
-rw-r--r--rust/kernel/alloc/kvec.rs27
2 files changed, 31 insertions, 4 deletions
diff --git a/rust/kernel/alloc/kbox.rs b/rust/kernel/alloc/kbox.rs
index 35d1e015848d..c63d6acdbb6f 100644
--- a/rust/kernel/alloc/kbox.rs
+++ b/rust/kernel/alloc/kbox.rs
@@ -372,13 +372,13 @@ where
// - `ptr` is a valid pointer to uninitialized memory.
// - `ptr` is not used if an error is returned.
// - `ptr` won't be moved until it is dropped, i.e. it is pinned.
- unsafe { init(i).__pinned_init(ptr)? };
+ unsafe { pin_init::raw_try_init(ptr, init(i))? };
// SAFETY:
// - `i + 1 <= len`, hence we don't exceed the capacity, due to the call to
// `with_capacity()` above.
// - The new value at index buffer.len() + 1 is the only element being added here, and
- // it has been initialized above by `init(i).__pinned_init(ptr)`.
+ // it has been initialized above by `raw_try_init(ptr, i)`.
unsafe { buffer.inc_len(1) };
}
@@ -463,7 +463,7 @@ where
let slot = self.as_mut_ptr();
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
// slot is valid.
- unsafe { init.__init(slot)? };
+ unsafe { pin_init::raw_try_init(slot, init)? };
// SAFETY: All fields have been initialized.
Ok(unsafe { Box::assume_init(self) })
}
@@ -473,7 +473,7 @@ where
let slot = self.as_mut_ptr();
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
// slot is valid and will not be moved, because we pin it later.
- unsafe { init.__pinned_init(slot)? };
+ unsafe { pin_init::raw_try_init(slot, init)? };
// SAFETY: All fields have been initialized.
Ok(unsafe { Box::assume_init(self) }.into())
}
diff --git a/rust/kernel/alloc/kvec.rs b/rust/kernel/alloc/kvec.rs
index f7af62835aa8..c7546b9da4fa 100644
--- a/rust/kernel/alloc/kvec.rs
+++ b/rust/kernel/alloc/kvec.rs
@@ -9,6 +9,7 @@ use super::{
Vmalloc,
VmallocPageIter, //
},
+ flags::__GFP_ZERO,
layout::ArrayLayout,
AllocError,
Allocator,
@@ -51,6 +52,8 @@ use core::{
}, //
};
+use pin_init::Zeroable;
+
mod errors;
pub use self::errors::{InsertError, PushError, RemoveError};
@@ -532,6 +535,30 @@ where
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