1 #![feature(unix_sigpipe)]
3 // A note about jemalloc: rustc uses jemalloc when built for CI and
4 // distribution. The obvious way to do this is with the `#[global_allocator]`
5 // mechanism. However, for complicated reasons (see
6 // https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some
7 // details) that mechanism doesn't work here. Also, we must use a consistent
8 // allocator across the rustc <-> llvm boundary, and `#[global_allocator]`
9 // wouldn't provide that.
11 // Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a
12 // way such that jemalloc's implementation of `malloc`, `free`, etc., override
13 // the libc allocator's implementation. This means that Rust's `System`
14 // allocator, which calls `libc::malloc()` et al., is actually calling into
17 // A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate
18 // provides an impl of that trait, which is called `Jemalloc`) is that we
19 // cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides.
20 // It's unclear how much performance is lost because of this.
22 // As for the symbol overrides in `main` below: we're pulling in a static copy
23 // of jemalloc. We need to actually reference its symbols for it to get linked.
24 // The two crates we link to here, `std` and `rustc_driver`, are both dynamic
25 // libraries. So we must reference jemalloc symbols one way or another, because
26 // this file is the only object code in the rustc executable.
28 #[unix_sigpipe = "sig_dfl"]
30 // See the comment at the top of this file for an explanation of this.
31 #[cfg(feature = "jemalloc-sys")]
33 use std::os::raw::{c_int, c_void};
36 static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
38 static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
39 jemalloc_sys::posix_memalign;
41 static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
43 static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
45 static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
47 static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
49 // On OSX, jemalloc doesn't directly override malloc/free, but instead
50 // registers itself with the allocator's zone APIs in a ctor. However,
51 // the linker doesn't seem to consider ctors as "used" when statically
52 // linking, so we need to explicitly depend on the function.
53 #[cfg(target_os = "macos")]
56 fn _rjem_je_zone_register();
60 static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;