Skip to content

Commit 70c5af8

Browse files
committed
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`.
1 parent e544947 commit 70c5af8

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

src/libstd/sys/unix/weak.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@
1818
1919
use libc;
2020

21-
use ffi::CString;
21+
use ffi::CStr;
2222
use marker;
2323
use mem;
2424
use sync::atomic::{AtomicUsize, Ordering};
2525

2626
macro_rules! weak {
2727
(fn $name:ident($($t:ty),*) -> $ret:ty) => (
2828
static $name: ::sys::weak::Weak<unsafe extern fn($($t),*) -> $ret> =
29-
::sys::weak::Weak::new(stringify!($name));
29+
::sys::weak::Weak::new(concat!(stringify!($name), '\0'));
3030
)
3131
}
3232

@@ -61,7 +61,7 @@ impl<F> Weak<F> {
6161
}
6262

6363
unsafe fn fetch(name: &str) -> usize {
64-
let name = match CString::new(name) {
64+
let name = match CStr::from_bytes_with_nul(name.as_bytes()) {
6565
Ok(cstr) => cstr,
6666
Err(..) => return 0,
6767
};

0 commit comments

Comments
 (0)