Module: Mesa Branch: staging/23.0 Commit: 0e28382eee82ee7d992b0bce727cdd78190c330b URL: http://cgit.freedesktop.org/mesa/mesa/commit/?id=0e28382eee82ee7d992b0bce727cdd78190c330b
Author: Karol Herbst <[email protected]> Date: Fri Feb 10 17:00:24 2023 +0100 rusticl/util: extract offset_of macro Signed-off-by: Karol Herbst <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/22007> --- src/gallium/frontends/rusticl/mesa/compiler/nir.rs | 21 +-------------------- src/gallium/frontends/rusticl/util/ptr.rs | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/gallium/frontends/rusticl/mesa/compiler/nir.rs b/src/gallium/frontends/rusticl/mesa/compiler/nir.rs index 347b3c95c2a..7063b67334c 100644 --- a/src/gallium/frontends/rusticl/mesa/compiler/nir.rs +++ b/src/gallium/frontends/rusticl/mesa/compiler/nir.rs @@ -1,5 +1,6 @@ use mesa_rust_gen::*; use mesa_rust_util::bitset; +use mesa_rust_util::offset_of; use std::convert::TryInto; use std::ffi::c_void; @@ -9,26 +10,6 @@ use std::ptr; use std::ptr::NonNull; use std::slice; -// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2 -macro_rules! offset_of { - ($Struct:path, $field:ident) => {{ - // Using a separate function to minimize unhygienic hazards - // (e.g. unsafety of #[repr(packed)] field borrows). - // Uncomment `const` when `const fn`s can juggle pointers. - /*const*/ - fn offset() -> usize { - let u = std::mem::MaybeUninit::<$Struct>::uninit(); - // Use pattern-matching to avoid accidentally going through Deref. - let &$Struct { $field: ref f, .. } = unsafe { &*u.as_ptr() }; - let o = (f as *const _ as usize).wrapping_sub(&u as *const _ as usize); - // Triple check that we are within `u` still. - assert!((0..=std::mem::size_of_val(&u)).contains(&o)); - o - } - offset() - }}; -} - pub struct ExecListIter<'a, T> { n: &'a mut exec_node, offset: usize, diff --git a/src/gallium/frontends/rusticl/util/ptr.rs b/src/gallium/frontends/rusticl/util/ptr.rs index 9167be7fec0..98532ad4fb0 100644 --- a/src/gallium/frontends/rusticl/util/ptr.rs +++ b/src/gallium/frontends/rusticl/util/ptr.rs @@ -24,3 +24,24 @@ impl<T> CheckedPtr<T> for *mut T { } } } + +// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2 +#[macro_export] +macro_rules! offset_of { + ($Struct:path, $field:ident) => {{ + // Using a separate function to minimize unhygienic hazards + // (e.g. unsafety of #[repr(packed)] field borrows). + // Uncomment `const` when `const fn`s can juggle pointers. + /*const*/ + fn offset() -> usize { + let u = std::mem::MaybeUninit::<$Struct>::uninit(); + // Use pattern-matching to avoid accidentally going through Deref. + let &$Struct { $field: ref f, .. } = unsafe { &*u.as_ptr() }; + let o = (f as *const _ as usize).wrapping_sub(&u as *const _ as usize); + // Triple check that we are within `u` still. + assert!((0..=std::mem::size_of_val(&u)).contains(&o)); + o + } + offset() + }}; +}
