2014-04-25 01:19:34 -05:00
|
|
|
// Copyright 2014 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.
|
|
|
|
|
2014-06-14 01:22:58 -05:00
|
|
|
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
|
2014-05-06 16:01:16 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns a pointer to `size` bytes of memory.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// Behavior is undefined if the requested size is 0 or the alignment is not a
|
|
|
|
/// power of 2. The alignment must be no larger than the largest supported page
|
|
|
|
/// size on the platform.
|
2014-04-25 01:19:34 -05:00
|
|
|
#[inline]
|
|
|
|
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::allocate(size, align)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
|
2014-06-14 01:22:58 -05:00
|
|
|
/// memory.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// Behavior is undefined if the requested size is 0 or the alignment is not a
|
|
|
|
/// power of 2. The alignment must be no larger than the largest supported page
|
|
|
|
/// size on the platform.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// The `old_size` and `align` parameters are the parameters that were used to
|
|
|
|
/// create the allocation referenced by `ptr`. The `old_size` parameter may also
|
|
|
|
/// be the value returned by `usable_size` for the requested size.
|
2014-04-25 01:19:34 -05:00
|
|
|
#[inline]
|
2014-06-14 01:22:58 -05:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
|
|
|
|
old_size: uint) -> *mut u8 {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::reallocate(ptr, size, align, old_size)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Extends or shrinks the allocation referenced by `ptr` to `size` bytes of
|
2014-06-14 01:22:58 -05:00
|
|
|
/// memory in-place.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns true if successful, otherwise false if the allocation was not
|
2014-06-14 01:22:58 -05:00
|
|
|
/// altered.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// Behavior is undefined if the requested size is 0 or the alignment is not a
|
|
|
|
/// power of 2. The alignment must be no larger than the largest supported page
|
|
|
|
/// size on the platform.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
|
|
|
/// The `old_size` and `align` parameters are the parameters that were used to
|
|
|
|
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
|
|
|
|
/// any value in range_inclusive(requested_size, usable_size).
|
|
|
|
#[inline]
|
2014-06-14 01:22:58 -05:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
|
|
|
|
old_size: uint) -> bool {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::reallocate_inplace(ptr, size, align, old_size)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Deallocates the memory referenced by `ptr`.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
|
|
|
/// The `ptr` parameter must not be null.
|
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// The `size` and `align` parameters are the parameters that were used to
|
|
|
|
/// create the allocation referenced by `ptr`. The `size` parameter may also be
|
|
|
|
/// the value returned by `usable_size` for the requested size.
|
2014-04-25 01:19:34 -05:00
|
|
|
#[inline]
|
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::deallocate(ptr, size, align)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Returns the usable size of an allocation created with the specified the
|
2014-06-14 01:22:58 -05:00
|
|
|
/// `size` and `align`.
|
2014-04-25 01:19:34 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn usable_size(size: uint, align: uint) -> uint {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::usable_size(size, align)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
2014-05-06 21:03:14 -05:00
|
|
|
|
2014-08-04 05:48:39 -05:00
|
|
|
/// Prints implementation-defined allocator statistics.
|
2014-05-11 16:41:15 -05:00
|
|
|
///
|
2014-06-14 01:22:58 -05:00
|
|
|
/// These statistics may be inconsistent if other threads use the allocator
|
|
|
|
/// during the call.
|
2014-05-11 16:41:15 -05:00
|
|
|
#[unstable]
|
|
|
|
pub fn stats_print() {
|
2014-06-14 01:23:31 -05:00
|
|
|
imp::stats_print();
|
2014-05-11 16:41:15 -05:00
|
|
|
}
|
|
|
|
|
2014-09-15 14:37:01 -05:00
|
|
|
/// An arbitrary non-null address to represent zero-size allocations.
|
|
|
|
///
|
|
|
|
/// This preserves the non-null invariant for types like `Box<T>`. The address may overlap with
|
|
|
|
/// non-zero-size memory allocations.
|
|
|
|
pub static EMPTY: *mut () = 0x1 as *mut ();
|
2014-05-20 01:19:56 -05:00
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
/// The allocator for unique pointers.
|
2014-05-12 01:51:00 -05:00
|
|
|
#[cfg(not(test))]
|
2014-05-06 21:03:14 -05:00
|
|
|
#[lang="exchange_malloc"]
|
|
|
|
#[inline]
|
2014-05-20 22:43:18 -05:00
|
|
|
unsafe fn exchange_malloc(size: uint, align: uint) -> *mut u8 {
|
2014-05-06 21:03:14 -05:00
|
|
|
if size == 0 {
|
2014-09-15 14:37:01 -05:00
|
|
|
EMPTY as *mut u8
|
2014-05-06 21:03:14 -05:00
|
|
|
} else {
|
|
|
|
allocate(size, align)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:32:50 -05:00
|
|
|
#[cfg(not(test))]
|
2014-05-20 23:18:10 -05:00
|
|
|
#[lang="exchange_free"]
|
|
|
|
#[inline]
|
|
|
|
unsafe fn exchange_free(ptr: *mut u8, size: uint, align: uint) {
|
|
|
|
deallocate(ptr, size, align);
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:46:33 -05: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.
|
2014-09-29 00:07:45 -05:00
|
|
|
#[cfg(any(target_arch = "arm",
|
|
|
|
target_arch = "mips",
|
|
|
|
target_arch = "mipsel"))]
|
2014-09-08 00:46:33 -05:00
|
|
|
static MIN_ALIGN: uint = 8;
|
2014-09-29 00:07:45 -05:00
|
|
|
#[cfg(any(target_arch = "x86",
|
|
|
|
target_arch = "x86_64"))]
|
2014-09-08 00:46:33 -05:00
|
|
|
static MIN_ALIGN: uint = 16;
|
|
|
|
|
2014-06-14 01:23:31 -05:00
|
|
|
#[cfg(jemalloc)]
|
|
|
|
mod imp {
|
|
|
|
use core::option::{None, Option};
|
2014-09-14 22:27:36 -05:00
|
|
|
use core::ptr::{RawPtr, null_mut, null};
|
2014-06-18 19:05:15 -05:00
|
|
|
use core::num::Int;
|
2014-06-14 01:23:31 -05:00
|
|
|
use libc::{c_char, c_int, c_void, size_t};
|
2014-09-08 00:46:33 -05:00
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 01:23:31 -05:00
|
|
|
|
|
|
|
#[link(name = "jemalloc", kind = "static")]
|
2014-07-30 09:44:20 -05:00
|
|
|
#[cfg(not(test))]
|
|
|
|
extern {}
|
|
|
|
|
2014-06-14 01:23:31 -05:00
|
|
|
extern {
|
|
|
|
fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
|
|
|
|
fn je_rallocx(ptr: *mut c_void, size: size_t,
|
|
|
|
flags: c_int) -> *mut c_void;
|
|
|
|
fn je_xallocx(ptr: *mut c_void, size: size_t, extra: size_t,
|
|
|
|
flags: c_int) -> size_t;
|
2014-09-05 09:39:36 -05:00
|
|
|
fn je_sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
|
2014-06-14 01:23:31 -05:00
|
|
|
fn je_nallocx(size: size_t, flags: c_int) -> size_t;
|
2014-06-25 14:47:34 -05:00
|
|
|
fn je_malloc_stats_print(write_cb: Option<extern "C" fn(cbopaque: *mut c_void,
|
|
|
|
*const c_char)>,
|
2014-06-14 01:23:31 -05:00
|
|
|
cbopaque: *mut c_void,
|
2014-06-25 14:47:34 -05:00
|
|
|
opts: *const c_char);
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
|
2014-09-29 00:07:45 -05:00
|
|
|
#[cfg(all(not(windows), not(target_os = "android")))]
|
2014-06-14 01:23:31 -05:00
|
|
|
#[link(name = "pthread")]
|
|
|
|
extern {}
|
|
|
|
|
|
|
|
// MALLOCX_ALIGN(a) macro
|
|
|
|
#[inline(always)]
|
|
|
|
fn mallocx_align(a: uint) -> c_int { a.trailing_zeros() as c_int }
|
|
|
|
|
2014-09-08 00:46:33 -05:00
|
|
|
#[inline(always)]
|
|
|
|
fn align_to_flags(align: uint) -> c_int {
|
|
|
|
if align <= MIN_ALIGN { 0 } else { mallocx_align(align) }
|
|
|
|
}
|
|
|
|
|
2014-06-14 01:23:31 -05:00
|
|
|
#[inline]
|
|
|
|
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
let flags = align_to_flags(align);
|
|
|
|
let ptr = je_mallocx(size as size_t, flags) as *mut u8;
|
2014-06-14 01:23:31 -05:00
|
|
|
if ptr.is_null() {
|
2014-06-14 01:35:54 -05:00
|
|
|
::oom()
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
|
|
|
|
_old_size: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
let flags = align_to_flags(align);
|
|
|
|
let ptr = je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8;
|
2014-06-14 01:23:31 -05:00
|
|
|
if ptr.is_null() {
|
2014-06-14 01:35:54 -05:00
|
|
|
::oom()
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8, size: uint, align: uint,
|
2014-10-02 02:29:39 -05:00
|
|
|
old_size: uint) -> bool {
|
2014-09-08 00:46:33 -05:00
|
|
|
let flags = align_to_flags(align);
|
2014-10-02 02:29:39 -05:00
|
|
|
let new_size = je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as uint;
|
|
|
|
// checking for failure to shrink is tricky
|
|
|
|
if size < old_size {
|
|
|
|
usable_size(size, align) == new_size as uint
|
|
|
|
} else {
|
|
|
|
new_size >= size
|
|
|
|
}
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-09-05 09:39:36 -05:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, size: uint, align: uint) {
|
|
|
|
let flags = align_to_flags(align);
|
|
|
|
je_sdallocx(ptr as *mut c_void, size as size_t, flags)
|
|
|
|
}
|
|
|
|
|
2014-06-14 01:23:31 -05:00
|
|
|
#[inline]
|
|
|
|
pub fn usable_size(size: uint, align: uint) -> uint {
|
2014-09-08 00:46:33 -05:00
|
|
|
let flags = align_to_flags(align);
|
|
|
|
unsafe { je_nallocx(size as size_t, flags) as uint }
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats_print() {
|
|
|
|
unsafe {
|
2014-09-14 22:27:36 -05:00
|
|
|
je_malloc_stats_print(None, null_mut(), null())
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-29 00:07:45 -05:00
|
|
|
#[cfg(all(not(jemalloc), unix))]
|
2014-06-14 01:23:31 -05:00
|
|
|
mod imp {
|
2014-08-23 10:29:48 -05:00
|
|
|
use core::cmp;
|
2014-06-14 01:23:31 -05:00
|
|
|
use core::ptr;
|
|
|
|
use libc;
|
|
|
|
use libc_heap;
|
2014-09-08 00:46:33 -05:00
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 01:23:31 -05:00
|
|
|
|
|
|
|
extern {
|
|
|
|
fn posix_memalign(memptr: *mut *mut libc::c_void,
|
|
|
|
align: libc::size_t,
|
|
|
|
size: libc::size_t) -> libc::c_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
if align <= MIN_ALIGN {
|
2014-06-14 01:23:31 -05:00
|
|
|
libc_heap::malloc_raw(size)
|
|
|
|
} else {
|
|
|
|
let mut out = 0 as *mut libc::c_void;
|
|
|
|
let ret = posix_memalign(&mut out,
|
|
|
|
align as libc::size_t,
|
|
|
|
size as libc::size_t);
|
|
|
|
if ret != 0 {
|
|
|
|
::oom();
|
|
|
|
}
|
|
|
|
out as *mut u8
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
|
|
|
|
old_size: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
libc_heap::realloc_raw(ptr, size)
|
|
|
|
} else {
|
|
|
|
let new_ptr = allocate(size, align);
|
|
|
|
ptr::copy_memory(new_ptr, ptr as *const u8, cmp::min(size, old_size));
|
|
|
|
deallocate(ptr, old_size, align);
|
|
|
|
new_ptr
|
|
|
|
}
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-02 02:29:39 -05:00
|
|
|
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
|
|
|
|
old_size: uint) -> bool {
|
|
|
|
size == old_size
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, _size: uint, _align: uint) {
|
|
|
|
libc::free(ptr as *mut libc::c_void)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn usable_size(size: uint, _align: uint) -> uint {
|
|
|
|
size
|
|
|
|
}
|
|
|
|
|
2014-09-08 00:46:33 -05:00
|
|
|
pub fn stats_print() {}
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
2014-09-29 00:07:45 -05:00
|
|
|
#[cfg(all(not(jemalloc), windows))]
|
2014-06-14 01:23:31 -05:00
|
|
|
mod imp {
|
|
|
|
use libc::{c_void, size_t};
|
2014-09-08 00:46:33 -05:00
|
|
|
use libc;
|
|
|
|
use libc_heap;
|
2014-06-14 01:23:31 -05:00
|
|
|
use core::ptr::RawPtr;
|
2014-09-08 00:46:33 -05:00
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 01:23:31 -05:00
|
|
|
|
|
|
|
extern {
|
|
|
|
fn _aligned_malloc(size: size_t, align: size_t) -> *mut c_void;
|
|
|
|
fn _aligned_realloc(block: *mut c_void, size: size_t,
|
|
|
|
align: size_t) -> *mut c_void;
|
|
|
|
fn _aligned_free(ptr: *mut c_void);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
libc_heap::malloc_raw(size)
|
|
|
|
} else {
|
|
|
|
let ptr = _aligned_malloc(size as size_t, align as size_t);
|
|
|
|
if ptr.is_null() {
|
|
|
|
::oom();
|
|
|
|
}
|
|
|
|
ptr as *mut u8
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
|
|
|
|
_old_size: uint) -> *mut u8 {
|
2014-09-08 00:46:33 -05:00
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
libc_heap::realloc_raw(ptr, size)
|
|
|
|
} else {
|
|
|
|
let ptr = _aligned_realloc(ptr as *mut c_void, size as size_t,
|
|
|
|
align as size_t);
|
|
|
|
if ptr.is_null() {
|
|
|
|
::oom();
|
|
|
|
}
|
|
|
|
ptr as *mut u8
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-10-02 02:29:39 -05:00
|
|
|
pub unsafe fn reallocate_inplace(_ptr: *mut u8, size: uint, _align: uint,
|
|
|
|
old_size: uint) -> bool {
|
|
|
|
size == old_size
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2014-09-08 00:46:33 -05:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, _size: uint, align: uint) {
|
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
libc::free(ptr as *mut libc::c_void)
|
|
|
|
} else {
|
|
|
|
_aligned_free(ptr as *mut c_void)
|
|
|
|
}
|
2014-06-14 01:23:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn usable_size(size: uint, _align: uint) -> uint {
|
|
|
|
size
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats_print() {}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[cfg(test)]
|
2014-10-02 02:29:39 -05:00
|
|
|
mod test {
|
2014-05-06 21:03:14 -05:00
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
|
|
|
|
2014-10-02 02:29:39 -05:00
|
|
|
#[test]
|
|
|
|
fn basic_reallocate_inplace_noop() {
|
|
|
|
unsafe {
|
|
|
|
let size = 4000;
|
|
|
|
let ptr = heap::allocate(size, 8);
|
|
|
|
let ret = heap::reallocate_inplace(ptr, size, 8, size);
|
|
|
|
heap::deallocate(ptr, size, 8);
|
|
|
|
assert!(ret);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[bench]
|
|
|
|
fn alloc_owned_small(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2014-06-27 14:30:25 -05:00
|
|
|
box 10i
|
2014-05-06 21:03:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|