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