@@ -30,6 +30,7 @@ extern crate tracing;
3030// So if `rustc` was specified in Cargo.toml, this would spuriously rebuild crates.
3131//
3232// Dependencies listed in Cargo.toml do not need `extern crate`.
33+
3334extern crate rustc_ast;
3435extern crate rustc_ast_lowering;
3536extern crate rustc_ast_pretty;
@@ -60,6 +61,15 @@ extern crate rustc_trait_selection;
6061extern crate rustc_typeck;
6162extern crate test as testing;
6263
64+ #[ cfg( feature = "jemalloc" ) ]
65+ extern crate tikv_jemalloc_sys;
66+ #[ cfg( feature = "jemalloc" ) ]
67+ use tikv_jemalloc_sys as jemalloc_sys;
68+ #[ cfg( feature = "jemalloc" ) ]
69+ extern crate tikv_jemallocator;
70+ #[ cfg( feature = "jemalloc" ) ]
71+ use tikv_jemallocator as jemallocator;
72+
6373use std:: default:: Default ;
6474use std:: env;
6575use std:: process;
@@ -113,7 +123,48 @@ mod theme;
113123mod visit_ast;
114124mod visit_lib;
115125
126+ // See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
127+ // about jemallocator
128+ #[ cfg( feature = "jemalloc" ) ]
129+ #[ global_allocator]
130+ static ALLOC : jemallocator:: Jemalloc = jemallocator:: Jemalloc ;
131+
116132pub fn main ( ) {
133+ // See docs in https://github.com/rust-lang/rust/blob/master/compiler/rustc/src/main.rs
134+ // about jemalloc-sys
135+ #[ cfg( feature = "jemalloc" ) ]
136+ {
137+ use std:: os:: raw:: { c_int, c_void} ;
138+
139+ #[ used]
140+ static _F1: unsafe extern "C" fn ( usize , usize ) -> * mut c_void = jemalloc_sys:: calloc;
141+ #[ used]
142+ static _F2: unsafe extern "C" fn ( * mut * mut c_void , usize , usize ) -> c_int =
143+ jemalloc_sys:: posix_memalign;
144+ #[ used]
145+ static _F3: unsafe extern "C" fn ( usize , usize ) -> * mut c_void = jemalloc_sys:: aligned_alloc;
146+ #[ used]
147+ static _F4: unsafe extern "C" fn ( usize ) -> * mut c_void = jemalloc_sys:: malloc;
148+ #[ used]
149+ static _F5: unsafe extern "C" fn ( * mut c_void , usize ) -> * mut c_void = jemalloc_sys:: realloc;
150+ #[ used]
151+ static _F6: unsafe extern "C" fn ( * mut c_void ) = jemalloc_sys:: free;
152+
153+ // On OSX, jemalloc doesn't directly override malloc/free, but instead
154+ // registers itself with the allocator's zone APIs in a ctor. However,
155+ // the linker doesn't seem to consider ctors as "used" when statically
156+ // linking, so we need to explicitly depend on the function.
157+ #[ cfg( target_os = "macos" ) ]
158+ {
159+ extern "C" {
160+ fn _rjem_je_zone_register ( ) ;
161+ }
162+
163+ #[ used]
164+ static _F7: unsafe extern "C" fn ( ) = _rjem_je_zone_register;
165+ }
166+ }
167+
117168 rustc_driver:: set_sigpipe_handler ( ) ;
118169 rustc_driver:: install_ice_hook ( ) ;
119170
0 commit comments