Skip to content

Commit

Permalink
Avoid allocation in std::sys::unix::weak
Browse files Browse the repository at this point in the history
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`.
  • Loading branch information
cuviper committed Feb 13, 2019
1 parent e544947 commit 70c5af8
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/libstd/sys/unix/weak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
use libc;

use ffi::CString;
use ffi::CStr;
use marker;
use mem;
use sync::atomic::{AtomicUsize, Ordering};

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'));
)
}

Expand Down Expand Up @@ -61,7 +61,7 @@ impl<F> Weak<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,
};
Expand Down

0 comments on commit 70c5af8

Please sign in to comment.