From 70c5af85e09be583128df5eda6b4de25a23387c1 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 13 Feb 2019 13:46:45 -0800 Subject: [PATCH] Avoid allocation in std::sys::unix::weak If we add a terminating NUL to the name in the `weak!` macro, then `fetch()` can use `CStr::from_bytes_with_nul()` instead of `CString`. --- src/libstd/sys/unix/weak.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libstd/sys/unix/weak.rs b/src/libstd/sys/unix/weak.rs index d0242ca74229..9b80ad8d9b27 100644 --- a/src/libstd/sys/unix/weak.rs +++ b/src/libstd/sys/unix/weak.rs @@ -18,7 +18,7 @@ use libc; -use ffi::CString; +use ffi::CStr; use marker; use mem; use sync::atomic::{AtomicUsize, Ordering}; @@ -26,7 +26,7 @@ use sync::atomic::{AtomicUsize, Ordering}; macro_rules! weak { (fn $name:ident($($t:ty),*) -> $ret:ty) => ( static $name: ::sys::weak::Weak $ret> = - ::sys::weak::Weak::new(stringify!($name)); + ::sys::weak::Weak::new(concat!(stringify!($name), '\0')); ) } @@ -61,7 +61,7 @@ impl Weak { } unsafe fn fetch(name: &str) -> usize { - let name = match CString::new(name) { + let name = match CStr::from_bytes_with_nul(name.as_bytes()) { Ok(cstr) => cstr, Err(..) => return 0, };