From 88ac7ace569c6ad8ac2b9300cdeeb0170a1f04c5 Mon Sep 17 00:00:00 2001 From: Arthur Carcano Date: Mon, 24 Jul 2023 16:09:15 +0200 Subject: [PATCH] Delete architecture-specific memchr code in std::sys Currently all architecture-specific memchr code is only used in `std::io`. Most of the actual `memchr` capacity exposed to the user through the slice API is instead implemented in core::slice::memchr. Hence this commit deletes memchr from std::sys[_common] and replace calls to it by calls to core::slice::memchr functions. This deletes (r)memchr from the list of symbols linked to libc. --- library/std/src/io/buffered/linewritershim.rs | 2 +- library/std/src/io/mod.rs | 2 +- library/std/src/sys/pal/hermit/memchr.rs | 1 - library/std/src/sys/pal/hermit/mod.rs | 1 - library/std/src/sys/pal/hermit/os.rs | 2 +- library/std/src/sys/pal/sgx/memchr.rs | 1 - library/std/src/sys/pal/sgx/mod.rs | 1 - library/std/src/sys/pal/solid/memchr.rs | 21 ----- library/std/src/sys/pal/solid/mod.rs | 1 - library/std/src/sys/pal/solid/os.rs | 4 +- library/std/src/sys/pal/teeos/mod.rs | 2 - library/std/src/sys/pal/uefi/mod.rs | 4 - library/std/src/sys/pal/unix/memchr.rs | 40 --------- library/std/src/sys/pal/unix/mod.rs | 1 - library/std/src/sys/pal/unix/os.rs | 2 +- library/std/src/sys/pal/unsupported/common.rs | 4 - library/std/src/sys/pal/wasi/os.rs | 2 +- library/std/src/sys/pal/windows/memchr.rs | 5 -- library/std/src/sys/pal/windows/mod.rs | 1 - library/std/src/sys/pal/zkvm/mod.rs | 4 - library/std/src/sys_common/memchr.rs | 51 ----------- library/std/src/sys_common/memchr/tests.rs | 86 ------------------- library/std/src/sys_common/mod.rs | 1 - 23 files changed, 8 insertions(+), 231 deletions(-) delete mode 100644 library/std/src/sys/pal/hermit/memchr.rs delete mode 100644 library/std/src/sys/pal/sgx/memchr.rs delete mode 100644 library/std/src/sys/pal/solid/memchr.rs delete mode 100644 library/std/src/sys/pal/unix/memchr.rs delete mode 100644 library/std/src/sys/pal/windows/memchr.rs delete mode 100644 library/std/src/sys_common/memchr.rs delete mode 100644 library/std/src/sys_common/memchr/tests.rs diff --git a/library/std/src/io/buffered/linewritershim.rs b/library/std/src/io/buffered/linewritershim.rs index f2a55da05b22e..7eadfd413661d 100644 --- a/library/std/src/io/buffered/linewritershim.rs +++ b/library/std/src/io/buffered/linewritershim.rs @@ -1,5 +1,5 @@ use crate::io::{self, BufWriter, IoSlice, Write}; -use crate::sys_common::memchr; +use core::slice::memchr; /// Private helper struct for implementing the line-buffered writing logic. /// This shim temporarily wraps a BufWriter, and uses its internals to diff --git a/library/std/src/io/mod.rs b/library/std/src/io/mod.rs index 102db62fced34..f35bffd6a3ca7 100644 --- a/library/std/src/io/mod.rs +++ b/library/std/src/io/mod.rs @@ -304,7 +304,7 @@ use crate::ops::{Deref, DerefMut}; use crate::slice; use crate::str; use crate::sys; -use crate::sys_common::memchr; +use core::slice::memchr; #[stable(feature = "bufwriter_into_parts", since = "1.56.0")] pub use self::buffered::WriterPanicked; diff --git a/library/std/src/sys/pal/hermit/memchr.rs b/library/std/src/sys/pal/hermit/memchr.rs deleted file mode 100644 index 9967482197eb3..0000000000000 --- a/library/std/src/sys/pal/hermit/memchr.rs +++ /dev/null @@ -1 +0,0 @@ -pub use core::slice::memchr::{memchr, memrchr}; diff --git a/library/std/src/sys/pal/hermit/mod.rs b/library/std/src/sys/pal/hermit/mod.rs index ada408107dc36..910935541bd33 100644 --- a/library/std/src/sys/pal/hermit/mod.rs +++ b/library/std/src/sys/pal/hermit/mod.rs @@ -25,7 +25,6 @@ pub mod fs; pub mod futex; #[path = "../unsupported/io.rs"] pub mod io; -pub mod memchr; pub mod net; pub mod os; #[path = "../unsupported/pipe.rs"] diff --git a/library/std/src/sys/pal/hermit/os.rs b/library/std/src/sys/pal/hermit/os.rs index a54536aecb8b2..c26c275f9311b 100644 --- a/library/std/src/sys/pal/hermit/os.rs +++ b/library/std/src/sys/pal/hermit/os.rs @@ -9,9 +9,9 @@ use crate::os::hermit::ffi::OsStringExt; use crate::path::{self, PathBuf}; use crate::str; use crate::sync::Mutex; -use crate::sys::memchr; use crate::sys::unsupported; use crate::vec; +use core::slice::memchr; pub fn errno() -> i32 { 0 diff --git a/library/std/src/sys/pal/sgx/memchr.rs b/library/std/src/sys/pal/sgx/memchr.rs deleted file mode 100644 index 9967482197eb3..0000000000000 --- a/library/std/src/sys/pal/sgx/memchr.rs +++ /dev/null @@ -1 +0,0 @@ -pub use core::slice::memchr::{memchr, memrchr}; diff --git a/library/std/src/sys/pal/sgx/mod.rs b/library/std/src/sys/pal/sgx/mod.rs index 8ef3495884f2a..76f930b86f2ec 100644 --- a/library/std/src/sys/pal/sgx/mod.rs +++ b/library/std/src/sys/pal/sgx/mod.rs @@ -17,7 +17,6 @@ pub mod fd; pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; -pub mod memchr; pub mod net; pub mod os; #[path = "../unsupported/pipe.rs"] diff --git a/library/std/src/sys/pal/solid/memchr.rs b/library/std/src/sys/pal/solid/memchr.rs deleted file mode 100644 index 452b7a3de1b33..0000000000000 --- a/library/std/src/sys/pal/solid/memchr.rs +++ /dev/null @@ -1,21 +0,0 @@ -pub fn memchr(needle: u8, haystack: &[u8]) -> Option { - let p = unsafe { - libc::memchr( - haystack.as_ptr() as *const libc::c_void, - needle as libc::c_int, - haystack.len(), - ) - }; - if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) } -} - -pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { - let p = unsafe { - libc::memrchr( - haystack.as_ptr() as *const libc::c_void, - needle as libc::c_int, - haystack.len(), - ) - }; - if p.is_null() { None } else { Some(p as usize - (haystack.as_ptr() as usize)) } -} diff --git a/library/std/src/sys/pal/solid/mod.rs b/library/std/src/sys/pal/solid/mod.rs index 9ada7d130f050..109ee1a0ab666 100644 --- a/library/std/src/sys/pal/solid/mod.rs +++ b/library/std/src/sys/pal/solid/mod.rs @@ -33,7 +33,6 @@ pub mod pipe; pub mod process; pub mod stdio; pub use self::itron::thread; -pub mod memchr; pub mod thread_local_dtor; pub mod thread_local_key; pub use self::itron::thread_parking; diff --git a/library/std/src/sys/pal/solid/os.rs b/library/std/src/sys/pal/solid/os.rs index 5ceab3b27e0ae..ef35d8788a236 100644 --- a/library/std/src/sys/pal/solid/os.rs +++ b/library/std/src/sys/pal/solid/os.rs @@ -12,7 +12,9 @@ use crate::sync::{PoisonError, RwLock}; use crate::sys::common::small_c_string::run_with_cstr; use crate::vec; -use super::{error, itron, memchr}; +use super::{error, itron}; + +use core::slice::memchr; // `solid` directly maps `errno`s to μITRON error codes. impl itron::error::ItronError { diff --git a/library/std/src/sys/pal/teeos/mod.rs b/library/std/src/sys/pal/teeos/mod.rs index 51ef96a69a0f9..1fb9d5438dee1 100644 --- a/library/std/src/sys/pal/teeos/mod.rs +++ b/library/std/src/sys/pal/teeos/mod.rs @@ -18,8 +18,6 @@ pub mod env; pub mod fs; #[path = "../unsupported/io.rs"] pub mod io; -#[path = "../unix/memchr.rs"] -pub mod memchr; pub mod net; #[path = "../unsupported/once.rs"] pub mod once; diff --git a/library/std/src/sys/pal/uefi/mod.rs b/library/std/src/sys/pal/uefi/mod.rs index ff8e3bd32adfe..7c5b37fb4900c 100644 --- a/library/std/src/sys/pal/uefi/mod.rs +++ b/library/std/src/sys/pal/uefi/mod.rs @@ -48,10 +48,6 @@ use crate::os::uefi; use crate::ptr::NonNull; use crate::sync::atomic::{AtomicPtr, Ordering}; -pub mod memchr { - pub use core::slice::memchr::{memchr, memrchr}; -} - static EXIT_BOOT_SERVICE_EVENT: AtomicPtr = AtomicPtr::new(crate::ptr::null_mut()); diff --git a/library/std/src/sys/pal/unix/memchr.rs b/library/std/src/sys/pal/unix/memchr.rs deleted file mode 100644 index 73ba604eccba2..0000000000000 --- a/library/std/src/sys/pal/unix/memchr.rs +++ /dev/null @@ -1,40 +0,0 @@ -// Original implementation taken from rust-memchr. -// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch - -pub fn memchr(needle: u8, haystack: &[u8]) -> Option { - let p = unsafe { - libc::memchr( - haystack.as_ptr() as *const libc::c_void, - needle as libc::c_int, - haystack.len(), - ) - }; - if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) } -} - -pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { - #[cfg(target_os = "linux")] - fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { - // GNU's memrchr() will - unlike memchr() - error if haystack is empty. - if haystack.is_empty() { - return None; - } - let p = unsafe { - libc::memrchr( - haystack.as_ptr() as *const libc::c_void, - needle as libc::c_int, - haystack.len(), - ) - }; - // FIXME: this should *likely* use `offset_from`, but more - // investigation is needed (including running tests in miri). - if p.is_null() { None } else { Some(p.addr() - haystack.as_ptr().addr()) } - } - - #[cfg(not(target_os = "linux"))] - fn memrchr_specific(needle: u8, haystack: &[u8]) -> Option { - core::slice::memchr::memrchr(needle, haystack) - } - - memrchr_specific(needle, haystack) -} diff --git a/library/std/src/sys/pal/unix/mod.rs b/library/std/src/sys/pal/unix/mod.rs index 04b8c5ca91604..aac3315f645a6 100644 --- a/library/std/src/sys/pal/unix/mod.rs +++ b/library/std/src/sys/pal/unix/mod.rs @@ -20,7 +20,6 @@ pub mod io; pub mod kernel_copy; #[cfg(target_os = "l4re")] mod l4re; -pub mod memchr; #[cfg(not(target_os = "l4re"))] pub mod net; #[cfg(target_os = "l4re")] diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index af2b9db46854c..0b9c8027e6fc3 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -21,8 +21,8 @@ use crate::sync::{PoisonError, RwLock}; use crate::sys::common::small_c_string::{run_path_with_cstr, run_with_cstr}; use crate::sys::cvt; use crate::sys::fd; -use crate::sys::memchr; use crate::vec; +use core::slice::memchr; #[cfg(all(target_env = "gnu", not(target_os = "vxworks")))] use crate::sys::weak::weak; diff --git a/library/std/src/sys/pal/unsupported/common.rs b/library/std/src/sys/pal/unsupported/common.rs index 5c379992b2041..4f44db610af15 100644 --- a/library/std/src/sys/pal/unsupported/common.rs +++ b/library/std/src/sys/pal/unsupported/common.rs @@ -1,9 +1,5 @@ use crate::io as std_io; -pub mod memchr { - pub use core::slice::memchr::{memchr, memrchr}; -} - // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {} diff --git a/library/std/src/sys/pal/wasi/os.rs b/library/std/src/sys/pal/wasi/os.rs index d62ff8a2f1822..ee377b6ef791d 100644 --- a/library/std/src/sys/pal/wasi/os.rs +++ b/library/std/src/sys/pal/wasi/os.rs @@ -10,9 +10,9 @@ use crate::os::wasi::prelude::*; use crate::path::{self, PathBuf}; use crate::str; use crate::sys::common::small_c_string::{run_path_with_cstr, run_with_cstr}; -use crate::sys::memchr; use crate::sys::unsupported; use crate::vec; +use core::slice::memchr; // Add a few symbols not in upstream `libc` just yet. mod libc { diff --git a/library/std/src/sys/pal/windows/memchr.rs b/library/std/src/sys/pal/windows/memchr.rs deleted file mode 100644 index b9e5bcc1b4bbd..0000000000000 --- a/library/std/src/sys/pal/windows/memchr.rs +++ /dev/null @@ -1,5 +0,0 @@ -// Original implementation taken from rust-memchr. -// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch - -// Fallback memchr is fastest on Windows. -pub use core::slice::memchr::{memchr, memrchr}; diff --git a/library/std/src/sys/pal/windows/mod.rs b/library/std/src/sys/pal/windows/mod.rs index b47d213df343a..831cb04c566d8 100644 --- a/library/std/src/sys/pal/windows/mod.rs +++ b/library/std/src/sys/pal/windows/mod.rs @@ -19,7 +19,6 @@ pub mod env; pub mod fs; pub mod handle; pub mod io; -pub mod memchr; pub mod net; pub mod os; pub mod pipe; diff --git a/library/std/src/sys/pal/zkvm/mod.rs b/library/std/src/sys/pal/zkvm/mod.rs index 016c977dc33d6..6c714f76309a3 100644 --- a/library/std/src/sys/pal/zkvm/mod.rs +++ b/library/std/src/sys/pal/zkvm/mod.rs @@ -43,10 +43,6 @@ mod abi; use crate::io as std_io; -pub mod memchr { - pub use core::slice::memchr::{memchr, memrchr}; -} - // SAFETY: must be called only once during runtime initialization. // NOTE: this is not guaranteed to run, for example when Rust code is called externally. pub unsafe fn init(_argc: isize, _argv: *const *const u8, _sigpipe: u8) {} diff --git a/library/std/src/sys_common/memchr.rs b/library/std/src/sys_common/memchr.rs deleted file mode 100644 index b219e87891264..0000000000000 --- a/library/std/src/sys_common/memchr.rs +++ /dev/null @@ -1,51 +0,0 @@ -// Original implementation taken from rust-memchr. -// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch - -use crate::sys::memchr as sys; - -#[cfg(test)] -mod tests; - -/// A safe interface to `memchr`. -/// -/// Returns the index corresponding to the first occurrence of `needle` in -/// `haystack`, or `None` if one is not found. -/// -/// memchr reduces to super-optimized machine code at around an order of -/// magnitude faster than `haystack.iter().position(|&b| b == needle)`. -/// (See benchmarks.) -/// -/// # Examples -/// -/// This shows how to find the first position of a byte in a byte string. -/// -/// ```ignore (cannot-doctest-private-modules) -/// use memchr::memchr; -/// -/// let haystack = b"the quick brown fox"; -/// assert_eq!(memchr(b'k', haystack), Some(8)); -/// ``` -#[inline] -pub fn memchr(needle: u8, haystack: &[u8]) -> Option { - sys::memchr(needle, haystack) -} - -/// A safe interface to `memrchr`. -/// -/// Returns the index corresponding to the last occurrence of `needle` in -/// `haystack`, or `None` if one is not found. -/// -/// # Examples -/// -/// This shows how to find the last position of a byte in a byte string. -/// -/// ```ignore (cannot-doctest-private-modules) -/// use memchr::memrchr; -/// -/// let haystack = b"the quick brown fox"; -/// assert_eq!(memrchr(b'o', haystack), Some(17)); -/// ``` -#[inline] -pub fn memrchr(needle: u8, haystack: &[u8]) -> Option { - sys::memrchr(needle, haystack) -} diff --git a/library/std/src/sys_common/memchr/tests.rs b/library/std/src/sys_common/memchr/tests.rs deleted file mode 100644 index 557d749c7f63e..0000000000000 --- a/library/std/src/sys_common/memchr/tests.rs +++ /dev/null @@ -1,86 +0,0 @@ -// Original implementation taken from rust-memchr. -// Copyright 2015 Andrew Gallant, bluss and Nicolas Koch - -// test the implementations for the current platform -use super::{memchr, memrchr}; - -#[test] -fn matches_one() { - assert_eq!(Some(0), memchr(b'a', b"a")); -} - -#[test] -fn matches_begin() { - assert_eq!(Some(0), memchr(b'a', b"aaaa")); -} - -#[test] -fn matches_end() { - assert_eq!(Some(4), memchr(b'z', b"aaaaz")); -} - -#[test] -fn matches_nul() { - assert_eq!(Some(4), memchr(b'\x00', b"aaaa\x00")); -} - -#[test] -fn matches_past_nul() { - assert_eq!(Some(5), memchr(b'z', b"aaaa\x00z")); -} - -#[test] -fn no_match_empty() { - assert_eq!(None, memchr(b'a', b"")); -} - -#[test] -fn no_match() { - assert_eq!(None, memchr(b'a', b"xyz")); -} - -#[test] -fn matches_one_reversed() { - assert_eq!(Some(0), memrchr(b'a', b"a")); -} - -#[test] -fn matches_begin_reversed() { - assert_eq!(Some(3), memrchr(b'a', b"aaaa")); -} - -#[test] -fn matches_end_reversed() { - assert_eq!(Some(0), memrchr(b'z', b"zaaaa")); -} - -#[test] -fn matches_nul_reversed() { - assert_eq!(Some(4), memrchr(b'\x00', b"aaaa\x00")); -} - -#[test] -fn matches_past_nul_reversed() { - assert_eq!(Some(0), memrchr(b'z', b"z\x00aaaa")); -} - -#[test] -fn no_match_empty_reversed() { - assert_eq!(None, memrchr(b'a', b"")); -} - -#[test] -fn no_match_reversed() { - assert_eq!(None, memrchr(b'a', b"xyz")); -} - -#[test] -fn each_alignment() { - let mut data = [1u8; 64]; - let needle = 2; - let pos = 40; - data[pos] = needle; - for start in 0..16 { - assert_eq!(Some(pos - start), memchr(needle, &data[start..])); - } -} diff --git a/library/std/src/sys_common/mod.rs b/library/std/src/sys_common/mod.rs index 01f83ecb41452..c9025a81bf3df 100644 --- a/library/std/src/sys_common/mod.rs +++ b/library/std/src/sys_common/mod.rs @@ -24,7 +24,6 @@ pub mod backtrace; pub mod fs; pub mod io; pub mod lazy_box; -pub mod memchr; pub mod once; pub mod process; pub mod thread;