arc.rs (61b7369483efb5e0a9f3b48e75fac00d46d661e0) arc.rs (1d24eb2d536ba27ef938a6563ac8bfb49c738cc1)
1// SPDX-License-Identifier: GPL-2.0
2
3//! A reference-counted pointer.
4//!
5//! This module implements a way for users to create reference-counted objects and pointers to
6//! them. Such a pointer automatically increments and decrements the count, and drops the
7//! underlying object when it reaches zero. It is also safe to use concurrently from multiple
8//! threads.

--- 229 unchanged lines hidden (view full) ---

238 }
239
240 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
241 // SAFETY: By the safety requirement of this function, we know that `ptr` came from
242 // a previous call to `Arc::into_foreign`.
243 let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
244
245 // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
1// SPDX-License-Identifier: GPL-2.0
2
3//! A reference-counted pointer.
4//!
5//! This module implements a way for users to create reference-counted objects and pointers to
6//! them. Such a pointer automatically increments and decrements the count, and drops the
7//! underlying object when it reaches zero. It is also safe to use concurrently from multiple
8//! threads.

--- 229 unchanged lines hidden (view full) ---

238 }
239
240 unsafe fn borrow<'a>(ptr: *const core::ffi::c_void) -> ArcBorrow<'a, T> {
241 // SAFETY: By the safety requirement of this function, we know that `ptr` came from
242 // a previous call to `Arc::into_foreign`.
243 let inner = NonNull::new(ptr as *mut ArcInner<T>).unwrap();
244
245 // SAFETY: The safety requirements of `from_foreign` ensure that the object remains alive
246 // for the lifetime of the returned value. Additionally, the safety requirements of
247 // `ForeignOwnable::borrow_mut` ensure that no new mutable references are created.
246 // for the lifetime of the returned value.
248 unsafe { ArcBorrow::new(inner) }
249 }
250
251 unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
252 // SAFETY: By the safety requirement of this function, we know that `ptr` came from
253 // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
254 // holds a reference count increment that is transferrable to us.
255 unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }

--- 378 unchanged lines hidden ---
247 unsafe { ArcBorrow::new(inner) }
248 }
249
250 unsafe fn from_foreign(ptr: *const core::ffi::c_void) -> Self {
251 // SAFETY: By the safety requirement of this function, we know that `ptr` came from
252 // a previous call to `Arc::into_foreign`, which guarantees that `ptr` is valid and
253 // holds a reference count increment that is transferrable to us.
254 unsafe { Self::from_inner(NonNull::new(ptr as _).unwrap()) }

--- 378 unchanged lines hidden ---