Skip to content

Commit a06c865

Browse files
Fix pointer alignment check in get_batch_aligned_raw
1 parent 11f76f0 commit a06c865

File tree

2 files changed

+12
-18
lines changed

2 files changed

+12
-18
lines changed

crates/bevy_ptr/src/batch.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,19 +305,13 @@ impl<'a, T> ThinSimdAlignedSlicePtr<'a, T> {
305305

306306
let off_ptr = self.ptr.as_ptr().add(index);
307307

308+
//NOTE: ZSTs may cause this "slice" to point into nothingness.
309+
//This sounds dangerous, but won't cause harm as nothing
310+
//will actually access anything "in the slice"
311+
312+
//TODO: when pointer_is_aligned is standardized, we can just use ptr::is_aligned()
308313
#[cfg(debug_assertions)]
309-
if core::mem::size_of::<T>() > 0 {
310-
//NOTE: ZSTs may cause this "slice" to point into nothingness.
311-
//A "batch" of ZSTs is itself a ZST.
312-
//This sounds dangerous, but won't cause harm as nothing
313-
//will actually access anything "in the slice".
314-
//ZSTs are trivially well aligned, so there is nothing to be checked.
315-
//align_offset will probably return MAX for these types which
316-
//can throw the test off.
317-
//When is_aligned is standardized, we can just use that.
318-
319-
debug_assert_eq!(off_ptr.align_offset(ALIGN), 0);
320-
}
314+
debug_assert_eq!(off_ptr as usize % ALIGN, 0);
321315

322316
//SAFETY: off_ptr is not null
323317
off_ptr as *const AlignedBatchT<T, N, ALIGN>

crates/bevy_ptr/src/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -220,13 +220,13 @@ impl<'a, T> ThinSimdAlignedSlicePtr<'a, T> {
220220
/// `ptr` must be aligned to at least `MAX_SIMD_ALIGNMENT`
221221
#[inline]
222222
pub unsafe fn new(ptr: *mut T, _len: usize) -> Self {
223+
//NOTE: ZSTs may cause this "slice" to point into nothingness.
224+
//This sounds dangerous, but won't cause harm as nothing
225+
//will actually access anything "in the slice"
226+
227+
//TODO: when pointer_is_aligned is standardized, we can just use ptr::is_aligned()
223228
#[cfg(debug_assertions)]
224-
if core::mem::size_of::<T>() > 0 {
225-
//NOTE: ZSTs may cause this "slice" to point into nothingness.
226-
//This sounds dangerous, but won't cause harm as nothing
227-
//will actually access anything "in the slice"
228-
debug_assert!(ptr.align_offset(batch::MAX_SIMD_ALIGNMENT) == 0);
229-
}
229+
debug_assert_eq!(ptr as usize % batch::MAX_SIMD_ALIGNMENT, 0);
230230

231231
Self {
232232
ptr: NonNull::new_unchecked(ptr),

0 commit comments

Comments
 (0)