rusticl: align memory utilities with std

Rename `is_alligned()` util to match std naming and move it alongside
similar utilities which copy upstream functionality and annotate those
utilities with their first stable version where applicable.

Replace use of `mesa_rust_util::offset_of!()` in one case where nested
field support is not needed.

v2: revert removal of `offset_of!()` for nested field support

Reviewed-by: Karol Herbst <kherbst@redhat.com>
Reviewed-by: @LingMan
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/34167>
This commit is contained in:
Seán de Búrca 2025-03-20 15:03:31 -07:00 committed by Marge Bot
parent 26867670b2
commit ec314aa358
4 changed files with 14 additions and 9 deletions

View file

@ -656,7 +656,7 @@ fn validate_buffer(
let addr_alignment = dev.image_base_address_alignment();
if addr_alignment == 0 {
return Err(CL_INVALID_OPERATION);
} else if !is_alligned(host_ptr, addr_alignment as usize) {
} else if !is_aligned_to(host_ptr, addr_alignment as usize) {
return Err(err);
}
}

View file

@ -491,10 +491,6 @@ pub fn checked_compare(a: usize, o: cmp::Ordering, b: u64) -> bool {
}
}
pub fn is_alligned<T>(ptr: *const T, alignment: usize) -> bool {
ptr as usize & (alignment - 1) == 0
}
pub fn bit_check<A: BitAnd<Output = A> + PartialEq + Default, B: Into<A>>(a: A, b: B) -> bool {
a & b.into() != A::default()
}

View file

@ -1,10 +1,10 @@
use mesa_rust_gen::*;
use mesa_rust_util::bitset;
use mesa_rust_util::offset_of;
use std::convert::TryInto;
use std::ffi::CStr;
use std::marker::PhantomData;
use std::mem::offset_of;
use std::ops::Not;
use std::ptr;
use std::ptr::NonNull;

View file

@ -95,6 +95,8 @@ impl<T> CheckedPtr<T> for *mut T {
}
}
// While std::mem::offset_of!() is stable from 1.77.0, support for nested fields
// (required in some rusticl cases) wasn't stabilized until 1.82.0.
// from https://internals.rust-lang.org/t/discussion-on-offset-of/7440/2
#[macro_export]
macro_rules! offset_of {
@ -115,7 +117,7 @@ macro_rules! offset_of {
}};
}
// Adapted from libstd since std::ptr::is_aligned is still unstable
// Adapted from libstd since std::ptr::is_aligned isn't stable until 1.79.0
// See https://github.com/rust-lang/rust/issues/96284
#[must_use]
#[inline]
@ -123,11 +125,18 @@ pub fn is_aligned<T>(ptr: *const T) -> bool
where
T: Sized,
{
let align = mem::align_of::<T>();
is_aligned_to(ptr, mem::align_of::<T>())
}
// Adapted from libstd since std::ptr::is_aligned_to is still unstable
// See https://github.com/rust-lang/rust/issues/96284
#[must_use]
#[inline]
pub fn is_aligned_to<T>(ptr: *const T, align: usize) -> bool {
addr(ptr) & (align - 1) == 0
}
// Adapted from libstd since std::ptr::addr is still unstable
// Adapted from libstd since std::ptr::addr isn't stable until 1.84.0
// See https://github.com/rust-lang/rust/issues/95228
#[must_use]
#[inline(always)]