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