2015-06-25 10:07:01 -07:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
#![crate_name = "alloc_jemalloc"]
|
|
|
|
#![crate_type = "rlib"]
|
|
|
|
#![no_std]
|
2015-12-11 13:07:11 -08:00
|
|
|
#![allocator]
|
2015-06-25 10:07:01 -07:00
|
|
|
#![unstable(feature = "alloc_jemalloc",
|
|
|
|
reason = "this library is unlikely to be stabilized in its current \
|
2015-08-13 13:06:25 -07:00
|
|
|
form or name",
|
|
|
|
issue = "27783")]
|
2016-12-29 09:47:34 -08:00
|
|
|
#![deny(warnings)]
|
2015-06-25 10:07:01 -07:00
|
|
|
#![feature(allocator)]
|
|
|
|
#![feature(libc)]
|
|
|
|
#![feature(staged_api)]
|
|
|
|
|
|
|
|
extern crate libc;
|
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
pub use imp::*;
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
// See comments in build.rs for why we sometimes build a crate that does nothing
|
|
|
|
#[cfg(not(dummy_jemalloc))]
|
|
|
|
mod imp {
|
|
|
|
use libc::{c_int, c_void, size_t};
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2017-03-12 14:13:35 -04:00
|
|
|
// Note that the symbols here are prefixed by default on macOS and Windows (we
|
2016-09-27 21:27:22 +00:00
|
|
|
// don't explicitly request it), and on Android and DragonFly we explicitly
|
|
|
|
// request it as unprefixing cause segfaults (mismatches in allocators).
|
|
|
|
extern "C" {
|
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
2017-04-08 20:36:00 +02:00
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
2016-09-27 21:27:22 +00:00
|
|
|
link_name = "je_mallocx")]
|
|
|
|
fn mallocx(size: size_t, flags: c_int) -> *mut c_void;
|
2017-03-09 17:53:01 -08:00
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
|
|
|
link_name = "je_calloc")]
|
|
|
|
fn calloc(size: size_t, flags: c_int) -> *mut c_void;
|
2016-09-27 21:27:22 +00:00
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
2017-04-08 20:36:00 +02:00
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
2016-09-27 21:27:22 +00:00
|
|
|
link_name = "je_rallocx")]
|
|
|
|
fn rallocx(ptr: *mut c_void, size: size_t, flags: c_int) -> *mut c_void;
|
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
2017-04-08 20:36:00 +02:00
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
2016-09-27 21:27:22 +00:00
|
|
|
link_name = "je_xallocx")]
|
|
|
|
fn xallocx(ptr: *mut c_void, size: size_t, extra: size_t, flags: c_int) -> size_t;
|
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
2017-04-08 20:36:00 +02:00
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
2016-09-27 21:27:22 +00:00
|
|
|
link_name = "je_sdallocx")]
|
|
|
|
fn sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
|
|
|
|
#[cfg_attr(any(target_os = "macos", target_os = "android", target_os = "ios",
|
2017-04-08 20:36:00 +02:00
|
|
|
target_os = "dragonfly", target_os = "windows", target_env = "musl"),
|
2016-09-27 21:27:22 +00:00
|
|
|
link_name = "je_nallocx")]
|
|
|
|
fn nallocx(size: size_t, flags: c_int) -> size_t;
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:01 -08:00
|
|
|
const MALLOCX_ZERO: c_int = 0x40;
|
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
// The minimum alignment guaranteed by the architecture. This value is used to
|
|
|
|
// add fast paths for low alignment values. In practice, the alignment is a
|
|
|
|
// constant at the call site and the branch will be optimized out.
|
|
|
|
#[cfg(all(any(target_arch = "arm",
|
|
|
|
target_arch = "mips",
|
|
|
|
target_arch = "powerpc")))]
|
|
|
|
const MIN_ALIGN: usize = 8;
|
|
|
|
#[cfg(all(any(target_arch = "x86",
|
|
|
|
target_arch = "x86_64",
|
|
|
|
target_arch = "aarch64",
|
|
|
|
target_arch = "powerpc64",
|
|
|
|
target_arch = "mips64",
|
2016-12-06 15:55:11 -06:00
|
|
|
target_arch = "s390x",
|
|
|
|
target_arch = "sparc64")))]
|
2016-09-27 21:27:22 +00:00
|
|
|
const MIN_ALIGN: usize = 16;
|
|
|
|
|
|
|
|
// MALLOCX_ALIGN(a) macro
|
|
|
|
fn mallocx_align(a: usize) -> c_int {
|
|
|
|
a.trailing_zeros() as c_int
|
|
|
|
}
|
|
|
|
|
|
|
|
fn align_to_flags(align: usize) -> c_int {
|
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
0
|
|
|
|
} else {
|
|
|
|
mallocx_align(align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_allocate(size: usize, align: usize) -> *mut u8 {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { mallocx(size as size_t, flags) as *mut u8 }
|
|
|
|
}
|
|
|
|
|
2017-03-09 17:53:01 -08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8 {
|
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
unsafe { calloc(size as size_t, 1) as *mut u8 }
|
|
|
|
} else {
|
|
|
|
let flags = align_to_flags(align) | MALLOCX_ZERO;
|
|
|
|
unsafe { mallocx(size as size_t, flags) as *mut u8 }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_reallocate(ptr: *mut u8,
|
|
|
|
_old_size: usize,
|
|
|
|
size: usize,
|
|
|
|
align: usize)
|
|
|
|
-> *mut u8 {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8 }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_reallocate_inplace(ptr: *mut u8,
|
|
|
|
_old_size: usize,
|
|
|
|
size: usize,
|
|
|
|
align: usize)
|
|
|
|
-> usize {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize }
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize) {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { sdallocx(ptr as *mut c_void, old_size as size_t, flags) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_usable_size(size: usize, align: usize) -> usize {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { nallocx(size as size_t, flags) as usize }
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
}
|
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[cfg(dummy_jemalloc)]
|
|
|
|
mod imp {
|
|
|
|
fn bogus() -> ! {
|
|
|
|
panic!("jemalloc is not implemented for this platform");
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_allocate(_size: usize, _align: usize) -> *mut u8 {
|
|
|
|
bogus()
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2017-03-09 17:53:01 -08:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_allocate_zeroed(_size: usize, _align: usize) -> *mut u8 {
|
|
|
|
bogus()
|
|
|
|
}
|
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_reallocate(_ptr: *mut u8,
|
|
|
|
_old_size: usize,
|
|
|
|
_size: usize,
|
|
|
|
_align: usize)
|
|
|
|
-> *mut u8 {
|
|
|
|
bogus()
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_reallocate_inplace(_ptr: *mut u8,
|
|
|
|
_old_size: usize,
|
|
|
|
_size: usize,
|
|
|
|
_align: usize)
|
|
|
|
-> usize {
|
|
|
|
bogus()
|
|
|
|
}
|
2015-06-25 10:07:01 -07:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_deallocate(_ptr: *mut u8, _old_size: usize, _align: usize) {
|
|
|
|
bogus()
|
|
|
|
}
|
2015-12-02 10:31:29 -08:00
|
|
|
|
2016-09-27 21:27:22 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn __rust_usable_size(_size: usize, _align: usize) -> usize {
|
|
|
|
bogus()
|
|
|
|
}
|
2015-12-02 10:31:29 -08:00
|
|
|
}
|