]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/lib.rs
std: Stop prefixing jemalloc symbols
[rust.git] / src / liballoc_jemalloc / lib.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 #![crate_name = "alloc_jemalloc"]
12 #![crate_type = "rlib"]
13 #![no_std]
14 #![allocator]
15 #![unstable(feature = "alloc_jemalloc",
16             reason = "this library is unlikely to be stabilized in its current \
17                       form or name",
18             issue = "27783")]
19 #![cfg_attr(not(stage0), deny(warnings))]
20 #![feature(allocator)]
21 #![feature(libc)]
22 #![feature(staged_api)]
23
24 extern crate libc;
25
26 use libc::{c_int, c_void, size_t};
27
28 // Linkage directives to pull in jemalloc and its dependencies.
29 //
30 // On some platforms we need to be sure to link in `pthread` which jemalloc
31 // depends on, and specifically on android we need to also link to libgcc.
32 // Currently jemalloc is compiled with gcc which will generate calls to
33 // intrinsics that are libgcc specific (e.g. those intrinsics aren't present in
34 // libcompiler-rt), so link that in to get that support.
35 #[link(name = "jemalloc", kind = "static")]
36 #[cfg_attr(target_os = "android", link(name = "gcc"))]
37 #[cfg_attr(all(not(windows),
38                not(target_os = "android"),
39                not(target_env = "musl")),
40            link(name = "pthread"))]
41 #[cfg(not(cargobuild))]
42 extern {}
43
44 // Note that the symbols here are prefixed by default on OSX (we don't
45 // explicitly request it), and on Android we explicitly request it as
46 // unprefixing cause segfaults (mismatches in allocators).
47 extern {
48     #[cfg_attr(any(target_os = "macos", target_os = "android"),
49                link_name = "je_mallocx")]
50     fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
51     #[cfg_attr(any(target_os = "macos", target_os = "android"),
52                link_name = "je_rallocx")]
53     fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
54     #[cfg_attr(any(target_os = "macos", target_os = "android"),
55                link_name = "je_xallocx")]
56     fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
57     #[cfg_attr(any(target_os = "macos", target_os = "android"),
58                link_name = "je_sdallocx")]
59     fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
60     #[cfg_attr(any(target_os = "macos", target_os = "android"),
61                link_name = "je_nallocx")]
62     fn nallocx(size: size_t, flags: c_int) -> size_t;
63 }
64
65 // The minimum alignment guaranteed by the architecture. This value is used to
66 // add fast paths for low alignment values. In practice, the alignment is a
67 // constant at the call site and the branch will be optimized out.
68 #[cfg(all(any(target_arch = "arm",
69               target_arch = "mips",
70               target_arch = "powerpc")))]
71 const MIN_ALIGN: usize = 8;
72 #[cfg(all(any(target_arch = "x86",
73               target_arch = "x86_64",
74               target_arch = "aarch64",
75               target_arch = "powerpc64")))]
76 const MIN_ALIGN: usize = 16;
77
78 // MALLOCX_ALIGN(a) macro
79 fn mallocx_align(a: usize) -> c_int {
80     a.trailing_zeros() as c_int
81 }
82
83 fn align_to_flags(align: usize) -> c_int {
84     if align <= MIN_ALIGN {
85         0
86     } else {
87         mallocx_align(align)
88     }
89 }
90
91 #[no_mangle]
92 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
93     let flags = align_to_flags(align);
94     unsafe { mallocx(size as size_t, flags) as *mut u8 }
95 }
96
97 #[no_mangle]
98 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
99                                     _old_size: usize,
100                                     size: usize,
101                                     align: usize)
102                                     -> *mut u8 {
103     let flags = align_to_flags(align);
104     unsafe { rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
105 }
106
107 #[no_mangle]
108 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
109                                             _old_size: usize,
110                                             size: usize,
111                                             align: usize)
112                                             -> usize {
113     let flags = align_to_flags(align);
114     unsafe { xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
115 }
116
117 #[no_mangle]
118 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
119     let flags = align_to_flags(align);
120     unsafe { sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
121 }
122
123 #[no_mangle]
124 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
125     let flags = align_to_flags(align);
126     unsafe { nallocx(size as size_t, flags) as usize }
127 }
128
129 // These symbols are used by jemalloc on android but the really old android
130 // we're building on doesn't have them defined, so just make sure the symbols
131 // are available.
132 #[no_mangle]
133 #[cfg(target_os = "android")]
134 pub extern fn pthread_atfork(_prefork: *mut u8,
135                              _postfork_parent: *mut u8,
136                              _postfork_child: *mut u8) -> i32 {
137     0
138 }