From 70c5af85e09be583128df5eda6b4de25a23387c1 Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 13 Feb 2019 13:46:45 -0800 Subject: [PATCH 1/2] 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 d0242ca7422..9b80ad8d9b2 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 @@ 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 @@ pub fn get(&self) -> Option<&F> { } 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, }; From 33d80bfaa0f2a4ca996a942e6d65a932e72fec1b Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Wed, 13 Feb 2019 14:07:08 -0800 Subject: [PATCH 2/2] Return without a reference in unix Weak::get() --- src/libstd/sys/unix/weak.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/libstd/sys/unix/weak.rs b/src/libstd/sys/unix/weak.rs index 9b80ad8d9b2..b60e241f10c 100644 --- a/src/libstd/sys/unix/weak.rs +++ b/src/libstd/sys/unix/weak.rs @@ -45,16 +45,15 @@ pub const fn new(name: &'static str) -> Weak { } } - pub fn get(&self) -> Option<&F> { + pub fn get(&self) -> Option { assert_eq!(mem::size_of::(), mem::size_of::()); unsafe { if self.addr.load(Ordering::SeqCst) == 1 { self.addr.store(fetch(self.name), Ordering::SeqCst); } - if self.addr.load(Ordering::SeqCst) == 0 { - None - } else { - mem::transmute::<&AtomicUsize, Option<&F>>(&self.addr) + match self.addr.load(Ordering::SeqCst) { + 0 => None, + addr => Some(mem::transmute_copy::(&addr)), } } }