]> git.proxmox.com Git - rustc.git/blame - compiler/rustc/src/main.rs
New upstream version 1.66.0+dfsg1
[rustc.git] / compiler / rustc / src / main.rs
CommitLineData
2b03887a
FG
1#![feature(unix_sigpipe)]
2
a2a8927a
XL
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.
cdc7bbd5 10//
a2a8927a
XL
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
15// jemalloc.
16//
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.
21//
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.
cdc7bbd5 27
2b03887a 28#[unix_sigpipe = "sig_dfl"]
83c7162d 29fn main() {
a2a8927a 30 // See the comment at the top of this file for an explanation of this.
923072b8 31 #[cfg(feature = "jemalloc-sys")]
0731742a 32 {
dfeec247 33 use std::os::raw::{c_int, c_void};
0731742a
XL
34
35 #[used]
dfeec247 36 static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
0731742a 37 #[used]
dfeec247 38 static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
0731742a
XL
39 jemalloc_sys::posix_memalign;
40 #[used]
dfeec247 41 static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
0731742a 42 #[used]
dfeec247 43 static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
0731742a 44 #[used]
dfeec247 45 static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
0731742a 46 #[used]
dfeec247 47 static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
6a06907d
XL
48
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")]
54 {
55 extern "C" {
56 fn _rjem_je_zone_register();
57 }
58
59 #[used]
60 static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
61 }
0731742a
XL
62 }
63
83c7162d
XL
64 rustc_driver::main()
65}