@@ -11,6 +11,42 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
1111#[ cfg( feature = "tikv-jemalloc-sys" ) ]
1212use 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+
1450fn main ( ) {
1551 // Pull in jemalloc when enabled.
1652 //
0 commit comments