|
1 | 1 | #![feature(rustc_private)]
|
2 | 2 |
|
| 3 | +#[cfg(feature = "jemalloc")] |
| 4 | +extern crate jemalloc_sys; |
3 | 5 | extern crate rustc_data_structures;
|
4 | 6 | extern crate rustc_driver;
|
5 | 7 | extern crate rustc_interface;
|
@@ -33,6 +35,46 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
|
33 | 35 | }
|
34 | 36 |
|
35 | 37 | fn main() {
|
| 38 | + // Pull in jemalloc when enabled. |
| 39 | + // |
| 40 | + // Note that we're pulling in a static copy of jemalloc which means that to |
| 41 | + // pull it in we need to actually reference its symbols for it to get |
| 42 | + // linked. The two crates we link to here, std and rustc_driver, are both |
| 43 | + // dynamic libraries. That means to pull in jemalloc we need to actually |
| 44 | + // reference allocation symbols one way or another (as this file is the only |
| 45 | + // object code in the rustc executable). |
| 46 | + #[cfg(feature = "jemalloc")] |
| 47 | + { |
| 48 | + use std::os::raw::{c_int, c_void}; |
| 49 | + #[used] |
| 50 | + static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc; |
| 51 | + #[used] |
| 52 | + static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = |
| 53 | + jemalloc_sys::posix_memalign; |
| 54 | + #[used] |
| 55 | + static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc; |
| 56 | + #[used] |
| 57 | + static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc; |
| 58 | + #[used] |
| 59 | + static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc; |
| 60 | + #[used] |
| 61 | + static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free; |
| 62 | + |
| 63 | + // On OSX, jemalloc doesn't directly override malloc/free, but instead |
| 64 | + // registers itself with the allocator's zone APIs in a ctor. However, |
| 65 | + // the linker doesn't seem to consider ctors as "used" when statically |
| 66 | + // linking, so we need to explicitly depend on the function. |
| 67 | + #[cfg(target_os = "macos")] |
| 68 | + { |
| 69 | + extern "C" { |
| 70 | + fn _rjem_je_zone_register(); |
| 71 | + } |
| 72 | + |
| 73 | + #[used] |
| 74 | + static _F7: unsafe extern "C" fn() = _rjem_je_zone_register; |
| 75 | + } |
| 76 | + } |
| 77 | + |
36 | 78 | let start_time = std::time::Instant::now();
|
37 | 79 | let start_rss = get_resident_set_size();
|
38 | 80 | rustc_driver::init_rustc_env_logger();
|
|
0 commit comments