std/sys/pal/common/
small_c_string.rs
1use crate::ffi::{CStr, CString};
2use crate::mem::MaybeUninit;
3use crate::path::Path;
4use crate::{io, ptr, slice};
5
6#[cfg(not(target_os = "espidf"))]
9const MAX_STACK_ALLOCATION: usize = 384;
10#[cfg(target_os = "espidf")]
11const MAX_STACK_ALLOCATION: usize = 32;
12
13const NUL_ERR: io::Error =
14 io::const_error!(io::ErrorKind::InvalidInput, "file name contained an unexpected NUL byte");
15
16#[inline]
17pub fn run_path_with_cstr<T>(path: &Path, f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> {
18 run_with_cstr(path.as_os_str().as_encoded_bytes(), f)
19}
20
21#[inline]
22pub fn run_with_cstr<T>(bytes: &[u8], f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> {
23 if bytes.len() >= MAX_STACK_ALLOCATION {
26 run_with_cstr_allocating(bytes, f)
27 } else {
28 unsafe { run_with_cstr_stack(bytes, f) }
29 }
30}
31
32unsafe fn run_with_cstr_stack<T>(
36 bytes: &[u8],
37 f: &dyn Fn(&CStr) -> io::Result<T>,
38) -> io::Result<T> {
39 let mut buf = MaybeUninit::<[u8; MAX_STACK_ALLOCATION]>::uninit();
40 let buf_ptr = buf.as_mut_ptr() as *mut u8;
41
42 unsafe {
43 ptr::copy_nonoverlapping(bytes.as_ptr(), buf_ptr, bytes.len());
44 buf_ptr.add(bytes.len()).write(0);
45 }
46
47 match CStr::from_bytes_with_nul(unsafe { slice::from_raw_parts(buf_ptr, bytes.len() + 1) }) {
48 Ok(s) => f(s),
49 Err(_) => Err(NUL_ERR),
50 }
51}
52
53#[cold]
54#[inline(never)]
55fn run_with_cstr_allocating<T>(bytes: &[u8], f: &dyn Fn(&CStr) -> io::Result<T>) -> io::Result<T> {
56 match CString::new(bytes) {
57 Ok(s) => f(&s),
58 Err(_) => Err(NUL_ERR),
59 }
60}