Skip to content

Commit fd36c7a

Browse files
committed
Add RawRc type
1 parent ea6130c commit fd36c7a

File tree

3 files changed

+392
-0
lines changed

3 files changed

+392
-0
lines changed

library/alloc/src/raw_rc/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ use core::cell::UnsafeCell;
6666
use core::mem;
6767
use core::sync::atomic::Atomic;
6868

69+
mod raw_rc;
6970
mod raw_weak;
7071
mod rc_alloc;
7172
mod rc_layout;

library/alloc/src/raw_rc/raw_rc.rs

Lines changed: 355 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,355 @@
1+
use core::alloc::Allocator;
2+
use core::cell::UnsafeCell;
3+
#[cfg(not(no_global_oom_handling))]
4+
use core::clone::CloneToUninit;
5+
use core::marker::PhantomData;
6+
#[cfg(not(no_global_oom_handling))]
7+
use core::mem::{self, DropGuard};
8+
#[cfg(not(no_global_oom_handling))]
9+
use core::ops::DerefMut;
10+
#[cfg(not(no_global_oom_handling))]
11+
use core::ptr;
12+
use core::ptr::NonNull;
13+
14+
#[cfg(not(no_global_oom_handling))]
15+
use crate::raw_rc::MakeMutStrategy;
16+
use crate::raw_rc::RefCounter;
17+
#[cfg(not(no_global_oom_handling))]
18+
use crate::raw_rc::raw_weak;
19+
use crate::raw_rc::raw_weak::RawWeak;
20+
#[cfg(not(no_global_oom_handling))]
21+
use crate::raw_rc::rc_alloc;
22+
#[cfg(not(no_global_oom_handling))]
23+
use crate::raw_rc::rc_layout::RcLayout;
24+
use crate::raw_rc::rc_value_pointer::RcValuePointer;
25+
26+
/// Decrements strong reference count in a reference-counted allocation with a value object that is
27+
/// pointed to by `value_ptr`.
28+
#[inline]
29+
unsafe fn decrement_strong_ref_count<R>(value_ptr: RcValuePointer) -> bool
30+
where
31+
R: RefCounter,
32+
{
33+
unsafe { R::from_raw_counter(value_ptr.strong_count_ptr().as_ref()).decrement() }
34+
}
35+
36+
/// Increments strong reference count in a reference-counted allocation with a value object that is
37+
/// pointed to by `value_ptr`.
38+
#[inline]
39+
unsafe fn increment_strong_ref_count<R>(value_ptr: RcValuePointer)
40+
where
41+
R: RefCounter,
42+
{
43+
unsafe { R::from_raw_counter(value_ptr.strong_count_ptr().as_ref()).increment() };
44+
}
45+
46+
#[inline]
47+
unsafe fn is_unique<R>(value_ptr: RcValuePointer) -> bool
48+
where
49+
R: RefCounter,
50+
{
51+
let ref_counts = unsafe { value_ptr.ref_counts_ptr().as_ref() };
52+
53+
unsafe {
54+
R::is_unique(R::from_raw_counter(&ref_counts.strong), R::from_raw_counter(&ref_counts.weak))
55+
}
56+
}
57+
58+
/// Base implementation of a strong pointer. `RawRc` does not implement `Drop`, user should call
59+
/// `RawRc::drop` manually to drop this object.
60+
#[repr(transparent)]
61+
pub(crate) struct RawRc<T, A>
62+
where
63+
T: ?Sized,
64+
{
65+
/// A `RawRc` is just a non-dangling `RawWeak` that has a strong reference count that is owned
66+
/// by the `RawRc` object. The weak pointer is always non-dangling.
67+
weak: RawWeak<T, A>,
68+
69+
// Defines the ownership of `T` for drop-check.
70+
_phantom_data: PhantomData<T>,
71+
}
72+
73+
impl<T, A> RawRc<T, A>
74+
where
75+
T: ?Sized,
76+
{
77+
/// # Safety
78+
///
79+
/// - `ptr` points to a value inside a reference-counted allocation.
80+
/// - The allocation can be freed by `A::default()`.
81+
pub(crate) unsafe fn from_raw(ptr: NonNull<T>) -> Self
82+
where
83+
A: Default,
84+
{
85+
unsafe { Self::from_raw_parts(ptr, A::default()) }
86+
}
87+
88+
/// # Safety
89+
///
90+
/// - `ptr` points to a value inside a reference-counted allocation.
91+
/// - The allocation can be freed by `alloc`.
92+
pub(crate) unsafe fn from_raw_parts(ptr: NonNull<T>, alloc: A) -> Self {
93+
unsafe { Self::from_weak(RawWeak::from_raw_parts(ptr, alloc)) }
94+
}
95+
96+
/// # Safety
97+
///
98+
/// `weak` must have at least one unowned strong reference count. The newly created `RawRc` will
99+
/// take the ownership of exactly one strong reference count.
100+
pub(super) unsafe fn from_weak(weak: RawWeak<T, A>) -> Self {
101+
Self { weak, _phantom_data: PhantomData }
102+
}
103+
104+
pub(crate) fn allocator(&self) -> &A {
105+
&self.weak.allocator()
106+
}
107+
108+
pub(crate) fn as_ptr(&self) -> NonNull<T> {
109+
self.weak.as_ptr()
110+
}
111+
112+
pub(crate) unsafe fn cast<U>(self) -> RawRc<U, A> {
113+
unsafe { RawRc::from_weak(self.weak.cast()) }
114+
}
115+
116+
#[inline]
117+
pub(crate) unsafe fn cast_with<U, F>(self, f: F) -> RawRc<U, A>
118+
where
119+
U: ?Sized,
120+
F: FnOnce(NonNull<T>) -> NonNull<U>,
121+
{
122+
unsafe { RawRc::from_weak(self.weak.cast_with(f)) }
123+
}
124+
125+
#[inline]
126+
pub(crate) unsafe fn clone<R>(&self) -> Self
127+
where
128+
A: Clone,
129+
R: RefCounter,
130+
{
131+
unsafe {
132+
increment_strong_ref_count::<R>(self.value_ptr());
133+
134+
Self::from_raw_parts(self.weak.as_ptr(), self.allocator().clone())
135+
}
136+
}
137+
138+
pub(crate) unsafe fn decrement_strong_count<R: RefCounter>(ptr: NonNull<T>)
139+
where
140+
A: Allocator + Default,
141+
{
142+
unsafe { Self::decrement_strong_count_in::<R>(ptr, A::default()) };
143+
}
144+
145+
pub(crate) unsafe fn decrement_strong_count_in<R: RefCounter>(ptr: NonNull<T>, alloc: A)
146+
where
147+
A: Allocator,
148+
{
149+
unsafe { RawRc::from_raw_parts(ptr, alloc).drop::<R>() };
150+
}
151+
152+
pub(crate) unsafe fn increment_strong_count<R: RefCounter>(ptr: NonNull<T>) {
153+
unsafe { increment_strong_ref_count::<R>(RcValuePointer::from_value_ptr(ptr.cast())) };
154+
}
155+
156+
pub(crate) unsafe fn downgrade<R>(&self) -> RawWeak<T, A>
157+
where
158+
A: Clone,
159+
R: RefCounter,
160+
{
161+
unsafe fn inner<R>(value_ptr: RcValuePointer)
162+
where
163+
R: RefCounter,
164+
{
165+
unsafe { R::from_raw_counter(value_ptr.weak_count_ptr().as_ref()).downgrade() };
166+
}
167+
168+
unsafe {
169+
inner::<R>(self.value_ptr());
170+
171+
RawWeak::from_raw_parts(self.weak.as_ptr(), self.allocator().clone())
172+
}
173+
}
174+
175+
#[inline]
176+
pub(crate) unsafe fn drop<R>(&mut self)
177+
where
178+
A: Allocator,
179+
R: RefCounter,
180+
{
181+
let is_last_strong_ref = unsafe { decrement_strong_ref_count::<R>(self.value_ptr()) };
182+
183+
if is_last_strong_ref {
184+
unsafe { self.weak.assume_init_drop::<R>() }
185+
}
186+
}
187+
188+
pub(crate) unsafe fn get_mut<R>(&mut self) -> Option<&mut T>
189+
where
190+
R: RefCounter,
191+
{
192+
unsafe fn inner<R>(value_ptr: RcValuePointer) -> Option<RcValuePointer>
193+
where
194+
R: RefCounter,
195+
{
196+
unsafe { is_unique::<R>(value_ptr) }.then_some(value_ptr)
197+
}
198+
199+
let (ptr, metadata) = self.weak.as_ptr().to_raw_parts();
200+
201+
unsafe { inner::<R>(RcValuePointer::from_value_ptr(ptr)) }
202+
.map(|ptr| unsafe { NonNull::from_raw_parts(ptr.as_ptr(), metadata).as_mut() })
203+
}
204+
205+
/// Returns a mutable reference to the contained value.
206+
///
207+
/// # Safety
208+
///
209+
/// No other active references to the contained value should exist, and no new references to the
210+
/// contained value will be acquired for the duration of the returned borrow.
211+
pub(crate) unsafe fn get_mut_unchecked(&mut self) -> &mut T {
212+
// SAFETY: The caller guarantees that we can access the contained value exclusively. Note
213+
// that we can't create mutable references that have access to reference counters, because
214+
// the caller only guarantee exclusive access to the contained value, not the reference
215+
// counters.
216+
unsafe { self.weak.as_ptr().as_mut() }
217+
}
218+
219+
pub(crate) fn into_raw(self) -> NonNull<T> {
220+
self.weak.into_raw()
221+
}
222+
223+
pub(crate) fn into_raw_parts(self) -> (NonNull<T>, A) {
224+
self.weak.into_raw_parts()
225+
}
226+
227+
#[cfg(not(no_global_oom_handling))]
228+
pub(crate) unsafe fn make_mut<R>(&mut self) -> &mut T
229+
where
230+
T: CloneToUninit,
231+
A: Allocator + Clone,
232+
R: RefCounter,
233+
{
234+
/// Returns a drop guard that sets the pointer in `rc` to `ptr` on drop.
235+
///
236+
/// # Safety
237+
///
238+
/// - `ptr` must point to a valid reference counted value that can be deallocated with the
239+
/// allocator associated with `rc`.
240+
/// - The value pointed to by `ptr` must have an unowned strong reference count that can be
241+
/// taken ownership by `rc`.
242+
unsafe fn set_rc_ptr_on_drop<'a, T, A>(
243+
rc: &'a mut RawRc<T, A>,
244+
ptr: NonNull<T>,
245+
) -> impl DerefMut<Target = &'a mut RawRc<T, A>>
246+
where
247+
T: ?Sized,
248+
{
249+
DropGuard::new(rc, move |rc| unsafe { rc.weak.set_ptr(ptr) })
250+
}
251+
252+
unsafe {
253+
let ref_counts = self.ref_counts();
254+
255+
if let Some(strategy) = R::make_mut(
256+
R::from_raw_counter(&ref_counts.strong),
257+
R::from_raw_counter(&ref_counts.weak),
258+
) {
259+
let rc_layout = RcLayout::from_value_ptr_unchecked(self.weak.as_ptr());
260+
261+
match strategy {
262+
MakeMutStrategy::Move => {
263+
// `R::make_mut` has made strong reference count to zero, so the `RawRc`
264+
// object is essentially a `RawWeak` object but has its value initialized.
265+
// This means we are the only owner of the value and we can safely move the
266+
// value into a new allocation.
267+
268+
// This guarantees to drop old `RawRc` object even if the allocation
269+
// panics.
270+
271+
let guard = raw_weak::new_weak_guard::<T, A, R>(&mut self.weak);
272+
273+
let new_ptr = rc_alloc::allocate_with_bytes_in::<A, 1>(
274+
guard.as_ptr().cast(),
275+
&guard.allocator(),
276+
rc_layout,
277+
);
278+
279+
// No panic happens, defuse the guard.
280+
mem::forget(guard);
281+
282+
let new_ptr = NonNull::from_raw_parts(
283+
new_ptr.as_ptr(),
284+
ptr::metadata(self.weak.as_ptr().as_ptr()),
285+
);
286+
287+
// Ensure the value pointer in `self` is updated to `new_ptr`.
288+
let mut update_ptr_on_drop = set_rc_ptr_on_drop(self, new_ptr);
289+
290+
// `MakeMutStrategy::Move` guarantees that the strong count is zero, also we
291+
// have copied the value to a new allocation, so we can pretend the original
292+
// `RawRc` is now essentially an `RawWeak` object, we can call the `RawWeak`
293+
// destructor to finish the cleanup.
294+
update_ptr_on_drop.weak.drop_unchecked::<R>();
295+
}
296+
MakeMutStrategy::Clone => {
297+
// There are multiple owners of the value, we need to clone the value into a
298+
// new allocation.
299+
300+
let new_ptr = rc_alloc::allocate_with_in::<A, _, 1>(
301+
&self.allocator(),
302+
rc_layout,
303+
|dst_ptr| {
304+
T::clone_to_uninit(
305+
self.as_ptr().as_ref(),
306+
dst_ptr.as_ptr().as_ptr().cast(),
307+
)
308+
},
309+
);
310+
311+
let new_ptr = NonNull::from_raw_parts(
312+
new_ptr.as_ptr(),
313+
ptr::metadata(self.weak.as_ptr().as_ptr()),
314+
);
315+
316+
// Ensure the value pointer in `self` is updated to `new_ptr`.
317+
let mut update_ptr_on_drop = set_rc_ptr_on_drop(self, new_ptr);
318+
319+
// Manually drop old `RawRc`.
320+
update_ptr_on_drop.drop::<R>();
321+
}
322+
}
323+
}
324+
325+
self.get_mut_unchecked()
326+
}
327+
}
328+
329+
pub(crate) fn ptr_eq(&self, other: &Self) -> bool {
330+
RawWeak::ptr_eq(&self.weak, &other.weak)
331+
}
332+
333+
pub(crate) fn ptr_ne(&self, other: &Self) -> bool {
334+
RawWeak::ptr_ne(&self.weak, &other.weak)
335+
}
336+
337+
#[cfg(not(no_global_oom_handling))]
338+
pub(crate) fn ref_counts(&self) -> &crate::raw_rc::RefCounts {
339+
unsafe { self.weak.ref_counts_unchecked() }
340+
}
341+
342+
pub(crate) fn strong_count(&self) -> &UnsafeCell<usize> {
343+
unsafe { self.weak.strong_count_unchecked() }
344+
}
345+
346+
pub(crate) fn weak_count(&self) -> &UnsafeCell<usize> {
347+
unsafe { self.weak.weak_count_unchecked() }
348+
}
349+
350+
#[inline]
351+
fn value_ptr(&self) -> RcValuePointer {
352+
// SAFETY: `self.weak` is guaranteed to be non-dangling.
353+
unsafe { self.weak.value_ptr_unchecked() }
354+
}
355+
}

0 commit comments

Comments
 (0)