rust: alloc: introduce the BoxExt trait

Make fallible versions of `new` and `new_uninit` methods available in
`Box` even though it doesn't implement them because we build `alloc`
with the `no_global_oom_handling` config.

They also have an extra `flags` parameter that allows callers to pass
flags to the allocator.

Signed-off-by: Wedson Almeida Filho <walmeida@microsoft.com>
Reviewed-by: Boqun Feng <boqun.feng@gmail.com>
Reviewed-by: Benno Lossin <benno.lossin@proton.me>
Link: https://lore.kernel.org/r/20240328013603.206764-7-wedsonaf@gmail.com
[ Used `Box::write()` to avoid one `unsafe` block as suggested by Boqun. ]
Signed-off-by: Miguel Ojeda <ojeda@kernel.org>
This commit is contained in:
Wedson Almeida Filho
2024-03-27 22:35:59 -03:00
committed by Miguel Ojeda
parent b6a006e21b
commit 08d3f54928
6 changed files with 70 additions and 9 deletions
+2 -1
View File
@@ -16,6 +16,7 @@
//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
use crate::{
alloc::{box_ext::BoxExt, flags::*},
bindings,
error::{self, Error},
init::{self, InPlaceInit, Init, PinInit},
@@ -170,7 +171,7 @@ impl<T> Arc<T> {
data: contents,
};
let inner = Box::try_new(value)?;
let inner = <Box<_> as BoxExt<_>>::new(value, GFP_KERNEL)?;
// SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
// `Arc` object.