|
1 | | -// A note about jemalloc: rustc uses jemalloc when built for CI and |
| 1 | +// A note about mimalloc: rustc uses mimalloc when built for CI and |
2 | 2 | // distribution. The obvious way to do this is with the `#[global_allocator]` |
3 | 3 | // mechanism. However, for complicated reasons (see |
4 | 4 | // https://github.com/rust-lang/rust/pull/81782#issuecomment-784438001 for some |
5 | 5 | // details) that mechanism doesn't work here. Also, we must use a consistent |
6 | 6 | // allocator across the rustc <-> llvm boundary, and `#[global_allocator]` |
7 | 7 | // wouldn't provide that. |
8 | 8 | // |
9 | | -// Instead, we use a lower-level mechanism. rustc is linked with jemalloc in a |
10 | | -// way such that jemalloc's implementation of `malloc`, `free`, etc., override |
| 9 | +// Instead, we use a lower-level mechanism. rustc is linked with mimalloc in a |
| 10 | +// way such that mimalloc's implementation of `malloc`, `free`, etc., override |
11 | 11 | // the libc allocator's implementation. This means that Rust's `System` |
12 | 12 | // allocator, which calls `libc::malloc()` et al., is actually calling into |
13 | | -// jemalloc. |
14 | | -// |
15 | | -// A consequence of not using `GlobalAlloc` (and the `tikv-jemallocator` crate |
16 | | -// provides an impl of that trait, which is called `Jemalloc`) is that we |
17 | | -// cannot use the sized deallocation APIs (`sdallocx`) that jemalloc provides. |
18 | | -// It's unclear how much performance is lost because of this. |
| 13 | +// mimalloc. |
19 | 14 | // |
20 | 15 | // As for the symbol overrides in `main` below: we're pulling in a static copy |
21 | | -// of jemalloc. We need to actually reference its symbols for it to get linked. |
| 16 | +// of mimalloc. We need to actually reference its symbols for it to get linked. |
22 | 17 | // The two crates we link to here, `std` and `rustc_driver`, are both dynamic |
23 | | -// libraries. So we must reference jemalloc symbols one way or another, because |
| 18 | +// libraries. So we must reference mimalloc symbols one way or another, because |
24 | 19 | // this file is the only object code in the rustc executable. |
25 | | -#[cfg(feature = "tikv-jemalloc-sys")] |
26 | | -use tikv_jemalloc_sys as jemalloc_sys; |
27 | 20 |
|
28 | 21 | fn main() { |
29 | 22 | // See the comment at the top of this file for an explanation of this. |
30 | | - #[cfg(feature = "tikv-jemalloc-sys")] |
| 23 | + #[cfg(feature = "libmimalloc-sys")] |
31 | 24 | { |
32 | 25 | use std::os::raw::{c_int, c_void}; |
33 | 26 |
|
34 | 27 | #[used] |
35 | | - static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::calloc; |
| 28 | + static _F1: unsafe extern "C" fn(usize, usize) -> *mut c_void = libmimalloc_sys::mi_calloc; |
36 | 29 | #[used] |
37 | 30 | static _F2: unsafe extern "C" fn(*mut *mut c_void, usize, usize) -> c_int = |
38 | | - jemalloc_sys::posix_memalign; |
| 31 | + libmimalloc_sys::mi_posix_memalign; |
39 | 32 | #[used] |
40 | | - static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = jemalloc_sys::aligned_alloc; |
| 33 | + static _F3: unsafe extern "C" fn(usize, usize) -> *mut c_void = |
| 34 | + libmimalloc_sys::mi_aligned_alloc; |
41 | 35 | #[used] |
42 | | - static _F4: unsafe extern "C" fn(usize) -> *mut c_void = jemalloc_sys::malloc; |
| 36 | + static _F4: unsafe extern "C" fn(usize) -> *mut c_void = libmimalloc_sys::mi_malloc; |
43 | 37 | #[used] |
44 | | - static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = jemalloc_sys::realloc; |
| 38 | + static _F5: unsafe extern "C" fn(*mut c_void, usize) -> *mut c_void = |
| 39 | + libmimalloc_sys::mi_realloc; |
45 | 40 | #[used] |
46 | | - static _F6: unsafe extern "C" fn(*mut c_void) = jemalloc_sys::free; |
| 41 | + static _F6: unsafe extern "C" fn(*mut c_void) = libmimalloc_sys::mi_free; |
47 | 42 |
|
48 | | - // On OSX, jemalloc doesn't directly override malloc/free, but instead |
| 43 | + // On OSX, mimalloc doesn't directly override malloc/free, but instead |
49 | 44 | // registers itself with the allocator's zone APIs in a ctor. However, |
50 | 45 | // the linker doesn't seem to consider ctors as "used" when statically |
51 | 46 | // linking, so we need to explicitly depend on the function. |
52 | 47 | #[cfg(target_os = "macos")] |
53 | 48 | { |
54 | 49 | extern "C" { |
55 | | - fn _rjem_je_zone_register(); |
| 50 | + fn _mi_macos_override_malloc(); |
56 | 51 | } |
57 | 52 |
|
58 | 53 | #[used] |
59 | | - static _F7: unsafe extern "C" fn() = _rjem_je_zone_register; |
| 54 | + static _F7: unsafe extern "C" fn() = _mi_macos_override_malloc; |
60 | 55 | } |
61 | 56 | } |
62 | 57 |
|
|
0 commit comments