From 1164d39c3851b83712ea181c01536f6ed691e403 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Se=C3=A1n=20de=20B=C3=BArca?= Date: Wed, 12 Mar 2025 11:22:35 -0700 Subject: [PATCH] rusticl: rename CheckedPtr::copy_checked to match primitive method v2: reorder commits for cherry-picking and remove alignment check Reviewed-by: Karol Herbst Part-of: --- src/gallium/frontends/rusticl/api/memory.rs | 2 +- src/gallium/frontends/rusticl/util/ptr.rs | 23 +++++++++++---------- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/gallium/frontends/rusticl/api/memory.rs b/src/gallium/frontends/rusticl/api/memory.rs index 4aa80b2df2d..5991d2a06b5 100644 --- a/src/gallium/frontends/rusticl/api/memory.rs +++ b/src/gallium/frontends/rusticl/api/memory.rs @@ -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::()` 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(()) } diff --git a/src/gallium/frontends/rusticl/util/ptr.rs b/src/gallium/frontends/rusticl/util/ptr.rs index be4cfa6efe8..9af454e0720 100644 --- a/src/gallium/frontends/rusticl/util/ptr.rs +++ b/src/gallium/frontends/rusticl/util/ptr.rs @@ -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 Send for ThreadSafeCPtr {} unsafe impl Sync for ThreadSafeCPtr {} pub trait CheckedPtr { + /// Copies `count * size_of::()` 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 CheckedPtr 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); } } }