Skip to content

Commit bff9aae

Browse files
committed
Auto merge of #3259 - RalfJung:jemalloc, r=RalfJung
use jemalloc as global allocator Hopefully fixes #3255
2 parents 312f174 + c26b5d6 commit bff9aae

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

Cargo.lock

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,14 @@ log = "0.4"
2424
rand = "0.8"
2525
smallvec = "1.7"
2626
aes = { version = "0.8.3", features = ["hazmat"] }
27-
2827
measureme = "10.0.0"
2928
ctrlc = "3.2.5"
3029

30+
# Copied from `compiler/rustc/Cargo.toml`.
31+
[dependencies.jemalloc-sys]
32+
version = "0.5.0"
33+
features = ['unprefixed_malloc_on_supported_platforms']
34+
3135
[target.'cfg(unix)'.dependencies]
3236
libc = "0.2"
3337

src/bin/miri.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -293,13 +293,49 @@ fn run_compiler(
293293
}
294294

295295
/// Parses a comma separated list of `T` from the given string:
296-
///
297296
/// `<value1>,<value2>,<value3>,...`
298297
fn parse_comma_list<T: FromStr>(input: &str) -> Result<Vec<T>, T::Err> {
299298
input.split(',').map(str::parse::<T>).collect()
300299
}
301300

301+
fn jemalloc_magic() {
302+
// These magic runes are copied from
303+
// <https://github.com/rust-lang/rust/blob/e89bd9428f621545c979c0ec686addc6563a394e/compiler/rustc/src/main.rs#L39>.
304+
// See there for further comments.
305+
use std::os::raw::{c_int, c_void};
306+
307+
#[used]
308+
static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc;
309+
#[used]
310+
static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int =
311+
jemalloc_sys::posix_memalign;
312+
#[used]
313+
static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc;
314+
#[used]
315+
static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc;
316+
#[used]
317+
static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc;
318+
#[used]
319+
static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free;
320+
321+
// On OSX, jemalloc doesn't directly override malloc/free, but instead
322+
// registers itself with the allocator's zone APIs in a ctor. However,
323+
// the linker doesn't seem to consider ctors as "used" when statically
324+
// linking, so we need to explicitly depend on the function.
325+
#[cfg(target_os = "macos")]
326+
{
327+
extern "C" {
328+
fn _rjem_je_zone_register();
329+
}
330+
331+
#[used]
332+
static _F7: unsafe extern "C" fn() = _rjem_je_zone_register;
333+
}
334+
}
335+
302336
fn main() {
337+
jemalloc_magic();
338+
303339
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
304340

305341
// Snapshot a copy of the environment before `rustc` starts messing with it.

0 commit comments

Comments
 (0)