mirror of
https://github.com/nxp-imx/linux-imx.git
synced 2025-07-18 23:29:57 +02:00
ANDROID: rust: sync: add ArcBorrow::from_raw
A dependency of the Rust linked list. This patch is an unmodified version of a patch included in the list of dependencies of the Rust Binder RFC [1]. It is marked ANDROID: because it has not yet been sent to the mailing list. It needs to be rebased on top of [2] before it can be sent. Link: https://lore.kernel.org/rust-for-linux/20231101-rust-binder-v1-0-08ba9197f637@google.com/ [1] Link: https://lore.kernel.org/all/20240205091842.2858516-1-aliceryhl@google.com/ [2] Bug: 324206405 Change-Id: I80322e059774e6381556dce1aad909e6a4ea31fe Signed-off-by: Alice Ryhl <aliceryhl@google.com>
This commit is contained in:
parent
a1bb998934
commit
b57704d237
|
@ -232,6 +232,39 @@ impl<T: ?Sized> Arc<T> {
|
||||||
/// `ptr` must have been returned by a previous call to [`Arc::into_raw`]. Additionally, it
|
/// `ptr` must have been returned by a previous call to [`Arc::into_raw`]. Additionally, it
|
||||||
/// must not be called more than once for each previous call to [`Arc::into_raw`].
|
/// must not be called more than once for each previous call to [`Arc::into_raw`].
|
||||||
pub unsafe fn from_raw(ptr: *const T) -> Self {
|
pub unsafe fn from_raw(ptr: *const T) -> Self {
|
||||||
|
// SAFETY: The pointer returned by `into_raw` points at the `data` field of an
|
||||||
|
// `ArcInner<T>`.
|
||||||
|
let ptr = unsafe { raw_to_inner_ptr(ptr) };
|
||||||
|
|
||||||
|
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
|
||||||
|
// reference count held then will be owned by the new `Arc` object.
|
||||||
|
unsafe { Self::from_inner(ptr) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns an [`ArcBorrow`] from the given [`Arc`].
|
||||||
|
///
|
||||||
|
/// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
|
||||||
|
/// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
|
||||||
|
#[inline]
|
||||||
|
pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
|
||||||
|
// SAFETY: The constraint that the lifetime of the shared reference must outlive that of
|
||||||
|
// the returned `ArcBorrow` ensures that the object remains alive and that no mutable
|
||||||
|
// reference can be created.
|
||||||
|
unsafe { ArcBorrow::new(self.ptr) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Compare whether two [`Arc`] pointers reference the same underlying object.
|
||||||
|
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
|
||||||
|
core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Converts a pointer to the contents of an [`Arc`] into a pointer to the [`ArcInner`].
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// The provided pointer must point the `data` field of an `ArcInner<T>` value.
|
||||||
|
unsafe fn raw_to_inner_ptr<T: ?Sized>(ptr: *const T) -> NonNull<ArcInner<T>> {
|
||||||
let refcount_layout = Layout::new::<bindings::refcount_t>();
|
let refcount_layout = Layout::new::<bindings::refcount_t>();
|
||||||
// SAFETY: The caller guarantees that the pointer is valid.
|
// SAFETY: The caller guarantees that the pointer is valid.
|
||||||
let val_layout = Layout::for_value(unsafe { &*ptr });
|
let val_layout = Layout::for_value(unsafe { &*ptr });
|
||||||
|
@ -252,27 +285,9 @@ impl<T: ?Sized> Arc<T> {
|
||||||
let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () };
|
let ptr = unsafe { (ptr as *mut u8).sub(val_offset) as *mut () };
|
||||||
let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
|
let ptr = core::ptr::from_raw_parts_mut(ptr, metadata);
|
||||||
|
|
||||||
// SAFETY: By the safety requirements we know that `ptr` came from `Arc::into_raw`, so the
|
// SAFETY: The pointer can't be null since you can't have an `ArcInner<T>` value at the null
|
||||||
// reference count held then will be owned by the new `Arc` object.
|
// address.
|
||||||
unsafe { Self::from_inner(NonNull::new_unchecked(ptr)) }
|
unsafe { NonNull::new_unchecked(ptr) }
|
||||||
}
|
|
||||||
|
|
||||||
/// Returns an [`ArcBorrow`] from the given [`Arc`].
|
|
||||||
///
|
|
||||||
/// This is useful when the argument of a function call is an [`ArcBorrow`] (e.g., in a method
|
|
||||||
/// receiver), but we have an [`Arc`] instead. Getting an [`ArcBorrow`] is free when optimised.
|
|
||||||
#[inline]
|
|
||||||
pub fn as_arc_borrow(&self) -> ArcBorrow<'_, T> {
|
|
||||||
// SAFETY: The constraint that the lifetime of the shared reference must outlive that of
|
|
||||||
// the returned `ArcBorrow` ensures that the object remains alive and that no mutable
|
|
||||||
// reference can be created.
|
|
||||||
unsafe { ArcBorrow::new(self.ptr) }
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Compare whether two [`Arc`] pointers reference the same underlying object.
|
|
||||||
pub fn ptr_eq(this: &Self, other: &Self) -> bool {
|
|
||||||
core::ptr::eq(this.ptr.as_ptr(), other.ptr.as_ptr())
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: 'static> ForeignOwnable for Arc<T> {
|
impl<T: 'static> ForeignOwnable for Arc<T> {
|
||||||
|
@ -455,6 +470,27 @@ impl<T: ?Sized> ArcBorrow<'_, T> {
|
||||||
_p: PhantomData,
|
_p: PhantomData,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates an [`ArcBorrow`] to an [`Arc`] that has previously been deconstructed with
|
||||||
|
/// [`Arc::into_raw`].
|
||||||
|
///
|
||||||
|
/// # Safety
|
||||||
|
///
|
||||||
|
/// * The provided pointer must originate from a call to [`Arc::into_raw`].
|
||||||
|
/// * 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
|
||||||
|
/// [`UniqueArc`] reference to this value.
|
||||||
|
pub unsafe fn from_raw(ptr: *const T) -> Self {
|
||||||
|
// SAFETY: The pointer returned by `into_raw` points at the `data` field of an
|
||||||
|
// `ArcInner<T>`.
|
||||||
|
let ptr = unsafe { raw_to_inner_ptr(ptr) };
|
||||||
|
|
||||||
|
// SAFETY: The caller promises that the value remains valid since the reference count must
|
||||||
|
// not hit zero, and no mutable reference will be created since that would involve a
|
||||||
|
// `UniqueArc`.
|
||||||
|
unsafe { Self::new(ptr) }
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
|
impl<T: ?Sized> From<ArcBorrow<'_, T>> for Arc<T> {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user