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.
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.
12 #![allow(unused_attributes)]
13 #![unstable(feature = "alloc_jemalloc",
14 reason = "this library is unlikely to be stabilized in its current \
17 #![feature(core_intrinsics)]
20 #![feature(staged_api)]
21 #![feature(rustc_attrs)]
22 #![cfg_attr(dummy_jemalloc, allow(dead_code, unused_extern_crates))]
23 #![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
24 #![rustc_alloc_kind = "exe"]
28 #[cfg(not(dummy_jemalloc))]
30 #[cfg(not(dummy_jemalloc))]
32 use libc::{c_int, c_void, size_t};
34 // Note that the symbols here are prefixed by default on macOS and Windows (we
35 // don't explicitly request it), and on Android and DragonFly we explicitly
36 // request it as unprefixing cause segfaults (mismatches in allocators).
38 #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
39 target_os = "dragonfly", target_os = "windows", target_env = "musl"),
40 link_name = "je_mallocx")]
41 fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
42 #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
43 target_os = "dragonfly", target_os = "windows", target_env = "musl"),
44 link_name = "je_calloc")]
45 fn calloc(size: size_t, flags: c_int) -> *mut c_void;
46 #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
47 target_os = "dragonfly", target_os = "windows", target_env = "musl"),
48 link_name = "je_rallocx")]
49 fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
50 #[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
51 target_os = "dragonfly", target_os = "windows", target_env = "musl"),
52 link_name = "je_sdallocx")]
53 fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
56 const MALLOCX_ZERO: c_int = 0x40;
58 // The minimum alignment guaranteed by the architecture. This value is used to
59 // add fast paths for low alignment values.
60 #[cfg(all(any(target_arch = "arm",
62 target_arch = "powerpc")))]
63 const MIN_ALIGN: usize = 8;
64 #[cfg(all(any(target_arch = "x86",
65 target_arch = "x86_64",
66 target_arch = "aarch64",
67 target_arch = "powerpc64",
68 target_arch = "mips64",
69 target_arch = "s390x",
70 target_arch = "sparc64")))]
71 const MIN_ALIGN: usize = 16;
73 // MALLOCX_ALIGN(a) macro
74 fn mallocx_align(a: usize) -> c_int {
75 a.trailing_zeros() as c_int
78 fn align_to_flags(align: usize, size: usize) -> c_int {
79 if align <= MIN_ALIGN && align <= size {
86 // for symbol names src/librustc/middle/allocator.rs
87 // for signatures src/librustc_allocator/lib.rs
89 // linkage directives are provided as part of the current compiler allocator
93 #[rustc_std_internal_symbol]
94 pub unsafe extern fn __rde_alloc(size: usize, align: usize) -> *mut u8 {
95 let flags = align_to_flags(align, size);
96 let ptr = mallocx(size as size_t, flags) as *mut u8;
101 #[rustc_std_internal_symbol]
102 pub unsafe extern fn __rde_dealloc(ptr: *mut u8,
105 let flags = align_to_flags(align, size);
106 sdallocx(ptr as *mut c_void, size, flags);
110 #[rustc_std_internal_symbol]
111 pub unsafe extern fn __rde_realloc(ptr: *mut u8,
114 new_size: usize) -> *mut u8 {
115 let flags = align_to_flags(align, new_size);
116 let ptr = rallocx(ptr as *mut c_void, new_size, flags) as *mut u8;
121 #[rustc_std_internal_symbol]
122 pub unsafe extern fn __rde_alloc_zeroed(size: usize, align: usize) -> *mut u8 {
123 let ptr = if align <= MIN_ALIGN && align <= size {
124 calloc(size as size_t, 1) as *mut u8
126 let flags = align_to_flags(align, size) | MALLOCX_ZERO;
127 mallocx(size as size_t, flags) as *mut u8