Skip to content

Commit 5ccd2a6

Browse files
wedsonaffbq
authored andcommitted
rust: sync: update Arc and UniqueArc to take allocation flags
We also remove the `try_` prefix to align with how `Box` and `Vec` are providing methods now. `init` is temporarily updated with uses of GFP_KERNEL. These will be updated in a subsequent patch to take flags as well. Reviewed-by: Benno Lossin <[email protected]> Signed-off-by: Wedson Almeida Filho <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 671977d commit 5ccd2a6

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

rust/kernel/init.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,7 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
11891189
where
11901190
E: From<AllocError>,
11911191
{
1192-
let mut this = UniqueArc::try_new_uninit()?;
1192+
let mut this = UniqueArc::new_uninit(GFP_KERNEL)?;
11931193
let slot = this.as_mut_ptr();
11941194
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
11951195
// slot is valid and will not be moved, because we pin it later.
@@ -1203,7 +1203,7 @@ impl<T> InPlaceInit<T> for UniqueArc<T> {
12031203
where
12041204
E: From<AllocError>,
12051205
{
1206-
let mut this = UniqueArc::try_new_uninit()?;
1206+
let mut this = UniqueArc::new_uninit(GFP_KERNEL)?;
12071207
let slot = this.as_mut_ptr();
12081208
// SAFETY: When init errors/panics, slot will get deallocated but not dropped,
12091209
// slot is valid.

rust/kernel/sync/arc.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
//! [`Arc`]: https://doc.rust-lang.org/std/sync/struct.Arc.html
1717
1818
use crate::{
19-
alloc::{box_ext::BoxExt, flags::*},
19+
alloc::{box_ext::BoxExt, Flags},
2020
bindings,
2121
error::{self, Error},
2222
init::{self, InPlaceInit, Init, PinInit},
@@ -58,7 +58,7 @@ mod std_vendor;
5858
/// }
5959
///
6060
/// // Create a refcounted instance of `Example`.
61-
/// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
61+
/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
6262
///
6363
/// // Get a new pointer to `obj` and increment the refcount.
6464
/// let cloned = obj.clone();
@@ -97,7 +97,7 @@ mod std_vendor;
9797
/// }
9898
/// }
9999
///
100-
/// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
100+
/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
101101
/// obj.use_reference();
102102
/// obj.take_over();
103103
/// # Ok::<(), Error>(())
@@ -120,7 +120,7 @@ mod std_vendor;
120120
/// impl MyTrait for Example {}
121121
///
122122
/// // `obj` has type `Arc<Example>`.
123-
/// let obj: Arc<Example> = Arc::try_new(Example)?;
123+
/// let obj: Arc<Example> = Arc::new(Example, GFP_KERNEL)?;
124124
///
125125
/// // `coerced` has type `Arc<dyn MyTrait>`.
126126
/// let coerced: Arc<dyn MyTrait> = obj;
@@ -163,15 +163,15 @@ unsafe impl<T: ?Sized + Sync + Send> Sync for Arc<T> {}
163163

164164
impl<T> Arc<T> {
165165
/// Constructs a new reference counted instance of `T`.
166-
pub fn try_new(contents: T) -> Result<Self, AllocError> {
166+
pub fn new(contents: T, flags: Flags) -> Result<Self, AllocError> {
167167
// INVARIANT: The refcount is initialised to a non-zero value.
168168
let value = ArcInner {
169169
// SAFETY: There are no safety requirements for this FFI call.
170170
refcount: Opaque::new(unsafe { bindings::REFCOUNT_INIT(1) }),
171171
data: contents,
172172
};
173173

174-
let inner = <Box<_> as BoxExt<_>>::new(value, GFP_KERNEL)?;
174+
let inner = <Box<_> as BoxExt<_>>::new(value, flags)?;
175175

176176
// SAFETY: We just created `inner` with a reference count of 1, which is owned by the new
177177
// `Arc` object.
@@ -388,7 +388,7 @@ impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
388388
/// e.into()
389389
/// }
390390
///
391-
/// let obj = Arc::try_new(Example)?;
391+
/// let obj = Arc::new(Example, GFP_KERNEL)?;
392392
/// let cloned = do_something(obj.as_arc_borrow());
393393
///
394394
/// // Assert that both `obj` and `cloned` point to the same underlying object.
@@ -412,7 +412,7 @@ impl<T: ?Sized> From<Pin<UniqueArc<T>>> for Arc<T> {
412412
/// }
413413
/// }
414414
///
415-
/// let obj = Arc::try_new(Example { a: 10, b: 20 })?;
415+
/// let obj = Arc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
416416
/// obj.as_arc_borrow().use_reference();
417417
/// # Ok::<(), Error>(())
418418
/// ```
@@ -500,7 +500,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
500500
/// }
501501
///
502502
/// fn test() -> Result<Arc<Example>> {
503-
/// let mut x = UniqueArc::try_new(Example { a: 10, b: 20 })?;
503+
/// let mut x = UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?;
504504
/// x.a += 1;
505505
/// x.b += 1;
506506
/// Ok(x.into())
@@ -523,7 +523,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
523523
/// }
524524
///
525525
/// fn test() -> Result<Arc<Example>> {
526-
/// let x = UniqueArc::try_new_uninit()?;
526+
/// let x = UniqueArc::new_uninit(GFP_KERNEL)?;
527527
/// Ok(x.write(Example { a: 10, b: 20 }).into())
528528
/// }
529529
///
@@ -543,7 +543,7 @@ impl<T: ?Sized> Deref for ArcBorrow<'_, T> {
543543
/// }
544544
///
545545
/// fn test() -> Result<Arc<Example>> {
546-
/// let mut pinned = Pin::from(UniqueArc::try_new(Example { a: 10, b: 20 })?);
546+
/// let mut pinned = Pin::from(UniqueArc::new(Example { a: 10, b: 20 }, GFP_KERNEL)?);
547547
/// // We can modify `pinned` because it is `Unpin`.
548548
/// pinned.as_mut().a += 1;
549549
/// Ok(pinned.into())
@@ -557,15 +557,15 @@ pub struct UniqueArc<T: ?Sized> {
557557

558558
impl<T> UniqueArc<T> {
559559
/// Tries to allocate a new [`UniqueArc`] instance.
560-
pub fn try_new(value: T) -> Result<Self, AllocError> {
560+
pub fn new(value: T, flags: Flags) -> Result<Self, AllocError> {
561561
Ok(Self {
562562
// INVARIANT: The newly-created object has a refcount of 1.
563-
inner: Arc::try_new(value)?,
563+
inner: Arc::new(value, flags)?,
564564
})
565565
}
566566

567567
/// Tries to allocate a new [`UniqueArc`] instance whose contents are not initialised yet.
568-
pub fn try_new_uninit() -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
568+
pub fn new_uninit(_flags: Flags) -> Result<UniqueArc<MaybeUninit<T>>, AllocError> {
569569
// INVARIANT: The refcount is initialised to a non-zero value.
570570
let inner = Box::try_init::<AllocError>(try_init!(ArcInner {
571571
// SAFETY: There are no safety requirements for this FFI call.

samples/rust/rust_print.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ struct RustPrint;
1818
fn arc_print() -> Result {
1919
use kernel::sync::*;
2020

21-
let a = Arc::try_new(1)?;
22-
let b = UniqueArc::try_new("hello, world")?;
21+
let a = Arc::new(1, GFP_KERNEL)?;
22+
let b = UniqueArc::new("hello, world", GFP_KERNEL)?;
2323

2424
// Prints the value of data in `a`.
2525
pr_info!("{}", a);

0 commit comments

Comments
 (0)