@@ -20,11 +20,6 @@ extern crate rustc_middle;
2020extern crate rustc_session;
2121extern crate rustc_span;
2222
23- /// See docs in https://github.com/rust-lang/rust/blob/HEAD/compiler/rustc/src/main.rs
24- /// and https://github.com/rust-lang/rust/pull/146627 for why we need this `use` statement.
25- #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
26- use tikv_jemalloc_sys as _;
27-
2823mod log;
2924
3025use std:: env;
@@ -397,7 +392,48 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
397392 Ok ( from..to)
398393}
399394
395+ #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
396+ fn jemalloc_magic ( ) {
397+ // These magic runes are copied from
398+ // <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
399+ // See there for further comments.
400+ use std:: os:: raw:: { c_int, c_void} ;
401+
402+ use tikv_jemalloc_sys as jemalloc_sys;
403+
404+ #[ used]
405+ static _F1: unsafe extern "C" fn ( usize , usize ) -> * mut c_void = jemalloc_sys:: calloc;
406+ #[ used]
407+ static _F2: unsafe extern "C" fn ( * mut * mut c_void , usize , usize ) -> c_int =
408+ jemalloc_sys:: posix_memalign;
409+ #[ used]
410+ static _F3: unsafe extern "C" fn ( usize , usize ) -> * mut c_void = jemalloc_sys:: aligned_alloc;
411+ #[ used]
412+ static _F4: unsafe extern "C" fn ( usize ) -> * mut c_void = jemalloc_sys:: malloc;
413+ #[ used]
414+ static _F5: unsafe extern "C" fn ( * mut c_void , usize ) -> * mut c_void = jemalloc_sys:: realloc;
415+ #[ used]
416+ static _F6: unsafe extern "C" fn ( * mut c_void ) = jemalloc_sys:: free;
417+
418+ // On OSX, jemalloc doesn't directly override malloc/free, but instead
419+ // registers itself with the allocator's zone APIs in a ctor. However,
420+ // the linker doesn't seem to consider ctors as "used" when statically
421+ // linking, so we need to explicitly depend on the function.
422+ #[ cfg( target_os = "macos" ) ]
423+ {
424+ unsafe extern "C" {
425+ fn _rjem_je_zone_register ( ) ;
426+ }
427+
428+ #[ used]
429+ static _F7: unsafe extern "C" fn ( ) = _rjem_je_zone_register;
430+ }
431+ }
432+
400433fn main ( ) {
434+ #[ cfg( any( target_os = "linux" , target_os = "macos" ) ) ]
435+ jemalloc_magic ( ) ;
436+
401437 let early_dcx = EarlyDiagCtxt :: new ( ErrorOutputType :: default ( ) ) ;
402438
403439 // Snapshot a copy of the environment before `rustc` starts messing with it.
0 commit comments