Skip to content

Commit e77f29b

Browse files
authored
mod c_{box,arc}: Make safe (#1275)
Including #1239, this is the last of non-`neon` `unsafe` ops.
2 parents 5cf49e0 + e19d07c commit e77f29b

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

src/c_arc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use crate::src::c_box::CBox;
24
use crate::src::error::Rav1dResult;
35
use std::marker::PhantomData;
@@ -212,7 +214,7 @@ impl<T: ?Sized> CArc<T> {
212214
pub unsafe fn from_raw(raw: RawCArc<T>) -> Self {
213215
// Safety: The [`RawCArc`] contains the output of [`Arc::into_raw`],
214216
// so we can call [`Arc::from_raw`] on it.
215-
let owner = raw.0.into_arc();
217+
let owner = unsafe { raw.0.into_arc() };
216218
owner.into()
217219
}
218220
}

src/c_box.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![deny(unsafe_op_in_unsafe_fn)]
2+
13
use std::ffi::c_void;
24
use std::marker::PhantomData;
35
use std::ops::Deref;
@@ -15,8 +17,15 @@ pub struct Free {
1517
}
1618

1719
impl Free {
20+
/// # Safety
21+
///
22+
/// `ptr` is a [`NonNull`]`<T>` and `free` deallocates it.
23+
/// It must not be used after this call as it is deallocated.
1824
pub unsafe fn free(&self, ptr: *mut c_void) {
19-
(self.free)(ptr as *const u8, self.cookie)
25+
// SAFETY: `self` came from `CBox::from_c`,
26+
// which requires `self.free` to deallocate the `NonNull<T>` passed to it,
27+
// and `self.cookie` to be passed to it, which it is.
28+
unsafe { (self.free)(ptr as *const u8, self.cookie) }
2029
}
2130
}
2231

@@ -85,7 +94,9 @@ impl<T: ?Sized> CBox<T> {
8594
/// # Safety
8695
///
8796
/// `data` must be valid to dereference
88-
/// until `free` is called on it, which must deallocate it.
97+
/// until `free.free` is called on it, which must deallocate it.
98+
/// `free.free` is always called with `free.cookie`,
99+
/// which must be accessed thread-safely.
89100
pub unsafe fn from_c(data: NonNull<T>, free: Free) -> Self {
90101
Self::C {
91102
data,

0 commit comments

Comments
 (0)