2014-12-13 00:39:27 +01:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-04-25 08:19:34 +02:00
|
|
|
// 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 08:22:58 +02:00
|
|
|
// FIXME: #13996: mark the `allocate` and `reallocate` return value as `noalias`
|
2014-05-06 23:01:16 +02:00
|
|
|
|
2014-10-28 22:06:06 +01:00
|
|
|
/// Return a pointer to `size` bytes of memory aligned to `align`.
|
|
|
|
///
|
|
|
|
/// On failure, return a null pointer.
|
2014-04-25 08:19:34 +02:00
|
|
|
///
|
2014-06-14 08:22:58 +02: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 08:19:34 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2014-06-14 08:23:31 +02:00
|
|
|
imp::allocate(size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 22:06:06 +01:00
|
|
|
/// Resize the allocation referenced by `ptr` to `size` bytes.
|
|
|
|
///
|
|
|
|
/// On failure, return a null pointer and leave the original allocation intact.
|
2014-04-25 08:19:34 +02:00
|
|
|
///
|
2015-03-20 20:22:57 +01:00
|
|
|
/// If the allocation was relocated, the memory at the passed-in pointer is
|
|
|
|
/// undefined after the call.
|
|
|
|
///
|
2014-06-14 08:22:58 +02: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 08:19:34 +02:00
|
|
|
///
|
2014-06-14 08:22:58 +02:00
|
|
|
/// The `old_size` and `align` parameters are the parameters that were used to
|
2014-10-25 02:11:28 +02:00
|
|
|
/// create the allocation referenced by `ptr`. The `old_size` parameter may be
|
|
|
|
/// any value in range_inclusive(requested_size, usable_size).
|
2014-04-25 08:19:34 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2014-10-08 06:57:29 +02:00
|
|
|
imp::reallocate(ptr, old_size, size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-28 22:06:06 +01:00
|
|
|
/// Resize the allocation referenced by `ptr` to `size` bytes.
|
2014-04-25 08:19:34 +02:00
|
|
|
///
|
2014-10-25 01:58:26 +02:00
|
|
|
/// If the operation succeeds, it returns `usable_size(size, align)` and if it
|
|
|
|
/// fails (or is a no-op) it returns `usable_size(old_size, align)`.
|
2014-04-25 08:19:34 +02:00
|
|
|
///
|
2014-06-14 08:22:58 +02: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 08:19:34 +02: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]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
|
|
|
|
align: usize) -> usize {
|
2014-10-08 06:57:29 +02:00
|
|
|
imp::reallocate_inplace(ptr, old_size, size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
|
|
|
|
2014-08-04 12:48:39 +02:00
|
|
|
/// Deallocates the memory referenced by `ptr`.
|
2014-04-25 08:19:34 +02:00
|
|
|
///
|
|
|
|
/// The `ptr` parameter must not be null.
|
|
|
|
///
|
2014-10-25 02:11:28 +02: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).
|
2014-04-25 08:19:34 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, old_size: usize, align: usize) {
|
2014-10-25 02:11:28 +02:00
|
|
|
imp::deallocate(ptr, old_size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
|
|
|
|
2014-08-04 12:48:39 +02:00
|
|
|
/// Returns the usable size of an allocation created with the specified the
|
2014-06-14 08:22:58 +02:00
|
|
|
/// `size` and `align`.
|
2014-04-25 08:19:34 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub fn usable_size(size: usize, align: usize) -> usize {
|
2014-06-14 08:23:31 +02:00
|
|
|
imp::usable_size(size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
2014-05-07 04:03:14 +02:00
|
|
|
|
2014-08-04 12:48:39 +02:00
|
|
|
/// Prints implementation-defined allocator statistics.
|
2014-05-11 23:41:15 +02:00
|
|
|
///
|
2014-06-14 08:22:58 +02:00
|
|
|
/// These statistics may be inconsistent if other threads use the allocator
|
|
|
|
/// during the call.
|
2015-01-23 03:22:03 +01:00
|
|
|
#[unstable(feature = "alloc")]
|
2014-05-11 23:41:15 +02:00
|
|
|
pub fn stats_print() {
|
2014-06-14 08:23:31 +02:00
|
|
|
imp::stats_print();
|
2014-05-11 23:41:15 +02:00
|
|
|
}
|
|
|
|
|
2014-09-15 21:37:01 +02: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.
|
2014-10-07 01:32:00 +02:00
|
|
|
pub const EMPTY: *mut () = 0x1 as *mut ();
|
2014-05-20 08:19:56 +02:00
|
|
|
|
2014-05-07 04:03:14 +02:00
|
|
|
/// The allocator for unique pointers.
|
2014-05-12 08:51:00 +02:00
|
|
|
#[cfg(not(test))]
|
2014-05-07 04:03:14 +02:00
|
|
|
#[lang="exchange_malloc"]
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
2014-05-07 04:03:14 +02:00
|
|
|
if size == 0 {
|
2014-09-15 21:37:01 +02:00
|
|
|
EMPTY as *mut u8
|
2014-05-07 04:03:14 +02:00
|
|
|
} else {
|
2014-10-28 22:06:06 +01:00
|
|
|
let ptr = allocate(size, align);
|
|
|
|
if ptr.is_null() { ::oom() }
|
|
|
|
ptr
|
2014-05-07 04:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-30 02:32:50 +02:00
|
|
|
#[cfg(not(test))]
|
2014-05-21 06:18:10 +02:00
|
|
|
#[lang="exchange_free"]
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
unsafe fn exchange_free(ptr: *mut u8, old_size: usize, align: usize) {
|
2014-10-25 02:11:28 +02:00
|
|
|
deallocate(ptr, old_size, align);
|
2014-05-21 06:18:10 +02:00
|
|
|
}
|
|
|
|
|
2014-09-08 07:46:33 +02: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-12-30 06:13:08 +01:00
|
|
|
#[cfg(all(not(feature = "external_funcs"),
|
|
|
|
not(feature = "external_crate"),
|
|
|
|
any(target_arch = "arm",
|
|
|
|
target_arch = "mips",
|
2015-01-10 04:54:55 +01:00
|
|
|
target_arch = "mipsel",
|
|
|
|
target_arch = "powerpc")))]
|
2015-02-09 08:00:46 +01:00
|
|
|
const MIN_ALIGN: usize = 8;
|
2014-12-30 06:13:08 +01:00
|
|
|
#[cfg(all(not(feature = "external_funcs"),
|
|
|
|
not(feature = "external_crate"),
|
|
|
|
any(target_arch = "x86",
|
|
|
|
target_arch = "x86_64",
|
2015-01-08 03:53:58 +01:00
|
|
|
target_arch = "aarch64")))]
|
2015-02-09 08:00:46 +01:00
|
|
|
const MIN_ALIGN: usize = 16;
|
2014-09-08 07:46:33 +02:00
|
|
|
|
2014-12-26 23:28:04 +01:00
|
|
|
#[cfg(feature = "external_funcs")]
|
2014-12-04 21:02:59 +01:00
|
|
|
mod imp {
|
2015-04-17 04:34:12 +02:00
|
|
|
#[allow(improper_ctypes)]
|
2014-12-04 21:02:59 +01:00
|
|
|
extern {
|
2015-02-09 08:00:46 +01:00
|
|
|
fn rust_allocate(size: usize, align: usize) -> *mut u8;
|
|
|
|
fn rust_deallocate(ptr: *mut u8, old_size: usize, align: usize);
|
|
|
|
fn rust_reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8;
|
|
|
|
fn rust_reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
|
|
|
|
align: usize) -> usize;
|
|
|
|
fn rust_usable_size(size: usize, align: usize) -> usize;
|
2014-12-04 21:02:59 +01:00
|
|
|
fn rust_stats_print();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2014-12-04 21:02:59 +01:00
|
|
|
rust_allocate(size, align)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, old_size: usize, align: usize) {
|
2014-12-27 00:34:51 +01:00
|
|
|
rust_deallocate(ptr, old_size, align)
|
2014-12-04 21:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2014-12-27 00:34:51 +01:00
|
|
|
rust_reallocate(ptr, old_size, size, align)
|
2014-12-04 21:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8, old_size: usize, size: usize,
|
|
|
|
align: usize) -> usize {
|
2014-12-04 21:02:59 +01:00
|
|
|
rust_reallocate_inplace(ptr, old_size, size, align)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub fn usable_size(size: usize, align: usize) -> usize {
|
2014-12-04 21:02:59 +01:00
|
|
|
unsafe { rust_usable_size(size, align) }
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn stats_print() {
|
|
|
|
unsafe { rust_stats_print() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-26 23:28:04 +01:00
|
|
|
#[cfg(feature = "external_crate")]
|
2014-12-04 21:02:59 +01:00
|
|
|
mod imp {
|
|
|
|
extern crate external;
|
|
|
|
pub use self::external::{allocate, deallocate, reallocate_inplace, reallocate};
|
|
|
|
pub use self::external::{usable_size, stats_print};
|
|
|
|
}
|
|
|
|
|
2014-12-27 00:09:04 +01:00
|
|
|
#[cfg(all(not(feature = "external_funcs"),
|
|
|
|
not(feature = "external_crate"),
|
|
|
|
jemalloc))]
|
2014-06-14 08:23:31 +02:00
|
|
|
mod imp {
|
2014-11-28 17:57:41 +01:00
|
|
|
use core::option::Option;
|
|
|
|
use core::option::Option::None;
|
2014-10-28 22:06:06 +01:00
|
|
|
use core::ptr::{null_mut, null};
|
2014-06-14 08:23:31 +02:00
|
|
|
use libc::{c_char, c_int, c_void, size_t};
|
2014-09-08 07:46:33 +02:00
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 08:23:31 +02:00
|
|
|
|
|
|
|
#[link(name = "jemalloc", kind = "static")]
|
2014-07-30 16:44:20 +02:00
|
|
|
#[cfg(not(test))]
|
|
|
|
extern {}
|
|
|
|
|
2014-06-14 08:23:31 +02:00
|
|
|
extern {
|
2015-03-13 03:19:30 +01:00
|
|
|
#[allocator]
|
2014-06-14 08:23:31 +02:00
|
|
|
fn je_mallocx(size: size_t, flags: c_int) -> *mut c_void;
|
2014-10-28 22:06:06 +01:00
|
|
|
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 16:39:36 +02:00
|
|
|
fn je_sdallocx(ptr: *mut c_void, size: size_t, flags: c_int);
|
2014-06-14 08:23:31 +02:00
|
|
|
fn je_nallocx(size: size_t, flags: c_int) -> size_t;
|
2014-06-25 21:47:34 +02:00
|
|
|
fn je_malloc_stats_print(write_cb: Option<extern "C" fn(cbopaque: *mut c_void,
|
|
|
|
*const c_char)>,
|
2014-06-14 08:23:31 +02:00
|
|
|
cbopaque: *mut c_void,
|
2014-06-25 21:47:34 +02:00
|
|
|
opts: *const c_char);
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// -lpthread needs to occur after -ljemalloc, the earlier argument isn't enough
|
2015-04-22 00:56:55 +02:00
|
|
|
#[cfg(all(not(windows),
|
|
|
|
not(target_os = "android"),
|
|
|
|
not(target_env = "musl")))]
|
2014-06-14 08:23:31 +02:00
|
|
|
#[link(name = "pthread")]
|
|
|
|
extern {}
|
|
|
|
|
|
|
|
// MALLOCX_ALIGN(a) macro
|
|
|
|
#[inline(always)]
|
2015-02-09 08:00:46 +01:00
|
|
|
fn mallocx_align(a: usize) -> c_int { a.trailing_zeros() as c_int }
|
2014-06-14 08:23:31 +02:00
|
|
|
|
2014-09-08 07:46:33 +02:00
|
|
|
#[inline(always)]
|
2015-02-09 08:00:46 +01:00
|
|
|
fn align_to_flags(align: usize) -> c_int {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN { 0 } else { mallocx_align(align) }
|
|
|
|
}
|
|
|
|
|
2014-06-14 08:23:31 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
let flags = align_to_flags(align);
|
2014-10-28 22:06:06 +01:00
|
|
|
je_mallocx(size as size_t, flags) as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
let flags = align_to_flags(align);
|
2014-10-28 22:06:06 +01:00
|
|
|
je_rallocx(ptr as *mut c_void, size as size_t, flags) as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8, _old_size: usize, size: usize,
|
|
|
|
align: usize) -> usize {
|
2014-09-08 07:46:33 +02:00
|
|
|
let flags = align_to_flags(align);
|
2015-02-09 08:00:46 +01:00
|
|
|
je_xallocx(ptr as *mut c_void, size as size_t, 0, flags) as usize
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, old_size: usize, align: usize) {
|
2014-09-05 16:39:36 +02:00
|
|
|
let flags = align_to_flags(align);
|
2014-10-25 02:11:28 +02:00
|
|
|
je_sdallocx(ptr as *mut c_void, old_size as size_t, flags)
|
2014-09-05 16:39:36 +02:00
|
|
|
}
|
|
|
|
|
2014-06-14 08:23:31 +02:00
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub fn usable_size(size: usize, align: usize) -> usize {
|
2014-09-08 07:46:33 +02:00
|
|
|
let flags = align_to_flags(align);
|
2015-02-09 08:00:46 +01:00
|
|
|
unsafe { je_nallocx(size as size_t, flags) as usize }
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats_print() {
|
|
|
|
unsafe {
|
2014-09-15 05:27:36 +02:00
|
|
|
je_malloc_stats_print(None, null_mut(), null())
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-27 00:09:04 +01:00
|
|
|
#[cfg(all(not(feature = "external_funcs"),
|
|
|
|
not(feature = "external_crate"),
|
|
|
|
not(jemalloc),
|
|
|
|
unix))]
|
2014-06-14 08:23:31 +02:00
|
|
|
mod imp {
|
2014-08-23 17:29:48 +02:00
|
|
|
use core::cmp;
|
2014-06-14 08:23:31 +02:00
|
|
|
use core::ptr;
|
|
|
|
use libc;
|
2014-09-08 07:46:33 +02:00
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 08:23:31 +02:00
|
|
|
|
|
|
|
extern {
|
|
|
|
fn posix_memalign(memptr: *mut *mut libc::c_void,
|
|
|
|
align: libc::size_t,
|
|
|
|
size: libc::size_t) -> libc::c_int;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN {
|
2014-10-28 22:06:06 +01:00
|
|
|
libc::malloc(size as libc::size_t) as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
} else {
|
2015-01-21 18:23:27 +01:00
|
|
|
let mut out = ptr::null_mut();
|
2014-06-14 08:23:31 +02:00
|
|
|
let ret = posix_memalign(&mut out,
|
|
|
|
align as libc::size_t,
|
|
|
|
size as libc::size_t);
|
|
|
|
if ret != 0 {
|
2014-10-28 22:06:06 +01:00
|
|
|
ptr::null_mut()
|
|
|
|
} else {
|
|
|
|
out as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN {
|
2014-10-28 22:06:06 +01:00
|
|
|
libc::realloc(ptr as *mut libc::c_void, size as libc::size_t) as *mut u8
|
2014-09-08 07:46:33 +02:00
|
|
|
} else {
|
|
|
|
let new_ptr = allocate(size, align);
|
2015-03-27 19:12:28 +01:00
|
|
|
ptr::copy(ptr, new_ptr, cmp::min(size, old_size));
|
2014-09-08 07:46:33 +02:00
|
|
|
deallocate(ptr, old_size, align);
|
|
|
|
new_ptr
|
|
|
|
}
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize,
|
|
|
|
_align: usize) -> usize {
|
2014-10-25 01:58:26 +02:00
|
|
|
old_size
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, _align: usize) {
|
2014-06-14 08:23:31 +02:00
|
|
|
libc::free(ptr as *mut libc::c_void)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub fn usable_size(size: usize, _align: usize) -> usize {
|
2014-06-14 08:23:31 +02:00
|
|
|
size
|
|
|
|
}
|
|
|
|
|
2014-09-08 07:46:33 +02:00
|
|
|
pub fn stats_print() {}
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
2014-12-27 00:09:04 +01:00
|
|
|
#[cfg(all(not(feature = "external_funcs"),
|
|
|
|
not(feature = "external_crate"),
|
|
|
|
not(jemalloc),
|
|
|
|
windows))]
|
2014-06-14 08:23:31 +02:00
|
|
|
mod imp {
|
|
|
|
use libc::{c_void, size_t};
|
2014-09-08 07:46:33 +02:00
|
|
|
use libc;
|
|
|
|
use super::MIN_ALIGN;
|
2014-06-14 08:23:31 +02: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]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN {
|
2014-10-28 22:06:06 +01:00
|
|
|
libc::malloc(size as size_t) as *mut u8
|
2014-09-08 07:46:33 +02:00
|
|
|
} else {
|
2014-10-28 22:06:06 +01:00
|
|
|
_aligned_malloc(size as size_t, align as size_t) as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, _old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN {
|
2014-10-28 22:06:06 +01:00
|
|
|
libc::realloc(ptr as *mut c_void, size as size_t) as *mut u8
|
2014-09-08 07:46:33 +02:00
|
|
|
} else {
|
2014-10-28 22:06:06 +01:00
|
|
|
_aligned_realloc(ptr as *mut c_void, size as size_t, align as size_t) as *mut u8
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn reallocate_inplace(_ptr: *mut u8, old_size: usize, _size: usize,
|
|
|
|
_align: usize) -> usize {
|
2014-10-25 01:58:26 +02:00
|
|
|
old_size
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, _old_size: usize, align: usize) {
|
2014-09-08 07:46:33 +02:00
|
|
|
if align <= MIN_ALIGN {
|
|
|
|
libc::free(ptr as *mut libc::c_void)
|
|
|
|
} else {
|
|
|
|
_aligned_free(ptr as *mut c_void)
|
|
|
|
}
|
2014-06-14 08:23:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2015-02-09 08:00:46 +01:00
|
|
|
pub fn usable_size(size: usize, _align: usize) -> usize {
|
2014-06-14 08:23:31 +02:00
|
|
|
size
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn stats_print() {}
|
|
|
|
}
|
|
|
|
|
2014-05-07 04:03:14 +02:00
|
|
|
#[cfg(test)]
|
2015-04-24 17:30:41 +02:00
|
|
|
mod tests {
|
2014-05-07 04:03:14 +02:00
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2015-02-17 21:41:32 +01:00
|
|
|
use boxed::Box;
|
2014-10-03 00:12:58 +02:00
|
|
|
use heap;
|
2014-05-07 04:03:14 +02:00
|
|
|
|
2014-10-02 09:29:39 +02:00
|
|
|
#[test]
|
|
|
|
fn basic_reallocate_inplace_noop() {
|
|
|
|
unsafe {
|
|
|
|
let size = 4000;
|
|
|
|
let ptr = heap::allocate(size, 8);
|
2014-10-28 22:06:06 +01:00
|
|
|
if ptr.is_null() { ::oom() }
|
2014-10-08 06:57:29 +02:00
|
|
|
let ret = heap::reallocate_inplace(ptr, size, size, 8);
|
2014-10-02 09:29:39 +02:00
|
|
|
heap::deallocate(ptr, size, 8);
|
2014-10-25 01:58:26 +02:00
|
|
|
assert_eq!(ret, heap::usable_size(size, 8));
|
2014-10-02 09:29:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-07 04:03:14 +02:00
|
|
|
#[bench]
|
|
|
|
fn alloc_owned_small(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2015-02-17 21:41:32 +01:00
|
|
|
let _: Box<_> = box 10;
|
2014-05-07 04:03:14 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|