Re: [PATCH v8 03/14] rust: sync: add `Arc::as_ptr`
From: Benno Lossin
Date: Thu Feb 20 2025 - 18:18:58 EST
On 18.02.25 14:27, Andreas Hindborg wrote:
> Add a method to get a pointer to the data contained in an `Arc`.
>
> Reviewed-by: Lyude Paul <lyude@xxxxxxxxxx>
> Reviewed-by: Alice Ryhl <aliceryhl@xxxxxxxxxx>
> Signed-off-by: Andreas Hindborg <a.hindborg@xxxxxxxxxx>
> ---
Reviewed-by: Benno Lossin <benno.lossin@xxxxxxxxx>
---
Cheers,
Benno
>
> This is a dependency for:
>
> rust: hrtimer: implement `HrTimerPointer` for `Arc`
> ---
> rust/kernel/sync/arc.rs | 13 +++++++++++--
> 1 file changed, 11 insertions(+), 2 deletions(-)
>
> diff --git a/rust/kernel/sync/arc.rs b/rust/kernel/sync/arc.rs
> index 3cefda7a43725..1dfa75714f9d6 100644
> --- a/rust/kernel/sync/arc.rs
> +++ b/rust/kernel/sync/arc.rs
> @@ -246,6 +246,15 @@ pub fn into_raw(self) -> *const T {
> unsafe { core::ptr::addr_of!((*ptr).data) }
> }
>
> + /// Return a raw pointer to the data in this arc.
> + pub fn as_ptr(this: &Self) -> *const T {
> + let ptr = this.ptr.as_ptr();
> +
> + // SAFETY: As `ptr` points to a valid allocation of type `ArcInner`,
> + // field projection to `data`is within bounds of the allocation.
> + unsafe { core::ptr::addr_of!((*ptr).data) }
> + }
> +
> /// Recreates an [`Arc`] instance previously deconstructed via [`Arc::into_raw`].
> ///
> /// # Safety
> @@ -539,11 +548,11 @@ unsafe fn new(inner: NonNull<ArcInner<T>>) -> Self {
> }
>
> /// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
> - /// [`Arc::into_raw`].
> + /// [`Arc::into_raw`] or [`Arc::as_ptr`].
> ///
> /// # Safety
> ///
> - /// * The provided pointer must originate from a call to [`Arc::into_raw`].
> + /// * The provided pointer must originate from a call to [`Arc::into_raw`] or [`Arc::as_ptr`].
> /// * For the duration of the lifetime annotated on this `ArcBorrow`, the reference count must
> /// not hit zero.
> /// * For the duration of the lifetime annotated on this `ArcBorrow`, there must not be a
>
> --
> 2.47.0
>
>