Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit a419846

Browse files
y86-devojeda
authored andcommittedAug 21, 2024
rust: init: add write_[pin_]init functions
Sometimes it is necessary to split allocation and initialization into two steps. One such situation is when reusing existing allocations obtained via `Box::drop_contents`. See [1] for an example. In order to support this use case add `write_[pin_]init` functions to the pin-init API. These functions operate on already allocated smart pointers that wrap `MaybeUninit<T>`. Signed-off-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/rust-for-linux/f026532f-8594-4f18-9aa5-57ad3f5bc592@proton.me/ [1] Reviewed-by: Boqun Feng <boqun.feng@gmail.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Gary Guo <gary@garyguo.net> Link: https://lore.kernel.org/r/20240819112415.99810-2-benno.lossin@proton.me Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
1 parent 767af3a commit a419846

File tree

2 files changed

+61
-25
lines changed

2 files changed

+61
-25
lines changed
 

‎rust/kernel/init.rs

+60-24
Original file line numberDiff line numberDiff line change
@@ -1183,27 +1183,15 @@ impl<T> InPlaceInit<T> for Box<T> {
11831183
where
11841184
E: From<AllocError>,
11851185
{
1186-
let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
1187-
let slot = this.as_mut_ptr();
1188-
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1189-
// slot is valid and will not be moved, because we pin it later.
1190-
unsafe { init.__pinned_init(slot)? };
1191-
// SAFETY: All fields have been initialized.
1192-
Ok(unsafe { this.assume_init() }.into())
1186+
<Box<_> as BoxExt<_>>::new_uninit(flags)?.write_pin_init(init)
11931187
}
11941188

11951189
#[inline]
11961190
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
11971191
where
11981192
E: From<AllocError>,
11991193
{
1200-
let mut this = <Box<_> as BoxExt<_>>::new_uninit(flags)?;
1201-
let slot = this.as_mut_ptr();
1202-
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1203-
// slot is valid.
1204-
unsafe { init.__init(slot)? };
1205-
// SAFETY: All fields have been initialized.
1206-
Ok(unsafe { this.assume_init() })
1194+
<Box<_> as BoxExt<_>>::new_uninit(flags)?.write_init(init)
12071195
}
12081196
}
12091197

@@ -1215,27 +1203,75 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
12151203
where
12161204
E: From<AllocError>,
12171205
{
1218-
let mut this = UniqueArc::new_uninit(flags)?;
1219-
let slot = this.as_mut_ptr();
1220-
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1221-
// slot is valid and will not be moved, because we pin it later.
1222-
unsafe { init.__pinned_init(slot)? };
1223-
// SAFETY: All fields have been initialized.
1224-
Ok(unsafe { this.assume_init() }.into())
1206+
UniqueArc::new_uninit(flags)?.write_pin_init(init)
12251207
}
12261208

12271209
#[inline]
12281210
fn try_init<E>(init: impl Init<T, E>, flags: Flags) -> Result<Self, E>
12291211
where
12301212
E: From<AllocError>,
12311213
{
1232-
let mut this = UniqueArc::new_uninit(flags)?;
1233-
let slot = this.as_mut_ptr();
1214+
UniqueArc::new_uninit(flags)?.write_init(init)
1215+
}
1216+
}
1217+
1218+
/// Smart pointer containing uninitialized memory and that can write a value.
1219+
pub trait InPlaceWrite<T> {
1220+
/// The type `Self` turns into when the contents are initialized.
1221+
type Initialized;
1222+
1223+
/// Use the given initializer to write a value into `self`.
1224+
///
1225+
/// Does not drop the current value and considers it as uninitialized memory.
1226+
fn write_init<E>(self, init: impl Init<T, E>) -> Result<Self::Initialized, E>;
1227+
1228+
/// Use the given pin-initializer to write a value into `self`.
1229+
///
1230+
/// Does not drop the current value and considers it as uninitialized memory.
1231+
fn write_pin_init<E>(self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E>;
1232+
}
1233+
1234+
impl<T> InPlaceWrite<T> for Box<MaybeUninit<T>> {
1235+
type Initialized = Box<T>;
1236+
1237+
fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
1238+
let slot = self.as_mut_ptr();
12341239
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12351240
// slot is valid.
12361241
unsafe { init.__init(slot)? };
12371242
// SAFETY: All fields have been initialized.
1238-
Ok(unsafe { this.assume_init() })
1243+
Ok(unsafe { self.assume_init() })
1244+
}
1245+
1246+
fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
1247+
let slot = self.as_mut_ptr();
1248+
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1249+
// slot is valid and will not be moved, because we pin it later.
1250+
unsafe { init.__pinned_init(slot)? };
1251+
// SAFETY: All fields have been initialized.
1252+
Ok(unsafe { self.assume_init() }.into())
1253+
}
1254+
}
1255+
1256+
impl<T> InPlaceWrite<T> for UniqueArc<MaybeUninit<T>> {
1257+
type Initialized = UniqueArc<T>;
1258+
1259+
fn write_init<E>(mut self, init: impl Init<T, E>) -> Result<Self::Initialized, E> {
1260+
let slot = self.as_mut_ptr();
1261+
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1262+
// slot is valid.
1263+
unsafe { init.__init(slot)? };
1264+
// SAFETY: All fields have been initialized.
1265+
Ok(unsafe { self.assume_init() })
1266+
}
1267+
1268+
fn write_pin_init<E>(mut self, init: impl PinInit<T, E>) -> Result<Pin<Self::Initialized>, E> {
1269+
let slot = self.as_mut_ptr();
1270+
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
1271+
// slot is valid and will not be moved, because we pin it later.
1272+
unsafe { init.__pinned_init(slot)? };
1273+
// SAFETY: All fields have been initialized.
1274+
Ok(unsafe { self.assume_init() }.into())
12391275
}
12401276
}
12411277

‎rust/kernel/prelude.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ pub use super::error::{code::*, Error, Result};
3737

3838
pub use super::{str::CStr, ThisModule};
3939

40-
pub use super::init::{InPlaceInit, Init, PinInit};
40+
pub use super::init::{InPlaceInit, InPlaceWrite, Init, PinInit};
4141

4242
pub use super::current;

0 commit comments

Comments
 (0)
Failed to load comments.