Skip to content

Commit f9ab851

Browse files
wedsonafintel-lab-lkp
authored andcommitted
rust: sync: add Arc::{from_raw, into_raw}
These methods can be used to turn an `Arc` into a raw pointer and back, in a way that preserves the metadata for fat pointers. This is done using the unstable ptr_metadata feature [1]. However, it could also be done using the unstable pointer_byte_offsets feature [2], which is likely to have a shorter path to stabilization than ptr_metadata. Link: rust-lang/rust#81513 [1] Link: rust-lang/rust#96283 [2] Signed-off-by: Wedson Almeida Filho <[email protected]> Co-developed-by: Alice Ryhl <[email protected]> Signed-off-by: Alice Ryhl <[email protected]> Reviewed-by: Martin Rodriguez Reboredo <[email protected]>
1 parent e2c3014 commit f9ab851

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

Diff for: rust/kernel/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#![feature(const_refs_to_cell)]
1818
#![feature(dispatch_from_dyn)]
1919
#![feature(new_uninit)]
20+
#![feature(ptr_metadata)]
2021
#![feature(receiver_trait)]
2122
#![feature(unsize)]
2223

Diff for: rust/kernel/sync/arc.rs

+41-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use crate::{
2424
};
2525
use alloc::boxed::Box;
2626
use core::{
27-
alloc::AllocError,
27+
alloc::{AllocError, Layout},
2828
fmt,
2929
marker::{PhantomData, Unsize},
3030
mem::{ManuallyDrop, MaybeUninit},
@@ -212,6 +212,46 @@ impl<T: ?Sized> Arc<T> {
212212
}
213213
}
214214

215+
/// Convert the [`Arc`] into a raw pointer.
216+
///
217+
/// The raw pointer has ownership of the refcount that this Arc object owned.
218+
pub fn into_raw(self) -> *const T {
219+
let ptr = self.ptr.as_ptr();
220+
core::mem::forget(self);
221+
// SAFETY: The pointer is valid.
222+
unsafe { core::ptr::addr_of!((*ptr).data) }
223+
}
224+
225+
/// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
226+
///
227+
/// This code relies on the `repr(C)` layout of structs as described in
228+
/// <https://doc.rust-lang.org/reference/type-layout.html#reprc-structs>.
229+
///
230+
/// # Safety
231+
///
232+
/// `ptr` must have been returned by a previous call to [`Arc::into_raw`]. Additionally, it
233+
/// can only be called once for each previous call to [`Arc::into_raw`].
234+
pub unsafe fn from_raw(ptr: *const T) -> Self {
235+
let refcount_layout = Layout::new::<bindings::refcount_t>();
236+
// SAFETY: The caller guarantees that the pointer is valid.
237+
let val_layout = unsafe { Layout::for_value(&*ptr) };
238+
// SAFETY: We're computing the layout of a real struct that existed when compiling this
239+
// binary, so its layout is not so large that it can trigger arithmetic overflow.
240+
let val_offset = unsafe { refcount_layout.extend(val_layout).unwrap_unchecked().1 };
241+
242+
// This preserves the metadata in the pointer, if any.
243+
//
244+
// Note that `*const T` and `*const ArcInner<T>` have the same metadata as documented at
245+
// <https://doc.rust-lang.org/std/ptr/trait.Pointee.html>.
246+
let metadata = core::ptr::metadata(ptr as *const ArcInner<T>);
247+
let ptr = (ptr as *mut u8).wrapping_sub(val_offset) as *mut ();
248+
let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
249+
250+
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
251+
// reference count held then will be owned by the new `Arc` object.
252+
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
253+
}
254+
215255
/// Returns an [`ArcBorrow`] from the given [`Arc`].
216256
///
217257
/// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method

0 commit comments

Comments
 (0)