Skip to content

Commit d194c70

Browse files
committed
Use jemalloc
1 parent cecd7a9 commit d194c70

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ smallvec = "1.6.1"
3535
#gimli = { path = "../" }
3636

3737
[features]
38-
default = ["jit", "inline_asm"]
38+
default = ["jit", "inline_asm", "jemalloc"]
3939
jit = ["cranelift-jit", "libloading"]
4040
inline_asm = []
41+
jemalloc = []
4142

4243
[profile.dev]
4344
# By compiling dependencies with optimizations, performing tests gets much faster.

src/bin/cg_clif.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![feature(rustc_private)]
22

3+
#[cfg(feature = "jemalloc")]
4+
extern crate jemalloc_sys;
35
extern crate rustc_data_structures;
46
extern crate rustc_driver;
57
extern crate rustc_interface;
@@ -33,6 +35,46 @@ impl rustc_driver::Callbacks for CraneliftPassesCallbacks {
3335
}
3436

3537
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+
3678
let start_time = std::time::Instant::now();
3779
let start_rss = get_resident_set_size();
3880
rustc_driver::init_rustc_env_logger();

0 commit comments

Comments
 (0)