-
Notifications
You must be signed in to change notification settings - Fork 66
struct Unique: Copy more from std and use for Rav1dPictureDataComponentInner::ptr to make it Send + Sync
#1327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
dd67e1c
9af7c95
3390314
7cf8f5b
8f52b83
4369d09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ use crate::src::error::Rav1dResult; | |
| use crate::src::pixels::Pixels; | ||
| use crate::src::send_sync_non_null::SendSyncNonNull; | ||
| use crate::src::strided::Strided; | ||
| use crate::src::unique::Unique; | ||
| use crate::src::with_offset::WithOffset; | ||
| use libc::ptrdiff_t; | ||
| use libc::uintptr_t; | ||
|
|
@@ -94,7 +95,7 @@ impl From<Rav1dPictureParameters> for Dav1dPictureParameters { | |
| pub struct Dav1dPicture { | ||
| pub seq_hdr: Option<NonNull<Dav1dSequenceHeader>>, | ||
| pub frame_hdr: Option<NonNull<Dav1dFrameHeader>>, | ||
| pub data: [Option<NonNull<c_void>>; 3], | ||
| pub data: [Option<Unique<c_void>>; 3], | ||
| pub stride: [ptrdiff_t; 2], | ||
| pub p: Dav1dPictureParameters, | ||
| pub m: Dav1dDataProps, | ||
|
|
@@ -134,7 +135,7 @@ pub struct Rav1dPictureDataComponentInner { | |
| /// even if [`Self::stride`] is negative. | ||
| /// | ||
| /// It is aligned to [`RAV1D_PICTURE_ALIGNMENT`]. | ||
| ptr: NonNull<u8>, | ||
| ptr: Unique<u8>, | ||
|
|
||
| /// The length of [`Self::ptr`] in [`u8`] bytes. | ||
| /// | ||
|
|
@@ -151,12 +152,12 @@ impl Rav1dPictureDataComponentInner { | |
| /// # Safety | ||
| /// | ||
| /// `ptr`, `len`, and `stride` must follow the requirements of [`Dav1dPicAllocator::alloc_picture_callback`]. | ||
| unsafe fn new(ptr: Option<NonNull<u8>>, len: usize, stride: isize) -> Self { | ||
| unsafe fn new(ptr: Option<Unique<u8>>, len: usize, stride: isize) -> Self { | ||
| let ptr = match ptr { | ||
| None => { | ||
| return Self { | ||
| // Ensure it is aligned enough. | ||
| ptr: NonNull::<AlignedPixelChunk>::dangling().cast(), | ||
| ptr: Unique::<AlignedPixelChunk>::dangling().cast(), | ||
| len: 0, | ||
| stride, | ||
| }; | ||
|
|
@@ -168,13 +169,15 @@ impl Rav1dPictureDataComponentInner { | |
| assert!(ptr.cast::<AlignedPixelChunk>().is_aligned()); | ||
|
|
||
| let ptr = if stride < 0 { | ||
| let ptr = ptr.as_ptr(); | ||
| // SAFETY: According to `Dav1dPicAllocator::alloc_picture_callback`, | ||
| // if the `stride` is negative, this is how we get the start of the data. | ||
| // `.offset(-stride)` puts us at one element past the end of the slice, | ||
| // and `.sub(len)` puts us back at the start of the slice. | ||
| let ptr = unsafe { ptr.offset(-stride).sub(len) }; | ||
| NonNull::new(ptr).unwrap() | ||
| ptr.map(|ptr| { | ||
| let ptr = ptr.as_ptr(); | ||
| // SAFETY: According to `Dav1dPicAllocator::alloc_picture_callback`, | ||
| // if the `stride` is negative, this is how we get the start of the data. | ||
| // `.offset(-stride)` puts us at one element past the end of the slice, | ||
| // and `.sub(len)` puts us back at the start of the slice. | ||
| let ptr = unsafe { ptr.offset(-stride).sub(len) }; | ||
| NonNull::new(ptr).unwrap() | ||
| }) | ||
| } else { | ||
| ptr | ||
| }; | ||
|
|
@@ -189,7 +192,7 @@ impl Rav1dPictureDataComponentInner { | |
| /// so it is sound to further subdivide it into disjoint `&mut`s. | ||
| pub fn wrap_buf<BD: BitDepth>(buf: &mut [BD::Pixel], stride: usize) -> Self { | ||
| let buf = AsBytes::as_bytes_mut(buf); | ||
| let ptr = NonNull::new(buf.as_mut_ptr()).unwrap(); | ||
| let ptr = Unique::from_ref_mut(buf).cast(); | ||
| assert!(ptr.cast::<AlignedPixelChunk>().is_aligned()); | ||
| let len = buf.len(); | ||
| assert!(len % RAV1D_PICTURE_GUARANTEED_MULTIPLE == 0); | ||
|
|
@@ -292,11 +295,14 @@ impl Rav1dPictureDataComponent { | |
| self.as_strided_mut_ptr::<BD>().cast_const() | ||
| } | ||
|
|
||
| fn as_dav1d(&self) -> Option<NonNull<c_void>> { | ||
| fn as_dav1d(&self) -> Option<Unique<c_void>> { | ||
| if self.byte_len() == 0 { | ||
| None | ||
| } else { | ||
| NonNull::new(self.as_strided_byte_mut_ptr().cast()) | ||
| let ptr = NonNull::new(self.as_strided_byte_mut_ptr()).unwrap(); | ||
| // SAFETY: The `ptr` originally comes from a `Unique` in `Rav1dPictureDataComponentInner::ptr`. | ||
| let ptr = unsafe { Unique::new(ptr) }; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Again, how is this unique? Can this method return a NonNull instead?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does that mean there's two Uniques pointing to the same data? The original and the one into the middle?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. e.g. can I just call this twice and get two
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ugh, yeah, you're right. Why does
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. No idea. |
||
| Some(ptr.cast()) | ||
| } | ||
| } | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How is this unique in the wrap_buf case? We're borrowing a reference to this data, right? So should we really use unique here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A
&mutreference is always unique. So it can be&mutor owned.core::ptr::UniqueimplsFrom<&mut T>even.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But once that constructor returns the borrow is over, unless there's a lifetime. So it's no longer unique? How do we ensure no where else has a pointer to the same thing?