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.
|
|
|
|
|
2015-06-09 20:52:41 +02: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 07:19:08 +02:00
|
|
|
tracing garbage collector",
|
|
|
|
issue = "27700")]
|
2015-06-09 20:52:41 +02:00
|
|
|
|
2015-02-08 00:49:54 +01:00
|
|
|
use core::{isize, usize};
|
2016-11-11 11:55:47 +01:00
|
|
|
use core::intrinsics::{min_align_of_val, size_of_val};
|
2015-02-08 00:49:54 +01:00
|
|
|
|
2015-06-25 19:07:01 +02:00
|
|
|
#[allow(improper_ctypes)]
|
2015-11-23 03:32:40 +01:00
|
|
|
extern "C" {
|
2015-06-25 19:07:01 +02:00
|
|
|
#[allocator]
|
|
|
|
fn __rust_allocate(size: usize, align: usize) -> *mut u8;
|
2017-03-10 02:53:01 +01:00
|
|
|
fn __rust_allocate_zeroed(size: usize, align: usize) -> *mut u8;
|
2015-06-25 19:07:01 +02:00
|
|
|
fn __rust_deallocate(ptr: *mut u8, old_size: usize, align: usize);
|
2015-09-24 00:00:54 +02: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 19:07:01 +02:00
|
|
|
fn __rust_usable_size(size: usize, align: usize) -> usize;
|
|
|
|
}
|
|
|
|
|
2015-02-08 00:49:54 +01:00
|
|
|
#[inline(always)]
|
|
|
|
fn check_size_and_alignment(size: usize, align: usize) {
|
|
|
|
debug_assert!(size != 0);
|
2015-09-24 01:32:01 +02: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-08 00:49:54 +01:00
|
|
|
}
|
|
|
|
|
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 {
|
2015-02-08 00:49:54 +01:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 19:07:01 +02:00
|
|
|
__rust_allocate(size, align)
|
2014-04-25 08:19:34 +02:00
|
|
|
}
|
|
|
|
|
2017-03-10 02:53:01 +01:00
|
|
|
/// Return a pointer to `size` bytes of memory aligned to `align` and
|
|
|
|
/// initialized to zeroes.
|
|
|
|
///
|
|
|
|
/// On failure, return a null pointer.
|
|
|
|
///
|
|
|
|
/// 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.
|
|
|
|
#[inline]
|
|
|
|
pub unsafe fn allocate_zeroed(size: usize, align: usize) -> *mut u8 {
|
|
|
|
check_size_and_alignment(size, align);
|
|
|
|
__rust_allocate_zeroed(size, align)
|
|
|
|
}
|
|
|
|
|
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 {
|
2015-02-08 00:49:54 +01:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 19:07:01 +02:00
|
|
|
__rust_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-09-24 00:00:54 +02:00
|
|
|
pub unsafe fn reallocate_inplace(ptr: *mut u8,
|
|
|
|
old_size: usize,
|
|
|
|
size: usize,
|
|
|
|
align: usize)
|
|
|
|
-> usize {
|
2015-02-08 00:49:54 +01:00
|
|
|
check_size_and_alignment(size, align);
|
2015-06-25 19:07:01 +02:00
|
|
|
__rust_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) {
|
2015-06-25 19:07:01 +02:00
|
|
|
__rust_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 {
|
2015-06-25 19:07:01 +02:00
|
|
|
unsafe { __rust_usable_size(size, align) }
|
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.
|
|
|
|
///
|
2015-06-25 19:07:01 +02:00
|
|
|
/// This preserves the non-null invariant for types like `Box<T>`. The address
|
|
|
|
/// may overlap with non-zero-size memory allocations.
|
2017-05-04 20:48:58 +02:00
|
|
|
#[rustc_deprecated(since = "1.19", reason = "Use Unique/Shared::empty() instead")]
|
|
|
|
#[unstable(feature = "heap_api", issue = "27700")]
|
|
|
|
pub const EMPTY: *mut () = 1 as *mut ();
|
2014-05-20 08:19:56 +02:00
|
|
|
|
2014-05-07 04:03:14 +02:00
|
|
|
/// The allocator for unique pointers.
|
2016-12-16 02:00:19 +01:00
|
|
|
// This function must not unwind. If it does, MIR trans will fail.
|
2014-05-12 08:51:00 +02:00
|
|
|
#[cfg(not(test))]
|
2015-05-09 21:50:28 +02:00
|
|
|
#[lang = "exchange_malloc"]
|
2014-05-07 04:03:14 +02:00
|
|
|
#[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 {
|
2017-05-04 20:48:58 +02:00
|
|
|
align 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);
|
2015-09-24 00:00:54 +02:00
|
|
|
if ptr.is_null() {
|
|
|
|
::oom()
|
|
|
|
}
|
2014-10-28 22:06:06 +01:00
|
|
|
ptr
|
2014-05-07 04:03:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 14:08:41 +02:00
|
|
|
#[cfg_attr(not(test), lang = "box_free")]
|
2016-01-28 22:59:00 +01:00
|
|
|
#[inline]
|
2017-04-20 14:08:41 +02:00
|
|
|
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
|
2016-11-11 11:55:47 +01:00
|
|
|
let size = size_of_val(&*ptr);
|
|
|
|
let align = min_align_of_val(&*ptr);
|
2016-01-28 22:59:00 +01:00
|
|
|
// We do not allocate for Box<T> when T is ZST, so deallocation is also not necessary.
|
|
|
|
if size != 0 {
|
2016-11-11 11:55:47 +01:00
|
|
|
deallocate(ptr as *mut u8, size, align);
|
2016-01-28 22:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2017-03-10 02:53:01 +01:00
|
|
|
#[test]
|
|
|
|
fn allocate_zeroed() {
|
|
|
|
unsafe {
|
|
|
|
let size = 1024;
|
|
|
|
let ptr = heap::allocate_zeroed(size, 1);
|
|
|
|
if ptr.is_null() {
|
|
|
|
::oom()
|
|
|
|
}
|
|
|
|
|
|
|
|
let end = ptr.offset(size as isize);
|
|
|
|
let mut i = ptr;
|
|
|
|
while i < end {
|
|
|
|
assert_eq!(*i, 0);
|
|
|
|
i = i.offset(1);
|
|
|
|
}
|
|
|
|
heap::deallocate(ptr, size, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-02 09:29:39 +02:00
|
|
|
#[test]
|
|
|
|
fn basic_reallocate_inplace_noop() {
|
|
|
|
unsafe {
|
|
|
|
let size = 4000;
|
|
|
|
let ptr = heap::allocate(size, 8);
|
2015-09-24 00:00:54 +02: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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|