Skip to content

Commit de9da0b

Browse files
committed
Make use of pointer::is_aligned[_to]
1 parent ed084ba commit de9da0b

File tree

4 files changed

+8
-7
lines changed

4 files changed

+8
-7
lines changed

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/sys/sgx/abi/usercalls/alloc.rs

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

120120
assert!(is_aligned(ptr as *const u8));
121121
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)
367367
unsafe {
368368
copy_bytewise_to_userspace(src, dst, len);
369369
}
370-
} else if len % 8 == 0 && dst as usize % 8 == 0 {
370+
} else if len % 8 == 0 && dst.is_aligned_to(8) {
371371
// Copying 8-byte aligned quadwords: copy quad word per quad word
372372
unsafe {
373373
copy_aligned_quadwords_to_userspace(src, dst, len);

0 commit comments

Comments
 (0)