2014-12-12 17:39:27 -06:00
|
|
|
// Copyright 2014-2015 The Rust Project Developers. See the COPYRIGHT
|
2014-04-25 01:19:34 -05: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.
|
|
|
|
|
2015-06-09 13:52:41 -05:00
|
|
|
#![unstable(feature = "heap_api",
|
|
|
|
reason = "the precise API and guarantees it provides may be tweaked \
|
|
|
|
slightly, especially to possibly take into account the \
|
|
|
|
types being stored to make room for a future \
|
2015-08-13 00:19:08 -05:00
|
|
|
tracing garbage collector",
|
|
|
|
issue = "27700")]
|
2015-06-09 13:52:41 -05:00
|
|
|
|
2015-02-07 17:49:54 -06:00
|
|
|
use core::{isize, usize};
|
2016-01-28 15:59:00 -06:00
|
|
|
#[cfg(not(test))]
|
2016-11-11 04:55:47 -06:00
|
|
|
use core::intrinsics::{min_align_of_val, size_of_val};
|
2015-02-07 17:49:54 -06:00
|
|
|
|
2015-06-25 12:07:01 -05:00
|
|
|
#[allow(improper_ctypes)]
|
2015-11-22 20:32:40 -06:00
|
|
|
extern "C" {
|
2015-06-25 12:07:01 -05:00
|
|
|
#[allocator]
|
|
|
|
fn __rust_allocate(size: usize, align: usize) -> *mut u8;
|
|
|
|
fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize);
|
2015-09-23 17:00:54 -05:00
|
|
|
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;
|
2015-06-25 12:07:01 -05:00
|
|
|
fn __rust_usable_size(size: usize, align: usize) -> usize;
|
|
|
|
}
|
|
|
|
|
2015-02-07 17:49:54 -06:00
|
|
|
#[inline(always)]
|
|
|
|
fn check_size_and_alignment(size: usize, align: usize) {
|
|
|
|
debug_assert!(size != 0);
|
2015-09-23 18:32:01 -05:00
|
|
|
debug_assert!(size <= isize::MAX as usize,
|
|
|
|
"Tried to allocate too much: {} bytes",
|
|
|
|
size);
|
|
|
|
debug_assert!(usize::is_power_of_two(align),
|
|
|
|
"Invalid alignment of allocation: {}",
|
|
|
|
align);
|
2015-02-07 17:49:54 -06:00
|
|
|
}
|
|
|
|
|
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-10-28 16:06:06 -05:00
|
|
|
/// Return a pointer to `size` bytes of memory aligned to `align`.
|
|
|
|
///
|
|
|
|
/// On failure, return a null pointer.
|
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]
|
2015-02-09 01:00:46 -06:00
|
|
|
pub unsafe fn allocate(size: usize, align: usize) -> *mut u8 {
|
2015-02-07 17:49:54 -06:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 12:07:01 -05:00
|
|
|
__rust_allocate(size, align)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:06:06 -05: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 01:19:34 -05:00
|
|
|
///
|
2015-03-20 14:22:57 -05:00
|
|
|
/// If the allocation was relocated, the memory at the passed-in pointer is
|
|
|
|
/// undefined after the call.
|
|
|
|
///
|
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
|
2014-10-24 19:11:28 -05: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 01:19:34 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
pub unsafe fn reallocate(ptr: *mut u8, old_size: usize, size: usize, align: usize) -> *mut u8 {
|
2015-02-07 17:49:54 -06:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 12:07:01 -05:00
|
|
|
__rust_reallocate(ptr, old_size, size, align)
|
2014-04-25 01:19:34 -05:00
|
|
|
}
|
|
|
|
|
2014-10-28 16:06:06 -05:00
|
|
|
/// Resize the allocation referenced by `ptr` to `size` bytes.
|
2014-04-25 01:19:34 -05:00
|
|
|
///
|
2014-10-24 18:58:26 -05: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 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]
|
2015-09-23 17:00:54 -05:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8,
|
|
|
|
old_size: usize,
|
|
|
|
size: usize,
|
|
|
|
align: usize)
|
|
|
|
-> usize {
|
2015-02-07 17:49:54 -06:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 12:07:01 -05:00
|
|
|
__rust_reallocate_inplace(ptr, old_size, size, align)
|
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-10-24 19:11:28 -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).
|
2014-04-25 01:19:34 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
pub unsafe fn deallocate(ptr: *mut u8, old_size: usize, align: usize) {
|
2015-06-25 12:07:01 -05:00
|
|
|
__rust_deallocate(ptr, old_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]
|
2015-02-09 01:00:46 -06:00
|
|
|
pub fn usable_size(size: usize, align: usize) -> usize {
|
2015-06-25 12:07:01 -05:00
|
|
|
unsafe { __rust_usable_size(size, align) }
|
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.
|
|
|
|
///
|
2015-06-25 12:07:01 -05:00
|
|
|
/// This preserves the non-null invariant for types like `Box<T>`. The address
|
|
|
|
/// may overlap with non-zero-size memory allocations.
|
2014-10-06 18:32:00 -05:00
|
|
|
pub const 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))]
|
2015-05-09 14:50:28 -05:00
|
|
|
#[lang = "exchange_malloc"]
|
2014-05-06 21:03:14 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
unsafe fn exchange_malloc(size: usize, align: usize) -> *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 {
|
2014-10-28 16:06:06 -05:00
|
|
|
let ptr = allocate(size, align);
|
2015-09-23 17:00:54 -05:00
|
|
|
if ptr.is_null() {
|
|
|
|
::oom()
|
|
|
|
}
|
2014-10-28 16:06:06 -05:00
|
|
|
ptr
|
2014-05-06 21:03:14 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-29 19:32:50 -05:00
|
|
|
#[cfg(not(test))]
|
2015-05-09 14:50:28 -05:00
|
|
|
#[lang = "exchange_free"]
|
2014-05-20 23:18:10 -05:00
|
|
|
#[inline]
|
2015-02-09 01:00:46 -06:00
|
|
|
unsafe fn exchange_free(ptr: *mut u8, old_size: usize, align: usize) {
|
2014-10-24 19:11:28 -05:00
|
|
|
deallocate(ptr, old_size, align);
|
2014-05-20 23:18:10 -05:00
|
|
|
}
|
|
|
|
|
2016-01-28 15:59:00 -06:00
|
|
|
#[cfg(not(test))]
|
|
|
|
#[lang = "box_free"]
|
|
|
|
#[inline]
|
2016-11-11 04:55:47 -06:00
|
|
|
unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
|
|
|
|
let size = size_of_val(&*ptr);
|
|
|
|
let align = min_align_of_val(&*ptr);
|
2016-01-28 15:59:00 -06:00
|
|
|
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
|
|
|
|
if size != 0 {
|
2016-11-11 04:55:47 -06:00
|
|
|
deallocate(ptr as *mut u8, size, align);
|
2016-01-28 15:59:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[cfg(test)]
|
2015-04-24 10:30:41 -05:00
|
|
|
mod tests {
|
2014-05-06 21:03:14 -05:00
|
|
|
extern crate test;
|
|
|
|
use self::test::Bencher;
|
2015-02-17 14:41:32 -06:00
|
|
|
use boxed::Box;
|
2014-10-02 17:12:58 -05:00
|
|
|
use heap;
|
2014-05-06 21:03:14 -05:00
|
|
|
|
2014-10-02 02:29:39 -05:00
|
|
|
#[test]
|
|
|
|
fn basic_reallocate_inplace_noop() {
|
|
|
|
unsafe {
|
|
|
|
let size = 4000;
|
|
|
|
let ptr = heap::allocate(size, 8);
|
2015-09-23 17:00:54 -05:00
|
|
|
if ptr.is_null() {
|
|
|
|
::oom()
|
|
|
|
}
|
2014-10-07 23:57:29 -05:00
|
|
|
let ret = heap::reallocate_inplace(ptr, size, size, 8);
|
2014-10-02 02:29:39 -05:00
|
|
|
heap::deallocate(ptr, size, 8);
|
2014-10-24 18:58:26 -05:00
|
|
|
assert_eq!(ret, heap::usable_size(size, 8));
|
2014-10-02 02:29:39 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
#[bench]
|
|
|
|
fn alloc_owned_small(b: &mut Bencher) {
|
|
|
|
b.iter(|| {
|
2015-02-17 14:41:32 -06:00
|
|
|
let _: Box<_> = box 10;
|
2014-05-06 21:03:14 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|