2018-05-31 18:23:42 +02:00
|
|
|
|
//! Memory allocation APIs
|
|
|
|
|
|
2018-05-31 18:36:51 +02:00
|
|
|
|
#![stable(feature = "alloc_module", since = "1.28.0")]
|
2015-06-09 20:52:41 +02:00
|
|
|
|
|
2019-02-03 08:27:44 +01:00
|
|
|
|
use core::intrinsics::{min_align_of_val, size_of_val};
|
|
|
|
|
use core::ptr::{NonNull, Unique};
|
|
|
|
|
use core::usize;
|
2015-02-08 00:49:54 +01:00
|
|
|
|
|
2018-05-31 18:36:51 +02:00
|
|
|
|
#[stable(feature = "alloc_module", since = "1.28.0")]
|
2018-04-02 10:38:07 +02:00
|
|
|
|
#[doc(inline)]
|
2018-04-03 21:05:10 +02:00
|
|
|
|
pub use core::alloc::*;
|
2018-04-02 10:38:07 +02:00
|
|
|
|
|
2019-08-02 00:40:56 +02:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2018-04-03 17:12:57 +02:00
|
|
|
|
extern "Rust" {
|
2018-11-12 09:00:39 +01:00
|
|
|
|
// These are the magic symbols to call the global allocator. rustc generates
|
|
|
|
|
// them from the `#[global_allocator]` attribute if there is one, or uses the
|
|
|
|
|
// default implementations in libstd (`__rdl_alloc` etc in `src/libstd/alloc.rs`)
|
|
|
|
|
// otherwise.
|
2019-07-04 16:05:50 +02:00
|
|
|
|
#[rustc_allocator]
|
2017-08-22 23:36:49 +02:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 17:12:57 +02:00
|
|
|
|
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
|
2017-08-22 23:36:49 +02:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 17:12:57 +02:00
|
|
|
|
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
|
2017-08-22 23:36:49 +02:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 17:12:57 +02:00
|
|
|
|
fn __rust_realloc(ptr: *mut u8,
|
|
|
|
|
old_size: usize,
|
|
|
|
|
align: usize,
|
|
|
|
|
new_size: usize) -> *mut u8;
|
2017-08-22 23:36:49 +02:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 17:12:57 +02:00
|
|
|
|
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
2015-02-08 00:49:54 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// The global memory allocator.
|
|
|
|
|
///
|
|
|
|
|
/// This type implements the [`Alloc`] trait by forwarding calls
|
|
|
|
|
/// to the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
2019-02-05 12:26:19 +01:00
|
|
|
|
///
|
|
|
|
|
/// Note: while this type is unstable, the functionality it provides can be
|
|
|
|
|
/// accessed through the [free functions in `alloc`](index.html#functions).
|
2019-05-04 16:48:57 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`Alloc`]: trait.Alloc.html
|
2018-05-31 18:36:51 +02:00
|
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
2017-05-23 14:47:41 +02:00
|
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
2018-04-03 14:43:34 +02:00
|
|
|
|
pub struct Global;
|
2017-05-23 14:47:41 +02:00
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// Allocate memory with the global allocator.
|
|
|
|
|
///
|
|
|
|
|
/// This function forwards calls to the [`GlobalAlloc::alloc`] method
|
|
|
|
|
/// of the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
|
|
|
|
///
|
|
|
|
|
/// This function is expected to be deprecated in favor of the `alloc` method
|
|
|
|
|
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::alloc`].
|
2018-08-14 14:37:12 +02:00
|
|
|
|
///
|
2019-05-04 16:48:57 +02:00
|
|
|
|
/// [`Global`]: struct.Global.html
|
|
|
|
|
/// [`Alloc`]: trait.Alloc.html
|
|
|
|
|
/// [`GlobalAlloc::alloc`]: trait.GlobalAlloc.html#tymethod.alloc
|
|
|
|
|
///
|
2018-08-14 14:37:12 +02:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::alloc::{alloc, dealloc, Layout};
|
|
|
|
|
///
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// let layout = Layout::new::<u16>();
|
|
|
|
|
/// let ptr = alloc(layout);
|
|
|
|
|
///
|
|
|
|
|
/// *(ptr as *mut u16) = 42;
|
|
|
|
|
/// assert_eq!(*(ptr as *mut u16), 42);
|
|
|
|
|
///
|
|
|
|
|
/// dealloc(ptr, layout);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2018-05-31 19:13:37 +02:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 09:10:01 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
|
|
|
|
__rust_alloc(layout.size(), layout.align())
|
|
|
|
|
}
|
2017-05-23 14:47:41 +02:00
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// Deallocate memory with the global allocator.
|
|
|
|
|
///
|
|
|
|
|
/// This function forwards calls to the [`GlobalAlloc::dealloc`] method
|
|
|
|
|
/// of the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
|
|
|
|
///
|
|
|
|
|
/// This function is expected to be deprecated in favor of the `dealloc` method
|
|
|
|
|
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::dealloc`].
|
2019-05-04 16:48:57 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`Global`]: struct.Global.html
|
|
|
|
|
/// [`Alloc`]: trait.Alloc.html
|
|
|
|
|
/// [`GlobalAlloc::dealloc`]: trait.GlobalAlloc.html#tymethod.dealloc
|
2018-05-31 19:13:37 +02:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 09:10:01 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
|
|
|
|
|
__rust_dealloc(ptr, layout.size(), layout.align())
|
|
|
|
|
}
|
2017-05-23 14:47:41 +02:00
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// Reallocate memory with the global allocator.
|
|
|
|
|
///
|
|
|
|
|
/// This function forwards calls to the [`GlobalAlloc::realloc`] method
|
|
|
|
|
/// of the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
|
|
|
|
///
|
|
|
|
|
/// This function is expected to be deprecated in favor of the `realloc` method
|
|
|
|
|
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::realloc`].
|
2019-05-04 16:48:57 +02:00
|
|
|
|
///
|
|
|
|
|
/// [`Global`]: struct.Global.html
|
|
|
|
|
/// [`Alloc`]: trait.Alloc.html
|
|
|
|
|
/// [`GlobalAlloc::realloc`]: trait.GlobalAlloc.html#method.realloc
|
2018-05-31 19:13:37 +02:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 09:10:01 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn realloc(ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
|
|
|
|
__rust_realloc(ptr, layout.size(), layout.align(), new_size)
|
|
|
|
|
}
|
2017-05-23 14:47:41 +02:00
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// Allocate zero-initialized memory with the global allocator.
|
|
|
|
|
///
|
|
|
|
|
/// This function forwards calls to the [`GlobalAlloc::alloc_zeroed`] method
|
|
|
|
|
/// of the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
|
|
|
|
///
|
|
|
|
|
/// This function is expected to be deprecated in favor of the `alloc_zeroed` method
|
|
|
|
|
/// of the [`Global`] type when it and the [`Alloc`] trait become stable.
|
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::alloc_zeroed`].
|
2018-08-14 14:37:12 +02:00
|
|
|
|
///
|
2019-05-04 16:48:57 +02:00
|
|
|
|
/// [`Global`]: struct.Global.html
|
|
|
|
|
/// [`Alloc`]: trait.Alloc.html
|
|
|
|
|
/// [`GlobalAlloc::alloc_zeroed`]: trait.GlobalAlloc.html#method.alloc_zeroed
|
|
|
|
|
///
|
2018-08-14 14:37:12 +02:00
|
|
|
|
/// # Examples
|
|
|
|
|
///
|
|
|
|
|
/// ```
|
|
|
|
|
/// use std::alloc::{alloc_zeroed, dealloc, Layout};
|
|
|
|
|
///
|
|
|
|
|
/// unsafe {
|
|
|
|
|
/// let layout = Layout::new::<u16>();
|
|
|
|
|
/// let ptr = alloc_zeroed(layout);
|
|
|
|
|
///
|
|
|
|
|
/// assert_eq!(*(ptr as *mut u16), 0);
|
|
|
|
|
///
|
|
|
|
|
/// dealloc(ptr, layout);
|
|
|
|
|
/// }
|
|
|
|
|
/// ```
|
2018-05-31 19:13:37 +02:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 09:10:01 +02:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
|
|
|
|
__rust_alloc_zeroed(layout.size(), layout.align())
|
2014-05-11 23:41:15 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 18:36:51 +02:00
|
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
2018-04-04 19:15:22 +02:00
|
|
|
|
unsafe impl Alloc for Global {
|
|
|
|
|
#[inline]
|
2018-05-31 08:57:43 +02:00
|
|
|
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
2018-05-31 09:10:01 +02:00
|
|
|
|
NonNull::new(alloc(layout)).ok_or(AllocErr)
|
2018-04-04 19:15:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-05-31 08:57:43 +02:00
|
|
|
|
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
2018-05-31 09:10:01 +02:00
|
|
|
|
dealloc(ptr.as_ptr(), layout)
|
2018-04-04 19:15:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
|
unsafe fn realloc(&mut self,
|
2018-05-31 08:57:43 +02:00
|
|
|
|
ptr: NonNull<u8>,
|
2018-04-04 19:15:22 +02:00
|
|
|
|
layout: Layout,
|
|
|
|
|
new_size: usize)
|
2018-05-31 08:57:43 +02:00
|
|
|
|
-> Result<NonNull<u8>, AllocErr>
|
2018-04-04 19:15:22 +02:00
|
|
|
|
{
|
2018-05-31 09:10:01 +02:00
|
|
|
|
NonNull::new(realloc(ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
|
2018-04-04 19:15:22 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2018-05-31 08:57:43 +02:00
|
|
|
|
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
2018-05-31 09:10:01 +02:00
|
|
|
|
NonNull::new(alloc_zeroed(layout)).ok_or(AllocErr)
|
2018-04-04 19:15:22 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-07 04:03:14 +02:00
|
|
|
|
/// The allocator for unique pointers.
|
2018-05-08 15:10:16 +02:00
|
|
|
|
// This function must not unwind. If it does, MIR codegen 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 {
|
2017-06-03 23:54:08 +02:00
|
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2018-05-31 09:10:01 +02:00
|
|
|
|
let ptr = alloc(layout);
|
2018-04-04 19:15:22 +02:00
|
|
|
|
if !ptr.is_null() {
|
2018-05-31 08:57:43 +02:00
|
|
|
|
ptr
|
2018-04-04 19:15:22 +02:00
|
|
|
|
} else {
|
2018-06-14 00:32:30 +02:00
|
|
|
|
handle_alloc_error(layout)
|
2018-04-04 19:15:22 +02:00
|
|
|
|
}
|
2014-05-07 04:03:14 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 20:02:19 +02:00
|
|
|
|
#[cfg_attr(not(test), lang = "box_free")]
|
2016-01-28 22:59:00 +01:00
|
|
|
|
#[inline]
|
2018-04-20 03:24:53 +02:00
|
|
|
|
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
|
|
|
|
let ptr = ptr.as_ptr();
|
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 {
|
2017-06-03 23:54:08 +02:00
|
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2018-05-31 09:10:01 +02:00
|
|
|
|
dealloc(ptr as *mut u8, layout);
|
2016-01-28 22:59:00 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 18:23:42 +02:00
|
|
|
|
/// Abort on memory allocation error or failure.
|
|
|
|
|
///
|
|
|
|
|
/// Callers of memory allocation APIs wishing to abort computation
|
|
|
|
|
/// in response to an allocation error are encouraged to call this function,
|
|
|
|
|
/// rather than directly invoking `panic!` or similar.
|
|
|
|
|
///
|
|
|
|
|
/// The default behavior of this function is to print a message to standard error
|
|
|
|
|
/// and abort the process.
|
2018-06-14 00:32:30 +02:00
|
|
|
|
/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
|
2018-06-01 09:18:25 +02:00
|
|
|
|
///
|
2018-06-14 00:32:30 +02:00
|
|
|
|
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
|
|
|
|
|
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
|
2018-05-31 19:13:57 +02:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-24 21:03:05 +02:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-06-14 00:32:30 +02:00
|
|
|
|
pub fn handle_alloc_error(layout: Layout) -> ! {
|
2018-05-15 02:56:46 +02:00
|
|
|
|
extern "Rust" {
|
2018-04-21 06:05:13 +02:00
|
|
|
|
#[lang = "oom"]
|
2018-05-15 02:56:46 +02:00
|
|
|
|
fn oom_impl(layout: Layout) -> !;
|
2018-04-21 06:05:13 +02:00
|
|
|
|
}
|
2018-05-15 02:56:46 +02:00
|
|
|
|
unsafe { oom_impl(layout) }
|
2018-04-21 06:05:13 +02:00
|
|
|
|
}
|