| 1 | // Trying to satisfy clippy here is hopeless |
| 2 | #![allow (clippy::style)] |
| 3 | |
| 4 | #[allow (warnings)] |
| 5 | #[cfg (target_pointer_width = "16" )] |
| 6 | type c_int = i16; |
| 7 | #[allow (warnings)] |
| 8 | #[cfg (not(target_pointer_width = "16" ))] |
| 9 | type c_int = i32; |
| 10 | |
| 11 | // memcpy/memmove/memset have optimized implementations on some architectures |
| 12 | #[cfg_attr ( |
| 13 | all(not(feature = "no-asm" ), target_arch = "x86_64" ), |
| 14 | path = "x86_64.rs" |
| 15 | )] |
| 16 | mod impls; |
| 17 | |
| 18 | intrinsics! { |
| 19 | #[mem_builtin] |
| 20 | pub unsafe extern "C" fn memcpy(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 { |
| 21 | impls::copy_forward(dest, src, n); |
| 22 | dest |
| 23 | } |
| 24 | |
| 25 | #[mem_builtin] |
| 26 | pub unsafe extern "C" fn memmove(dest: *mut u8, src: *const u8, n: usize) -> *mut u8 { |
| 27 | let delta = (dest as usize).wrapping_sub(src as usize); |
| 28 | if delta >= n { |
| 29 | // We can copy forwards because either dest is far enough ahead of src, |
| 30 | // or src is ahead of dest (and delta overflowed). |
| 31 | impls::copy_forward(dest, src, n); |
| 32 | } else { |
| 33 | impls::copy_backward(dest, src, n); |
| 34 | } |
| 35 | dest |
| 36 | } |
| 37 | |
| 38 | #[mem_builtin] |
| 39 | pub unsafe extern "C" fn memset(s: *mut u8, c: crate::mem::c_int, n: usize) -> *mut u8 { |
| 40 | impls::set_bytes(s, c as u8, n); |
| 41 | s |
| 42 | } |
| 43 | |
| 44 | #[mem_builtin] |
| 45 | pub unsafe extern "C" fn memcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { |
| 46 | impls::compare_bytes(s1, s2, n) |
| 47 | } |
| 48 | |
| 49 | #[mem_builtin] |
| 50 | pub unsafe extern "C" fn bcmp(s1: *const u8, s2: *const u8, n: usize) -> i32 { |
| 51 | memcmp(s1, s2, n) |
| 52 | } |
| 53 | |
| 54 | #[mem_builtin] |
| 55 | pub unsafe extern "C" fn strlen(s: *const core::ffi::c_char) -> usize { |
| 56 | impls::c_string_length(s) |
| 57 | } |
| 58 | } |
| 59 | |