@@ -11,6 +11,42 @@ static ALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
11
11
#[ cfg( feature = "tikv-jemalloc-sys" ) ]
12
12
use tikv_jemalloc_sys as jemalloc_sys;
13
13
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
+
14
50
fn main ( ) {
15
51
// Pull in jemalloc when enabled.
16
52
//
0 commit comments