Skip to content

Commit 0532364

Browse files
committed
Simplify jemalloc setup
Using the new `override_allocator_on_supported_platforms` feature in `tikv-jemalloc-sys v0.6.1` we can avoid the manual statics.
1 parent 4ce5ac8 commit 0532364

File tree

2 files changed

+7
-43
lines changed

2 files changed

+7
-43
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ serde_json = { version = "1.0", optional = true }
3333
# But only for some targets, it fails for others. Rustc configures this in its CI, but we can't
3434
# easily use that since we support of-tree builds.
3535
[target.'cfg(any(target_os = "linux", target_os = "macos"))'.dependencies.tikv-jemalloc-sys]
36-
version = "0.6.0"
37-
features = ['unprefixed_malloc_on_supported_platforms']
36+
version = "0.6.1"
37+
features = ['override_allocator_on_supported_platforms']
3838

3939
[target.'cfg(unix)'.dependencies]
4040
libc = "0.2"

src/bin/miri.rs

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ extern crate rustc_middle;
2020
extern crate rustc_session;
2121
extern 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+
2328
mod log;
2429

2530
use std::env;
@@ -392,48 +397,7 @@ fn parse_range(val: &str) -> Result<Range<u32>, &'static str> {
392397
Ok(from..to)
393398
}
394399

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-
433400
fn main() {
434-
#[cfg(any(target_os = "linux", target_os = "macos"))]
435-
jemalloc_magic();
436-
437401
let early_dcx = EarlyDiagCtxt::new(ErrorOutputType::default());
438402

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

0 commit comments

Comments
 (0)