rusticl: rename CheckedPtr::copy_checked to match primitive method
v2: reorder commits for cherry-picking and remove alignment check Reviewed-by: Karol Herbst <kherbst@redhat.com> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/33989>
This commit is contained in:
@@ -926,7 +926,7 @@ fn get_supported_image_formats(
|
||||
// SAFETY: Callers are responsible for providing either a null pointer or
|
||||
// one for which a write of `num_entries * size_of::<cl_image_format>()` is
|
||||
// valid. The validity of reading from `res` is guaranteed by the compiler.
|
||||
unsafe { image_formats.copy_checked(res.as_ptr(), num_entries_to_write) };
|
||||
unsafe { image_formats.copy_from_checked(res.as_ptr(), num_entries_to_write) };
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ use std::{
|
||||
hash::{Hash, Hasher},
|
||||
mem,
|
||||
ops::{Add, Deref},
|
||||
ptr::{self, NonNull},
|
||||
ptr::NonNull,
|
||||
};
|
||||
|
||||
/// A wrapper around pointers to C data type which are considered thread safe.
|
||||
@@ -54,24 +54,25 @@ unsafe impl<T> Send for ThreadSafeCPtr<T> {}
|
||||
unsafe impl<T> Sync for ThreadSafeCPtr<T> {}
|
||||
|
||||
pub trait CheckedPtr<T> {
|
||||
/// Copies `count * size_of::<T>()` bytes from `src` to `self`. The source
|
||||
/// and destination may overlap.
|
||||
///
|
||||
/// # Safety
|
||||
///
|
||||
/// besides a null check the function can't make sure the pointer is valid
|
||||
/// for the entire size
|
||||
unsafe fn copy_checked(self, val: *const T, size: usize);
|
||||
/// The nullity of `self` is checked. `self` and `src` must fulfill all
|
||||
/// other invariants of [`std::ptr::copy`].
|
||||
unsafe fn copy_from_checked(self, src: *const T, count: usize);
|
||||
|
||||
fn write_checked(self, val: T);
|
||||
}
|
||||
|
||||
impl<T> CheckedPtr<T> for *mut T {
|
||||
/// # Safety
|
||||
///
|
||||
/// This function follows the same safety rules as `std::ptr::copy` except that it already
|
||||
/// checks for a NULL pointer.
|
||||
unsafe fn copy_checked(self, val: *const T, size: usize) {
|
||||
unsafe fn copy_from_checked(self, src: *const T, count: usize) {
|
||||
if !self.is_null() {
|
||||
// SAFETY: we move the responsibilities up to the caller
|
||||
// SAFETY: Caller is responsible for satisfying all invariants save
|
||||
// pointer nullity.
|
||||
unsafe {
|
||||
ptr::copy(val, self, size);
|
||||
self.copy_from(src, count);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user