]> git.lizzy.rs Git - rust.git/blob - src/liballoc_jemalloc/lib.rs
f2ff0593bfa54f1a780e2f8a94b1bf4c88c55b85
[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 #![cfg_attr(stage0, feature(custom_attribute))]
12 #![crate_name = "alloc_jemalloc"]
13 #![crate_type = "rlib"]
14 #![staged_api]
15 #![no_std]
16 #![cfg_attr(not(stage0), allocator)]
17 #![cfg_attr(stage0, allow(improper_ctypes))]
18 #![unstable(feature = "alloc_jemalloc",
19             reason = "this library is unlikely to be stabilized in its current \
20                       form or name",
21             issue = "27783")]
22 #![feature(allocator)]
23 #![feature(libc)]
24 #![feature(no_std)]
25 #![feature(staged_api)]
26
27 extern crate libc;
28
29 use libc::{c_int, c_void, size_t};
30
31 // Linkage directives to pull in jemalloc and its dependencies.
32 //
33 // On some platforms we need to be sure to link in `pthread` which jemalloc
34 // depends on, and specifically on android we need to also link to libgcc.
35 // Currently jemalloc is compiled with gcc which will generate calls to
36 // intrinsics that are libgcc specific (e.g. those intrinsics aren't present in
37 // libcompiler-rt), so link that in to get that support.
38 #[link(name = "jemalloc", kind = "static")]
39 #[cfg_attr(target_os = "android", link(name = "gcc"))]
40 #[cfg_attr(all(not(windows),
41                not(target_os = "android"),
42                not(target_env = "musl")),
43            link(name = "pthread"))]
44 extern {
45     fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
46     fn je_rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
47     fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
48     fn je_sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
49     fn je_nallocx(size: size_t, flags: c_int) -> size_t;
50 }
51
52 // The minimum alignment guaranteed by the architecture. This value is used to
53 // add fast paths for low alignment values. In practice, the alignment is a
54 // constant at the call site and the branch will be optimized out.
55 #[cfg(all(any(target_arch = "arm",
56               target_arch = "mips",
57               target_arch = "mipsel",
58               target_arch = "powerpc")))]
59 const MIN_ALIGN: usize = 8;
60 #[cfg(all(any(target_arch = "x86",
61               target_arch = "x86_64",
62               target_arch = "aarch64")))]
63 const MIN_ALIGN: usize = 16;
64
65 // MALLOCX_ALIGN(a) macro
66 fn mallocx_align(a: usize) -> c_int {
67     a.trailing_zeros() as c_int
68 }
69
70 fn align_to_flags(align: usize) -> c_int {
71     if align <= MIN_ALIGN {
72         0
73     } else {
74         mallocx_align(align)
75     }
76 }
77
78 #[no_mangle]
79 pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
80     let flags = align_to_flags(align);
81     unsafe { je_mallocx(size as size_t, flags) as *mut u8 }
82 }
83
84 #[no_mangle]
85 pub extern "C" fn __rust_reallocate(ptr: *mut u8,
86                                     _old_size: usize,
87                                     size: usize,
88                                     align: usize)
89                                     -> *mut u8 {
90     let flags = align_to_flags(align);
91     unsafe { je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
92 }
93
94 #[no_mangle]
95 pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
96                                             _old_size: usize,
97                                             size: usize,
98                                             align: usize)
99                                             -> usize {
100     let flags = align_to_flags(align);
101     unsafe { je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
102 }
103
104 #[no_mangle]
105 pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
106     let flags = align_to_flags(align);
107     unsafe { je_sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
108 }
109
110 #[no_mangle]
111 pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
112     let flags = align_to_flags(align);
113     unsafe { je_nallocx(size as size_t, flags) as usize }
114 }