Skip to content

Commit 58d2373

Browse files
authored
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_
2 parents 75b7089 + efef211 commit 58d2373

File tree

6 files changed

+10
-8
lines changed

6 files changed

+10
-8
lines changed

library/alloc/src/vec/into_iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<T, A: Allocator> Iterator for IntoIter<T, A> {
148148

149149
#[inline]
150150
fn next(&mut self) -> Option<T> {
151-
if self.ptr as *const _ == self.end {
151+
if self.ptr == self.end {
152152
None
153153
} else if mem::size_of::<T>() == 0 {
154154
// purposefully don't use 'ptr.offset' because for

library/alloc/tests/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#![feature(const_str_from_utf8)]
3939
#![feature(nonnull_slice_from_raw_parts)]
4040
#![feature(panic_update_hook)]
41+
#![feature(pointer_is_aligned)]
4142
#![feature(slice_flatten)]
4243
#![feature(thin_box)]
4344
#![feature(bench_black_box)]

library/alloc/tests/thin_box.rs

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

library/core/src/intrinsics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2139,7 +2139,7 @@ pub(crate) use assert_unsafe_precondition;
21392139
/// Checks whether `ptr` is properly aligned with respect to
21402140
/// `align_of::<T>()`.
21412141
pub(crate) fn is_aligned_and_not_null<T>(ptr: *const T) -> bool {
2142-
!ptr.is_null() && ptr.addr() % mem::align_of::<T>() == 0
2142+
!ptr.is_null() && ptr.is_aligned()
21432143
}
21442144

21452145
/// Checks whether the regions of memory starting at `src` and `dst` of size

library/std/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@
296296
#![feature(panic_can_unwind)]
297297
#![feature(panic_info_message)]
298298
#![feature(panic_internals)]
299+
#![feature(pointer_is_aligned)]
299300
#![feature(portable_simd)]
300301
#![feature(prelude_2024)]
301302
#![feature(provide_any)]

library/std/src/sys/sgx/abi/usercalls/alloc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ pub unsafe trait UserSafe {
117117
/// * the pointer is null.
118118
/// * the pointed-to range is not in user memory.
119119
unsafe fn check_ptr(ptr: *const Self) {
120-
let is_aligned = |p: *const u8| -> bool { 0 == p.addr() & (Self::align_of() - 1) };
120+
let is_aligned = |p: *const u8| -> bool { p.is_aligned_to(Self::align_of()) };
121121

122122
assert!(is_aligned(ptr as *const u8));
123123
assert!(is_user_range(ptr as _, mem::size_of_val(unsafe { &*ptr })));
@@ -386,7 +386,7 @@ pub(crate) unsafe fn copy_to_userspace(src: *const u8, dst: *mut u8, len: usize)
386386
unsafe {
387387
copy_bytewise_to_userspace(src, dst, len);
388388
}
389-
} else if len % 8 == 0 && dst as usize % 8 == 0 {
389+
} else if len % 8 == 0 && dst.is_aligned_to(8) {
390390
// Copying 8-byte aligned quadwords: copy quad word per quad word
391391
unsafe {
392392
copy_quadwords(src, dst, len);

0 commit comments

Comments
 (0)