Skip to contentSkip to conversation

rust: kernel: types: Add `ARef::into_raw`

PATCH In review Vincenzo Palazzo 2 participants 3 messages Mar 25, 2024 – Mar 29, 2024 Rust

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: kernel: types: Add `ARef::into_raw`

Add the function into_raw to ARef<T>. This method can be used to turn an ARef into a raw pointer.

---
 rust/kernel/types.rs | 32 ++++++++++++++++++++++++++++++++
 1 file changed, 32 insertions(+)
rust/kernel/types.rs +32 -0
index aa77bad9bce4..b2a243bb810e 100644
@@ -366,6 +366,38 @@ pub unsafe fn from_raw(ptr: NonNull<T>) -> Self {
             _p: PhantomData,
         }
     }
+
+    /// Deconstructs a [`ARef`] object into a raw pointer.
+    ///
+    /// It can be reconstructed once via [`ARef::from_raw`].
+    ///
+    /// Note: This function does not decrement the reference count.
+    ///
+    /// # Examples
+    ///
+    /// ```
+    /// use core::ptr::NonNull;
+    /// use kernel::AlwaysRefCounted;
+    ///
+    /// struct Empty {}
+    ///
+    /// unsafe impl AlwaysRefCounted for Empty {
+    ///     fn inc_ref(&self) {}
+    ///     unsafe fn dec_ref(_obj: NonNull<Self>) {}
+    /// }
+    ///
+    /// let mut data = Empty {};
+    /// let ptr = NonNull::<Empty>::new(&mut data as *mut _).unwrap();
+    /// let data_ref: ARef<Empty> = unsafe { ARef::from_raw(ptr) };
+    /// let raw_ptr: *mut Empty = ARef::into_raw(data_ref);
+    ///
+    /// assert_eq!(ptr.as_ptr(), raw_ptr);
+    /// ```
+    pub fn into_raw(obj: Self) -> *mut T {
+        let ptr = obj.ptr.as_ptr();
+        core::mem::forget(obj);
+        ptr
+    }
 }
 
 impl<T: AlwaysRefCounted> Clone for ARef<T> {
Signature · 1 line
2.44.0
#1 lore raw

Hi Vincenzo,

Thanks for sending this!

(This is a resend -- apparently vger has had some delays for the last ~14 hours or so.)

Quoted 8 lines · Vincenzo Palazzo

Add the function into_raw to ARef<T>. This method can be used to turn an ARef into a raw pointer.

Given what you wrote here, I think you intended to give the primary authorship to Kartik, right? If that is the case, that would require a different ordering of these lines (like the last example at
https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by):

In addition, the commit should be properly attributed to Kartik in Git, which means you will automatically get the From: Kartik ... line too that you see in the example of the documentation I linked above.

However, from what I can tell, you didn't modify the commit from
Kartik at all. If that is correct, then the Co-developed-by should
not be there (https://docs.kernel.org/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
has the details).

Finally, for v2, could you please To/Cc the rest of the maintainers/reviewers? You can use scripts/get_maintainer.pl for that, or the couple commands at
https://rust-for-linux.com/contributing#submitting-patches.

Thanks!

Cheers,
Miguel

#2 lore raw
Quoted 6 lines

Hi Vincenzo,

Thanks for sending this!

(This is a resend -- apparently vger has had some delays for the last ~14 hours or so.)

Yeah sorry about the double posting on the ML, but looks like that now all is fine.

Thanks

Vincent.

Quoted 40 lines
Quoted 8 lines · Vincenzo Palazzo

Add the function into_raw to ARef<T>. This method can be used to turn an ARef into a raw pointer.

Given what you wrote here, I think you intended to give the primary authorship to Kartik, right? If that is the case, that would require a different ordering of these lines (like the last example at
https://docs.kernel.org/process/submitting-patches.html#when-to-use-acked-by-cc-and-co-developed-by):

In addition, the commit should be properly attributed to Kartik in Git, which means you will automatically get the From: Kartik ... line too that you see in the example of the documentation I linked above.

However, from what I can tell, you didn't modify the commit from
Kartik at all. If that is correct, then the Co-developed-by should
not be there (https://docs.kernel.org/process/submitting-patches.html#developer-s-certificate-of-origin-1-1
has the details).

Finally, for v2, could you please To/Cc the rest of the maintainers/reviewers? You can use scripts/get_maintainer.pl for that, or the couple commands at
https://rust-for-linux.com/contributing#submitting-patches.

Thanks!

Cheers,
Miguel