Make use of pointer::is_aligned[_to]

This commit is contained in:
Maybe Waffle 2022-08-19 13:26:37 +04:00
parent ed084ba292
commit efef211876
5 changed files with 9 additions and 7 deletions

View File

@ -38,6 +38,7 @@
#![feature(const_str_from_utf8)] #![feature(const_str_from_utf8)]
#![feature(nonnull_slice_from_raw_parts)] #![feature(nonnull_slice_from_raw_parts)]
#![feature(panic_update_hook)] #![feature(panic_update_hook)]
#![feature(pointer_is_aligned)]
#![feature(slice_flatten)] #![feature(slice_flatten)]
#![feature(thin_box)] #![feature(thin_box)]
#![feature(bench_black_box)] #![feature(bench_black_box)]

View File

@ -48,11 +48,11 @@ fn verify_aligned<T>(ptr: *const T) {
// practice these checks are mostly just smoke-detectors for an extremely // practice these checks are mostly just smoke-detectors for an extremely
// broken `ThinBox` impl, since it's an extremely subtle piece of code. // broken `ThinBox` impl, since it's an extremely subtle piece of code.
let ptr = core::hint::black_box(ptr); let ptr = core::hint::black_box(ptr);
let align = core::mem::align_of::<T>();
assert!( assert!(
(ptr.addr() & (align - 1)) == 0 && !ptr.is_null(), ptr.is_aligned() && !ptr.is_null(),
"misaligned ThinBox data; valid pointers to `{}` should be aligned to {align}: {ptr:p}", "misaligned ThinBox data; valid pointers to `{ty}` should be aligned to {align}: {ptr:p}",
core::any::type_name::<T>(), ty = core::any::type_name::<T>(),
align = core::mem::align_of::<T>(),
); );
} }

View File

@ -2139,7 +2139,7 @@ pub(crate) use assert_unsafe_precondition;
/// Checks whether `ptr` is properly aligned with respect to /// Checks whether `ptr` is properly aligned with respect to
/// `align_of::<T>()`. /// `align_of::<T>()`.
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool { pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
!ptr.is_null() && ptr.addr() % mem::align_of::<T>() == 0 !ptr.is_null() && ptr.is_aligned()
} }
/// Checks whether the regions of memory starting at `src` and `dst` of size /// Checks whether the regions of memory starting at `src` and `dst` of size

View File

@ -284,6 +284,7 @@
#![feature(panic_can_unwind)] #![feature(panic_can_unwind)]
#![feature(panic_info_message)] #![feature(panic_info_message)]
#![feature(panic_internals)] #![feature(panic_internals)]
#![feature(pointer_is_aligned)]
#![feature(portable_simd)] #![feature(portable_simd)]
#![feature(prelude_2024)] #![feature(prelude_2024)]
#![feature(provide_any)] #![feature(provide_any)]

View File

@ -115,7 +115,7 @@ pub unsafe trait UserSafe {
/// * the pointer is null. /// * the pointer is null.
/// * the pointed-to range is not in user memory. /// * the pointed-to range is not in user memory.
unsafe fn check_ptr(ptr: *const Self) { unsafe fn check_ptr(ptr: *const Self) {
let is_aligned = |p: *const u8| -> bool { 0 == p.addr() & (Self::align_of() - 1) }; let is_aligned = |p: *const u8| -> bool { p.is_aligned_to(Self::align_of()) };
assert!(is_aligned(ptr as *const u8)); assert!(is_aligned(ptr as *const u8));
assert!(is_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr }))); assert!(is_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr })));
@ -367,7 +367,7 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
unsafe { unsafe {
copy_bytewise_to_userspace(src, dst, len); copy_bytewise_to_userspace(src, dst, len);
} }
} else if len % 8 == 0 && dst as usize % 8 == 0 { } else if len % 8 == 0 && dst.is_aligned_to(8) {
// Copying 8-byte aligned quadwords: copy quad word per quad word // Copying 8-byte aligned quadwords: copy quad word per quad word
unsafe { unsafe {
copy_aligned_quadwords_to_userspace(src, dst, len); copy_aligned_quadwords_to_userspace(src, dst, len);