From 71e468c19641edd869a44dc0632c9181e30bdb16 Mon Sep 17 00:00:00 2001 From: Lukasz Anforowicz Date: Fri, 25 Oct 2024 16:42:49 +0000 Subject: [PATCH] safety: introduce `ALIGN_MASK` based on `core::mem::align_of` Fixes #194, Closes #197 --- src/ascii.rs | 5 +++-- src/byteset/scalar.rs | 7 +++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/ascii.rs b/src/ascii.rs index 464da96..7ef6b5c 100644 --- a/src/ascii.rs +++ b/src/ascii.rs @@ -24,6 +24,8 @@ #[cfg(any(test, miri, not(target_arch = "x86_64")))] const USIZE_BYTES: usize = core::mem::size_of::(); #[cfg(any(test, miri, not(target_arch = "x86_64")))] +const ALIGN_MASK: usize = core::mem::align_of::() - 1; +#[cfg(any(test, miri, not(target_arch = "x86_64")))] const FALLBACK_LOOP_SIZE: usize = 2 * USIZE_BYTES; // This is a mask where the most significant bit of each byte in the usize @@ -53,7 +55,6 @@ pub fn first_non_ascii_byte(slice: &[u8]) -> usize { #[cfg(any(test, miri, not(target_arch = "x86_64")))] fn first_non_ascii_byte_fallback(slice: &[u8]) -> usize { - let align = USIZE_BYTES - 1; let start_ptr = slice.as_ptr(); let end_ptr = slice[slice.len()..].as_ptr(); let mut ptr = start_ptr; @@ -69,7 +70,7 @@ fn first_non_ascii_byte_fallback(slice: &[u8]) -> usize { return first_non_ascii_byte_mask(mask); } - ptr = ptr_add(ptr, USIZE_BYTES - (start_ptr as usize & align)); + ptr = ptr_add(ptr, USIZE_BYTES - (start_ptr as usize & ALIGN_MASK)); debug_assert!(ptr > start_ptr); debug_assert!(ptr_sub(end_ptr, USIZE_BYTES) >= start_ptr); if slice.len() >= FALLBACK_LOOP_SIZE { diff --git a/src/byteset/scalar.rs b/src/byteset/scalar.rs index 3e0c7ed..24dcf37 100644 --- a/src/byteset/scalar.rs +++ b/src/byteset/scalar.rs @@ -5,6 +5,7 @@ use core::{cmp, usize}; const USIZE_BYTES: usize = core::mem::size_of::(); +const ALIGN_MASK: usize = core::mem::align_of::() - 1; // The number of bytes to loop at in one iteration of memchr/memrchr. const LOOP_SIZE: usize = 2 * USIZE_BYTES; @@ -22,7 +23,6 @@ pub fn inv_memchr(n1: u8, haystack: &[u8]) -> Option { let vn1 = repeat_byte(n1); let confirm = |byte| byte != n1; let loop_size = cmp::min(LOOP_SIZE, haystack.len()); - let align = USIZE_BYTES - 1; let start_ptr = haystack.as_ptr(); unsafe { @@ -38,7 +38,7 @@ pub fn inv_memchr(n1: u8, haystack: &[u8]) -> Option { return forward_search(start_ptr, end_ptr, ptr, confirm); } - ptr = ptr.add(USIZE_BYTES - (start_ptr as usize & align)); + ptr = ptr.add(USIZE_BYTES - (start_ptr as usize & ALIGN_MASK)); debug_assert!(ptr > start_ptr); debug_assert!(end_ptr.sub(USIZE_BYTES) >= start_ptr); while loop_size == LOOP_SIZE && ptr <= end_ptr.sub(loop_size) { @@ -62,7 +62,6 @@ pub fn inv_memrchr(n1: u8, haystack: &[u8]) -> Option { let vn1 = repeat_byte(n1); let confirm = |byte| byte != n1; let loop_size = cmp::min(LOOP_SIZE, haystack.len()); - let align = USIZE_BYTES - 1; let start_ptr = haystack.as_ptr(); unsafe { @@ -78,7 +77,7 @@ pub fn inv_memrchr(n1: u8, haystack: &[u8]) -> Option { return reverse_search(start_ptr, end_ptr, ptr, confirm); } - ptr = ptr.sub(end_ptr as usize & align); + ptr = ptr.sub(end_ptr as usize & ALIGN_MASK); debug_assert!(start_ptr <= ptr && ptr <= end_ptr); while loop_size == LOOP_SIZE && ptr >= start_ptr.add(loop_size) { debug_assert_eq!(0, (ptr as usize) % USIZE_BYTES);