mirror of
https://github.com/rust-lang/rust.git
synced 2025-05-14 02:49:40 +00:00
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`.
This commit is contained in:
parent
e544947278
commit
70c5af85e0
@ -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<unsafe extern fn($($t),*) -> $ret> =
|
||||
::sys::weak::Weak::new(stringify!($name));
|
||||
::sys::weak::Weak::new(concat!(stringify!($name), '\0'));
|
||||
)
|
||||
}
|
||||
|
||||
@ -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,
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user