1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#![deny(unsafe_op_in_unsafe_fn)]
use std::marker::PhantomData;
use std::ops::Deref;
use std::pin::Pin;
use std::ptr::NonNull;
use std::slice::SliceIndex;
use std::sync::Arc;
use crate::c_box::CRef;
use crate::error::Rav1dResult;
pub fn arc_into_raw<T: ?Sized>(arc: Arc<T>) -> NonNull<T> {
let raw = Arc::into_raw(arc).cast_mut();
// SAFETY: [`Arc::into_raw`] never returns null.
unsafe { NonNull::new_unchecked(raw) }
}
/// A C/custom [`Arc`].
///
/// That is, it is analogous to an [`Arc`],
/// but it lets you set a C-style `free` `fn` for deallocation
/// instead of the normal [`Box`] (de)allocator.
/// It can also store a normal [`Box`] as well.
///
/// It is built around the [`CRef`] abstraction.
/// However, that necessitates a double indirection
/// to reach the ptr through the [`Arc`] and [`CRef`].
/// To remedy this and improve performance,
/// a stable pointer is stored inline,
/// removing the double indirection.
/// This self-referential ptr is sound
/// because the [`CRef`] is [`Pin`]ned.
/// As long as [`Self::owner`] is never replaced
/// without also re-updating [`Self::stable_ref`], this is sound.
///
/// Furthermore, storing this stable ref ptr like this
/// allows for provenance projections of [`Self::stable_ref`],
/// such as slicing it for a `CArc<[T]>` (see [`Self::slice_in_place`]).
pub struct CArc<T: ?Sized + 'static> {
owner: Arc<Pin<CRef<T>>>,
/// The same as [`Self::stable_ref`] but it never changes.
#[cfg(debug_assertions)]
base_stable_ref: StableRef<T>,
stable_ref: StableRef<T>,
}
/// A stable reference, stored as a raw ptr.
///
/// # Safety
///
/// The raw ptr of a [`StableRef`] must have a stable address.
/// Even if `T`'s owning type, e.x. a [`Box`]`<T>`, is moved,
/// ptrs to `T` must remain valid and thus "stable".
///
/// Thus, it can be stored relative to its owner.
#[derive(Debug)]
struct StableRef<T: ?Sized>(NonNull<T>);
impl<T: ?Sized> Clone for StableRef<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T: ?Sized> Copy for StableRef<T> {}
/// SAFETY: [`StableRef`]`<T>`, if it follows its safety guarantees, is essentially a `&T`/`&mut T`, which is [`Send`] if `T: `[`Send`]`.
unsafe impl<T: Send + ?Sized> Send for StableRef<T> {}
/// SAFETY: [`StableRef`]`<T>`, if it follows its safety guarantees, is essentially a `&T`/`&mut T`, which is [`Sync`] if `T: `[`Sync`].
unsafe impl<T: Send + ?Sized> Sync for StableRef<T> {}
impl<T: ?Sized> AsRef<T> for CArc<T> {
fn as_ref(&self) -> &T {
#[cfg(debug_assertions)]
{
use std::{mem, ptr};
use to_method::To;
// Some extra checks to check if our ptrs are definitely invalid.
let real_ref = (*self.owner).as_ref().get_ref();
assert_eq!(real_ref.to::<NonNull<T>>(), self.base_stable_ref.0);
let real_ptr = ptr::from_ref(real_ref);
let stable_ptr = self.stable_ref.0.as_ptr().cast_const();
// Cast through `*const ()` to remove any fat ptr metadata.
// Use arithmetic on the addresses (similar to `.wrapping_*` methods),
// as they don't have safety conditions (which we're checking here).
let [real_address, stable_address] =
[real_ptr, stable_ptr].map(|ptr| ptr.cast::<()>() as isize);
let offset = stable_address - real_address;
let len = mem::size_of_val(real_ref);
if offset < 0 || offset > len as isize {
panic!(
"CArc::stable_ref is out of bounds:
real_ref: {real_ptr:?}
stable_ref: {stable_ptr:?}
offset: {offset}
len: {len}"
);
}
}
// SAFETY: [`Self::stable_ref`] is a ptr
// derived from [`Self::owner`]'s through [`CRef::as_ref`]
// and is thus safe to dereference.
// The [`CRef`] is [`Pin`]ned and
// [`Self::stable_ref`] is always updated on writes to [`Self::owner`],
// so they are always in sync.
unsafe { self.stable_ref.0.as_ref() }
}
}
impl<T: ?Sized> Deref for CArc<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
impl<T: ?Sized> Clone for CArc<T> {
fn clone(&self) -> Self {
let Self {
owner,
#[cfg(debug_assertions)]
base_stable_ref,
stable_ref,
} = self;
Self {
owner: owner.clone(),
#[cfg(debug_assertions)]
base_stable_ref: base_stable_ref.clone(),
stable_ref: stable_ref.clone(),
}
}
}
impl<T: ?Sized> From<Arc<Pin<CRef<T>>>> for CArc<T> {
fn from(owner: Arc<Pin<CRef<T>>>) -> Self {
let stable_ref = StableRef((*owner).as_ref().get_ref().into());
Self {
owner,
#[cfg(debug_assertions)]
base_stable_ref: stable_ref,
stable_ref,
}
}
}
impl<T: ?Sized> CArc<T> {
pub fn wrap(owner: CRef<T>) -> Rav1dResult<Self> {
let owner = Arc::new(owner.into_pin()); // TODO fallible allocation
Ok(owner.into())
}
}
/// An opaque, raw [`Arc`] ptr.
///
/// See [`Arc::from_raw`], [`Arc::into_raw`], and [`arc_into_raw`].
///
/// The [`PhantomData`] is so it can be FFI-safe
/// without `T` having to be `#[repr(C)]`,
/// which it doesn't since it's opaque,
/// while still keeping `T` in the type.
#[repr(transparent)]
pub struct RawArc<T>(NonNull<PhantomData<T>>);
/// We need a manual `impl` since we don't require `T: Clone`.
///
/// # Safety
///
/// Note that this [`RawArc::clone`] does not call [`Arc::clone`],
/// since implicit clones/copies are expected to be done outside of Rust,
/// for which there is no way to force [`RawArc::clone`] to be called.
/// Instead, [`RawArc::as_ref`] and [`RawArc::into_arc`] are `unsafe`,
/// and require [`RawArc::clone`]s (actual explicit calls
/// or implicit ones outside of Rust) to respect the rules of [`Arc`].
impl<T> Clone for RawArc<T> {
fn clone(&self) -> Self {
*self
}
}
impl<T> Copy for RawArc<T> {}
impl<T> RawArc<T> {
pub fn from_arc(arc: Arc<T>) -> Self {
Self(arc_into_raw(arc).cast())
}
/// # Safety
///
/// The [`RawArc`] must be originally from [`Self::from_arc`].
///
/// This must not be called after [`Self::into_arc`],
/// including on [`Clone`]s.
pub unsafe fn as_ref(&self) -> &T {
// SAFETY: `self` must be from `Self::from_arc`,
// which calls `Arc::into_raw`,
// which returns a ptr to its `T`.
// `Arc` allows us to get a `&T` from it,
// so this is allowed (unlike `&mut T`).
// We don't call `Self::into_arc` since that's consuming,
// so we'd have to `mem::forget` the `Arc`
// and also do a redundant dereference.
unsafe { self.0.cast().as_ref() }
}
/// # Safety
///
/// The [`RawArc`] must be originally from [`Self::from_arc`].
///
/// After calling this, the [`RawArc`] and [`Clone`]s of it may not be used anymore.
pub unsafe fn into_arc(self) -> Arc<T> {
let raw = self.0.cast().as_ptr();
// SAFETY: `self` must be from `Self::from_arc`,
// which calls `Arc::into_raw`.
// Thus, it is safe to call the inverse `Arc::from_raw` on it.
unsafe { Arc::from_raw(raw) }
}
}
#[repr(transparent)]
pub struct RawCArc<T: ?Sized + 'static>(RawArc<Pin<CRef<T>>>);
impl<T: ?Sized> CArc<T> {
/// Convert into a raw, opaque form suitable for C FFI.
pub fn into_raw(self) -> RawCArc<T> {
RawCArc(RawArc::from_arc(self.owner))
}
/// # Safety
///
/// The [`RawCArc`] must be originally from [`Self::into_raw`].
pub unsafe fn from_raw(raw: RawCArc<T>) -> Self {
// SAFETY: The [`RawCArc`] contains the output of [`Arc::into_raw`],
// so we can call [`Arc::from_raw`] on it.
let owner = unsafe { raw.0.into_arc() };
owner.into()
}
}
impl<T> CArc<[T]> {
/// Slice [`Self::stable_ref`] in-place.
///
/// The slice stays owned by the [`Arc`],
/// but the [`Self::stable_ref`]/[`Self::as_ref`]/[`Self::deref`] view into it
/// is assigned to the new sub-slice.
pub fn slice_in_place<I>(&mut self, range: I)
where
I: SliceIndex<[T], Output = [T]>,
{
self.stable_ref = StableRef(self.as_ref()[range].into());
}
pub fn split_at(this: Self, mid: usize) -> (Self, Self) {
let mut first = this.clone();
let mut second = this;
first.slice_in_place(..mid);
second.slice_in_place(mid..);
(first, second)
}
}
impl<T> CArc<[T]>
where
T: Default + 'static,
{
pub fn zeroed_slice(size: usize) -> Rav1dResult<Self> {
let owned_slice = (0..size).map(|_| Default::default()).collect::<Box<[_]>>(); // TODO fallible allocation
Self::wrap(CRef::Box(owned_slice))
}
}