]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/lib.rs
Unignore u128 test for stage 0,1
[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 #![deny(warnings)]
20 #![feature(allocator)]
21 #![feature(libc)]
22 #![feature(staged_api)]
23
24 extern crate libc;
25
26 pub use imp::*;
27
28 // See comments in build.rs for why we sometimes build a crate that does nothing
29 #[cfg(not(dummy_jemalloc))]
30 mod imp {
31     use libc::{c_int, c_void, size_t};
32
33     // Linkage directives to pull in jemalloc and its dependencies.
34     //
35     // On some platforms we need to be sure to link in `pthread` which jemalloc
36     // depends on, and specifically on android we need to also link to libgcc.
37     // Currently jemalloc is compiled with gcc which will generate calls to
38     // intrinsics that are libgcc specific (e.g. those intrinsics aren't present in
39     // libcompiler-rt), so link that in to get that support.
40     #[link(name = "jemalloc", kind = "static")]
41     #[cfg_attr(target_os = "android", link(name = "gcc"))]
42     #[cfg_attr(all(not(windows),
43                    not(target_os = "android"),
44                    not(target_env = "musl")),
45                link(name = "pthread"))]
46     #[cfg(not(cargobuild))]
47     extern "C" {}
48
49     // Note that the symbols here are prefixed by default on OSX and Windows (we
50     // don't explicitly request it), and on Android and DragonFly we explicitly
51     // request it as unprefixing cause segfaults (mismatches in allocators).
52     extern "C" {
53         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
54                        target_os = "dragonfly", target_os = "windows"),
55                    link_name = "je_mallocx")]
56         fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
57         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
58                        target_os = "dragonfly", target_os = "windows"),
59                    link_name = "je_rallocx")]
60         fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
61         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
62                        target_os = "dragonfly", target_os = "windows"),
63                    link_name = "je_xallocx")]
64         fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
65         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
66                        target_os = "dragonfly", target_os = "windows"),
67                    link_name = "je_sdallocx")]
68         fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
69         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
70                        target_os = "dragonfly", target_os = "windows"),
71                    link_name = "je_nallocx")]
72         fn nallocx(size: size_t, flags: c_int) -> size_t;
73     }
74
75     // The minimum alignment guaranteed by the architecture. This value is used to
76     // add fast paths for low alignment values. In practice, the alignment is a
77     // constant at the call site and the branch will be optimized out.
78     #[cfg(all(any(target_arch = "arm",
79                   target_arch = "mips",
80                   target_arch = "powerpc")))]
81     const MIN_ALIGN: usize = 8;
82     #[cfg(all(any(target_arch = "x86",
83                   target_arch = "x86_64",
84                   target_arch = "aarch64",
85                   target_arch = "powerpc64",
86                   target_arch = "mips64",
87                   target_arch = "s390x",
88                   target_arch = "sparc64")))]
89     const MIN_ALIGN: usize = 16;
90
91     // MALLOCX_ALIGN(a) macro
92     fn mallocx_align(a: usize) -> c_int {
93         a.trailing_zeros() as c_int
94     }
95
96     fn align_to_flags(align: usize) -> c_int {
97         if align <= MIN_ALIGN {
98             0
99         } else {
100             mallocx_align(align)
101         }
102     }
103
104     #[no_mangle]
105     pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
106         let flags = align_to_flags(align);
107         unsafe { mallocx(size as size_t, flags) as *mut u8 }
108     }
109
110     #[no_mangle]
111     pub extern "C" fn __rust_reallocate(ptr: *mut u8,
112                                         _old_size: usize,
113                                         size: usize,
114                                         align: usize)
115                                         -> *mut u8 {
116         let flags = align_to_flags(align);
117         unsafe { rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
118     }
119
120     #[no_mangle]
121     pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
122                                                 _old_size: usize,
123                                                 size: usize,
124                                                 align: usize)
125                                                 -> usize {
126         let flags = align_to_flags(align);
127         unsafe { xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
128     }
129
130     #[no_mangle]
131     pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
132         let flags = align_to_flags(align);
133         unsafe { sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
134     }
135
136     #[no_mangle]
137     pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
138         let flags = align_to_flags(align);
139         unsafe { nallocx(size as size_t, flags) as usize }
140     }
141
142     // These symbols are used by jemalloc on android but the really old android
143     // we're building on doesn't have them defined, so just make sure the symbols
144     // are available.
145     #[no_mangle]
146     #[cfg(all(target_os = "android", not(cargobuild)))]
147     pub extern "C" fn pthread_atfork(_prefork: *mut u8,
148                                      _postfork_parent: *mut u8,
149                                      _postfork_child: *mut u8)
150                                      -> i32 {
151         0
152     }
153 }
154
155 #[cfg(dummy_jemalloc)]
156 mod imp {
157     fn bogus() -> ! {
158         panic!("jemalloc is not implemented for this platform");
159     }
160
161     #[no_mangle]
162     pub extern "C" fn __rust_allocate(_size: usize, _align: usize) -> *mut u8 {
163         bogus()
164     }
165
166     #[no_mangle]
167     pub extern "C" fn __rust_reallocate(_ptr: *mut u8,
168                                         _old_size: usize,
169                                         _size: usize,
170                                         _align: usize)
171                                         -> *mut u8 {
172         bogus()
173     }
174
175     #[no_mangle]
176     pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8,
177                                                 _old_size: usize,
178                                                 _size: usize,
179                                                 _align: usize)
180                                                 -> usize {
181         bogus()
182     }
183
184     #[no_mangle]
185     pub extern "C" fn __rust_deallocate(_ptr: *mut u8, _old_size: usize, _align: usize) {
186         bogus()
187     }
188
189     #[no_mangle]
190     pub extern "C" fn __rust_usable_size(_size: usize, _align: usize) -> usize {
191         bogus()
192     }
193 }