Rollup merge of #100820 - WaffleLapkin:use_ptr_is_aligned_methods, r=scottmcm

Use pointer `is_aligned*` methods

This PR replaces some manual alignment checks with calls to `pointer::{is_aligned, is_aligned_to}` and removes a useless pointer cast.

r? `@scottmcm`

_split off from #100746_
This commit is contained in:
Dylan DPC 2022-08-22 20:34:15 +05:30 committed by GitHub
commit 58d23737a6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 10 additions and 8 deletions

View File

@ -148,7 +148,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
#[inline]
fn next(&mut self) -> Option<T> {
if self.ptr as *const _ == self.end {
if self.ptr == self.end {
None
} else if mem::size_of::<T>() == 0 {
// purposefully don't use 'ptr.offset' because for

View File

@ -38,6 +38,7 @@
#![feature(const_str_from_utf8)]
#![feature(nonnull_slice_from_raw_parts)]
#![feature(panic_update_hook)]
#![feature(pointer_is_aligned)]
#![feature(slice_flatten)]
#![feature(thin_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
// broken `ThinBox` impl, since it's an extremely subtle piece of code.
let ptr = core::hint::black_box(ptr);
let align = core::mem::align_of::<T>();
assert!(
(ptr.addr() & (align - 1)) == 0 && !ptr.is_null(),
"misaligned ThinBox data; valid pointers to `{}` should be aligned to {align}: {ptr:p}",
core::any::type_name::<T>(),
ptr.is_aligned() && !ptr.is_null(),
"misaligned ThinBox data; valid pointers to `{ty}` should be aligned to {align}: {ptr:p}",
ty = core::any::type_name::<T>(),
align = core::mem::align_of::<T>(),
);
}

View File

@ -2139,7 +2139,7 @@ const fn comptime() {}
/// Checks whether `ptr` is properly aligned with respect to
/// `align_of::<T>()`.
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

View File

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

View File

@ -117,7 +117,7 @@ unsafe fn from_raw_sized(ptr: *mut u8, size: usize) -> NonNull<Self> {
/// * the pointer is null.
/// * the pointed-to range is not in user memory.
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_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr })));
@ -386,7 +386,7 @@ unsafe fn copy_bytewise_to_userspace(src: *const u8, dst: *mut u8, len: usize) {
unsafe {
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
unsafe {
copy_quadwords(src, dst, len);