]> git.lizzy.rs Git - rust.git/commitdiff
Avoid allocation in std::sys::unix::weak
authorJosh Stone <jistone@redhat.com>
Wed, 13 Feb 2019 21:46:45 +0000 (13:46 -0800)
committerJosh Stone <jistone@redhat.com>
Wed, 13 Feb 2019 21:46:45 +0000 (13:46 -0800)
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

index d0242ca74229a79f431f22328fab82425dc5bb64..9b80ad8d9b27f1feee1e7c2961fb23b94dbb78c3 100644 (file)
@@ -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<unsafe extern fn($($t),*) -> $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,
     };