Skip to content

Commit a0c6fa8

Browse files
author
Andreas Hindborg
committed
rust: sync: add Arc::as_ptr
Add a method to get a pointer to the data contained in an `Arc`. Reviewed-by: Lyude Paul <lyude@redhat.com> Reviewed-by: Alice Ryhl <aliceryhl@google.com> Reviewed-by: Benno Lossin <benno.lossin@proton.me> Link: https://lore.kernel.org/r/20250309-hrtimer-v3-v6-12-rc2-v12-2-73586e2bd5f1@kernel.org Signed-off-by: Andreas Hindborg <a.hindborg@kernel.org>
1 parent 8a8afe9 commit a0c6fa8

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

rust/kernel/sync/arc.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,15 @@ impl<T: ?Sized> Arc<T> {
246246
unsafe { core::ptr::addr_of!((*ptr).data) }
247247
}
248248

249+
/// Return a raw pointer to the data in this arc.
250+
pub fn as_ptr(this: &Self) -> *const T {
251+
let ptr = this.ptr.as_ptr();
252+
253+
// SAFETY: As `ptr` points to a valid allocation of type `ArcInner`,
254+
// field projection to `data`is within bounds of the allocation.
255+
unsafe { core::ptr::addr_of!((*ptr).data) }
256+
}
257+
249258
/// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
250259
///
251260
/// # Safety
@@ -539,11 +548,11 @@ impl<T: ?Sized> ArcBorrow<'_, T> {
539548
}
540549

541550
/// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
542-
/// [`Arc::into_raw`].
551+
/// [`Arc::into_raw`] or [`Arc::as_ptr`].
543552
///
544553
/// # Safety
545554
///
546-
/// * The provided pointer must originate from a call to [`Arc::into_raw`].
555+
/// * The provided pointer must originate from a call to [`Arc::into_raw`] or [`Arc::as_ptr`].
547556
/// * For the duration of the lifetime annotated on this `ArcBorrow`, the reference count must
548557
/// not hit zero.
549558
/// * For the duration of the lifetime annotated on this `ArcBorrow`, there must not be a

0 commit comments

Comments
 (0)