]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/lib.rs
Rollup merge of #39604 - est31:i128_tests, r=alexcrichton
[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     // Note that the symbols here are prefixed by default on OSX and Windows (we
34     // don't explicitly request it), and on Android and DragonFly we explicitly
35     // request it as unprefixing cause segfaults (mismatches in allocators).
36     extern "C" {
37         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
38                        target_os = "dragonfly", target_os = "windows"),
39                    link_name = "je_mallocx")]
40         fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
41         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
42                        target_os = "dragonfly", target_os = "windows"),
43                    link_name = "je_rallocx")]
44         fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
45         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
46                        target_os = "dragonfly", target_os = "windows"),
47                    link_name = "je_xallocx")]
48         fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
49         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
50                        target_os = "dragonfly", target_os = "windows"),
51                    link_name = "je_sdallocx")]
52         fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
53         #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
54                        target_os = "dragonfly", target_os = "windows"),
55                    link_name = "je_nallocx")]
56         fn nallocx(size: size_t, flags: c_int) -> size_t;
57     }
58
59     // The minimum alignment guaranteed by the architecture. This value is used to
60     // add fast paths for low alignment values. In practice, the alignment is a
61     // constant at the call site and the branch will be optimized out.
62     #[cfg(all(any(target_arch = "arm",
63                   target_arch = "mips",
64                   target_arch = "powerpc")))]
65     const MIN_ALIGN: usize = 8;
66     #[cfg(all(any(target_arch = "x86",
67                   target_arch = "x86_64",
68                   target_arch = "aarch64",
69                   target_arch = "powerpc64",
70                   target_arch = "mips64",
71                   target_arch = "s390x",
72                   target_arch = "sparc64")))]
73     const MIN_ALIGN: usize = 16;
74
75     // MALLOCX_ALIGN(a) macro
76     fn mallocx_align(a: usize) -> c_int {
77         a.trailing_zeros() as c_int
78     }
79
80     fn align_to_flags(align: usize) -> c_int {
81         if align <= MIN_ALIGN {
82             0
83         } else {
84             mallocx_align(align)
85         }
86     }
87
88     #[no_mangle]
89     pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
90         let flags = align_to_flags(align);
91         unsafe { mallocx(size as size_t, flags) as *mut u8 }
92     }
93
94     #[no_mangle]
95     pub extern "C" fn __rust_reallocate(ptr: *mut u8,
96                                         _old_size: usize,
97                                         size: usize,
98                                         align: usize)
99                                         -> *mut u8 {
100         let flags = align_to_flags(align);
101         unsafe { rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
102     }
103
104     #[no_mangle]
105     pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
106                                                 _old_size: usize,
107                                                 size: usize,
108                                                 align: usize)
109                                                 -> usize {
110         let flags = align_to_flags(align);
111         unsafe { xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
112     }
113
114     #[no_mangle]
115     pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
116         let flags = align_to_flags(align);
117         unsafe { sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
118     }
119
120     #[no_mangle]
121     pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
122         let flags = align_to_flags(align);
123         unsafe { nallocx(size as size_t, flags) as usize }
124     }
125
126     // These symbols are used by jemalloc on android but the really old android
127     // we're building on doesn't have them defined, so just make sure the symbols
128     // are available.
129     #[no_mangle]
130     #[cfg(all(target_os = "android", not(cargobuild)))]
131     pub extern "C" fn pthread_atfork(_prefork: *mut u8,
132                                      _postfork_parent: *mut u8,
133                                      _postfork_child: *mut u8)
134                                      -> i32 {
135         0
136     }
137 }
138
139 #[cfg(dummy_jemalloc)]
140 mod imp {
141     fn bogus() -> ! {
142         panic!("jemalloc is not implemented for this platform");
143     }
144
145     #[no_mangle]
146     pub extern "C" fn __rust_allocate(_size: usize, _align: usize) -> *mut u8 {
147         bogus()
148     }
149
150     #[no_mangle]
151     pub extern "C" fn __rust_reallocate(_ptr: *mut u8,
152                                         _old_size: usize,
153                                         _size: usize,
154                                         _align: usize)
155                                         -> *mut u8 {
156         bogus()
157     }
158
159     #[no_mangle]
160     pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8,
161                                                 _old_size: usize,
162                                                 _size: usize,
163                                                 _align: usize)
164                                                 -> usize {
165         bogus()
166     }
167
168     #[no_mangle]
169     pub extern "C" fn __rust_deallocate(_ptr: *mut u8, _old_size: usize, _align: usize) {
170         bogus()
171     }
172
173     #[no_mangle]
174     pub extern "C" fn __rust_usable_size(_size: usize, _align: usize) -> usize {
175         bogus()
176     }
177 }