Skip to content

Commit 424c6c9

Browse files
committed
[DO NOT MERGE] Test jemalloc's sized deallocation path.
1 parent e95e084 commit 424c6c9

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

compiler/rustc/src/main.rs

+36
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,42 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1111
#[cfg(feature = "tikv-jemalloc-sys")]
1212
use tikv_jemalloc_sys as jemalloc_sys;
1313

14+
use std::alloc::{GlobalAlloc, Layout};
15+
use tikv_jemallocator::Jemalloc;
16+
17+
// XXX: these declarations make rustc hook into tikv-jemallocator, which means
18+
// jemalloc's sized deallocation path (`sdallocx`) gets used. Without them,
19+
// rustc hooks in at a lower level, via tikv-jemalloc-sys, which uses the
20+
// vanilla `free` path. In theory, the `sdallocx` path is faster.
21+
//
22+
// Note that this is a hack that works on Linux, but probably doesn't work on
23+
// other platforms. It's just for testing purposes.
24+
25+
#[no_mangle]
26+
pub unsafe extern "C" fn __rust_alloc(size: usize, align: usize) -> *mut u8 {
27+
Jemalloc.alloc(Layout::from_size_align_unchecked(size, align))
28+
}
29+
30+
#[no_mangle]
31+
pub unsafe extern "C" fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
32+
Jemalloc.alloc_zeroed(Layout::from_size_align_unchecked(size, align))
33+
}
34+
35+
#[no_mangle]
36+
pub unsafe extern "C" fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize) {
37+
Jemalloc.dealloc(ptr, Layout::from_size_align_unchecked(size, align))
38+
}
39+
40+
#[no_mangle]
41+
pub unsafe extern "C" fn __rust_realloc(
42+
ptr: *mut u8,
43+
old_size: usize,
44+
align: usize,
45+
new_size: usize,
46+
) -> *mut u8 {
47+
Jemalloc.realloc(ptr, Layout::from_size_align_unchecked(old_size, align), new_size)
48+
}
49+
1450
fn main() {
1551
// Pull in jemalloc when enabled.
1652
//

0 commit comments

Comments
 (0)