2018-05-31 11:23:42 -05:00
|
|
|
|
//! Memory allocation APIs
|
|
|
|
|
|
2018-05-31 11:36:51 -05:00
|
|
|
|
#![stable(feature = "alloc_module", since = "1.28.0")]
|
2015-06-09 13:52:41 -05:00
|
|
|
|
|
2020-03-24 05:45:38 -05:00
|
|
|
|
use core::intrinsics::{self, min_align_of_val, size_of_val};
|
2019-02-03 01:27:44 -06:00
|
|
|
|
use core::ptr::{NonNull, Unique};
|
2020-03-25 15:12:12 -05:00
|
|
|
|
use core::usize;
|
2015-02-07 17:49:54 -06:00
|
|
|
|
|
2018-05-31 11:36:51 -05:00
|
|
|
|
#[stable(feature = "alloc_module", since = "1.28.0")]
|
2018-04-02 03:38:07 -05:00
|
|
|
|
#[doc(inline)]
|
2018-04-03 14:05:10 -05:00
|
|
|
|
pub use core::alloc::*;
|
2018-04-02 03:38:07 -05:00
|
|
|
|
|
2019-08-01 17:40:56 -05:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests;
|
|
|
|
|
|
2018-04-03 10:12:57 -05:00
|
|
|
|
extern "Rust" {
|
2018-11-12 02:00:39 -06: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 09:05:50 -05:00
|
|
|
|
#[rustc_allocator]
|
2017-08-22 16:36:49 -05:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
|
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
|
2017-08-22 16:36:49 -05:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
|
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
|
2017-08-22 16:36:49 -05:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2019-12-22 16:42:04 -06:00
|
|
|
|
fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
|
2017-08-22 16:36:49 -05:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-04-03 10:12:57 -05:00
|
|
|
|
fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;
|
2015-02-07 17:49:54 -06:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 11:23:42 -05:00
|
|
|
|
/// The global memory allocator.
|
|
|
|
|
///
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// This type implements the [`AllocRef`] trait by forwarding calls
|
2018-05-31 11:23:42 -05:00
|
|
|
|
/// to the allocator registered with the `#[global_allocator]` attribute
|
|
|
|
|
/// if there is one, or the `std` crate’s default.
|
2019-02-05 05:26:19 -06: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 09:48:57 -05:00
|
|
|
|
///
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// [`AllocRef`]: trait.AllocRef.html
|
2018-05-31 11:36:51 -05:00
|
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
2017-05-23 07:47:41 -05:00
|
|
|
|
#[derive(Copy, Clone, Default, Debug)]
|
2018-04-03 07:43:34 -05:00
|
|
|
|
pub struct Global;
|
2017-05-23 07:47:41 -05:00
|
|
|
|
|
2018-05-31 11:23:42 -05: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
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
|
2018-05-31 11:23:42 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::alloc`].
|
2018-08-14 07:37:12 -05:00
|
|
|
|
///
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`Global`]: struct.Global.html
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// [`AllocRef`]: trait.AllocRef.html
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`GlobalAlloc::alloc`]: trait.GlobalAlloc.html#tymethod.alloc
|
|
|
|
|
///
|
2018-08-14 07:37:12 -05: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 12:13:37 -05:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 02:10:01 -05:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn alloc(layout: Layout) -> *mut u8 {
|
|
|
|
|
__rust_alloc(layout.size(), layout.align())
|
|
|
|
|
}
|
2017-05-23 07:47:41 -05:00
|
|
|
|
|
2018-05-31 11:23:42 -05: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
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
|
2018-05-31 11:23:42 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::dealloc`].
|
2019-05-04 09:48:57 -05:00
|
|
|
|
///
|
|
|
|
|
/// [`Global`]: struct.Global.html
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// [`AllocRef`]: trait.AllocRef.html
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`GlobalAlloc::dealloc`]: trait.GlobalAlloc.html#tymethod.dealloc
|
2018-05-31 12:13:37 -05:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 02:10:01 -05:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn dealloc(ptr: *mut u8, layout: Layout) {
|
|
|
|
|
__rust_dealloc(ptr, layout.size(), layout.align())
|
|
|
|
|
}
|
2017-05-23 07:47:41 -05:00
|
|
|
|
|
2018-05-31 11:23:42 -05: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
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
|
2018-05-31 11:23:42 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::realloc`].
|
2019-05-04 09:48:57 -05:00
|
|
|
|
///
|
|
|
|
|
/// [`Global`]: struct.Global.html
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// [`AllocRef`]: trait.AllocRef.html
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`GlobalAlloc::realloc`]: trait.GlobalAlloc.html#method.realloc
|
2018-05-31 12:13:37 -05:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 02:10:01 -05: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 07:47:41 -05:00
|
|
|
|
|
2018-05-31 11:23:42 -05: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
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// of the [`Global`] type when it and the [`AllocRef`] trait become stable.
|
2018-05-31 11:23:42 -05:00
|
|
|
|
///
|
|
|
|
|
/// # Safety
|
|
|
|
|
///
|
|
|
|
|
/// See [`GlobalAlloc::alloc_zeroed`].
|
2018-08-14 07:37:12 -05:00
|
|
|
|
///
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`Global`]: struct.Global.html
|
2020-01-22 18:49:29 -06:00
|
|
|
|
/// [`AllocRef`]: trait.AllocRef.html
|
2019-05-04 09:48:57 -05:00
|
|
|
|
/// [`GlobalAlloc::alloc_zeroed`]: trait.GlobalAlloc.html#method.alloc_zeroed
|
|
|
|
|
///
|
2018-08-14 07:37:12 -05: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 12:13:37 -05:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-31 02:10:01 -05:00
|
|
|
|
#[inline]
|
|
|
|
|
pub unsafe fn alloc_zeroed(layout: Layout) -> *mut u8 {
|
|
|
|
|
__rust_alloc_zeroed(layout.size(), layout.align())
|
2014-05-11 16:41:15 -05:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 11:36:51 -05:00
|
|
|
|
#[unstable(feature = "allocator_api", issue = "32838")]
|
2020-01-22 18:49:29 -06:00
|
|
|
|
unsafe impl AllocRef for Global {
|
2018-04-04 12:15:22 -05:00
|
|
|
|
#[inline]
|
2020-03-26 11:11:47 -05:00
|
|
|
|
fn alloc(&mut self, layout: Layout, init: AllocInit) -> Result<MemoryBlock, AllocErr> {
|
|
|
|
|
unsafe {
|
2020-03-25 15:12:12 -05:00
|
|
|
|
let size = layout.size();
|
|
|
|
|
if size == 0 {
|
2020-03-28 14:21:26 -05:00
|
|
|
|
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
|
2020-03-26 11:11:47 -05:00
|
|
|
|
} else {
|
2020-03-24 05:45:38 -05:00
|
|
|
|
let raw_ptr = match init {
|
|
|
|
|
AllocInit::Uninitialized => alloc(layout),
|
|
|
|
|
AllocInit::Zeroed => alloc_zeroed(layout),
|
|
|
|
|
};
|
|
|
|
|
let ptr = NonNull::new(raw_ptr).ok_or(AllocErr)?;
|
2020-03-28 14:21:26 -05:00
|
|
|
|
Ok(MemoryBlock { ptr, size })
|
2020-03-24 05:45:38 -05:00
|
|
|
|
}
|
2020-03-07 05:04:40 -06:00
|
|
|
|
}
|
2018-04-04 12:15:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-25 15:12:12 -05:00
|
|
|
|
unsafe fn dealloc(&mut self, ptr: NonNull<u8>, layout: Layout) {
|
|
|
|
|
if layout.size() != 0 {
|
|
|
|
|
dealloc(ptr.as_ptr(), layout)
|
2020-03-07 05:04:40 -06:00
|
|
|
|
}
|
2018-04-04 12:15:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-24 05:45:38 -05:00
|
|
|
|
unsafe fn grow(
|
2019-12-22 16:42:04 -06:00
|
|
|
|
&mut self,
|
2020-03-25 15:12:12 -05:00
|
|
|
|
ptr: NonNull<u8>,
|
|
|
|
|
layout: Layout,
|
2019-12-22 16:42:04 -06:00
|
|
|
|
new_size: usize,
|
2020-03-24 05:45:38 -05:00
|
|
|
|
placement: ReallocPlacement,
|
|
|
|
|
init: AllocInit,
|
2020-03-25 15:12:12 -05:00
|
|
|
|
) -> Result<MemoryBlock, AllocErr> {
|
2020-03-28 14:21:26 -05:00
|
|
|
|
let size = layout.size();
|
2020-03-24 05:45:38 -05:00
|
|
|
|
debug_assert!(
|
2020-03-28 14:21:26 -05:00
|
|
|
|
new_size >= size,
|
2020-03-26 11:11:47 -05:00
|
|
|
|
"`new_size` must be greater than or equal to `memory.size()`"
|
2020-03-24 05:45:38 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-03-28 14:21:26 -05:00
|
|
|
|
if size == new_size {
|
|
|
|
|
return Ok(MemoryBlock { ptr, size });
|
2020-03-24 05:45:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match placement {
|
2020-03-25 15:12:12 -05:00
|
|
|
|
ReallocPlacement::InPlace => Err(AllocErr),
|
|
|
|
|
ReallocPlacement::MayMove if layout.size() == 0 => {
|
|
|
|
|
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
|
|
|
|
self.alloc(new_layout, init)
|
2020-03-26 11:11:47 -05:00
|
|
|
|
}
|
2020-03-24 05:45:38 -05:00
|
|
|
|
ReallocPlacement::MayMove => {
|
2020-03-28 19:47:05 -05:00
|
|
|
|
// `realloc` probably checks for `new_size > size` or something similar.
|
2020-03-28 14:21:26 -05:00
|
|
|
|
intrinsics::assume(new_size > size);
|
2020-03-25 15:12:12 -05:00
|
|
|
|
let ptr = realloc(ptr.as_ptr(), layout, new_size);
|
2020-03-28 19:47:05 -05:00
|
|
|
|
let memory =
|
2020-03-28 14:21:26 -05:00
|
|
|
|
MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size };
|
2020-03-28 19:47:05 -05:00
|
|
|
|
init.init_offset(memory, size);
|
2020-03-25 15:12:12 -05:00
|
|
|
|
Ok(memory)
|
2020-03-07 05:04:40 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 12:15:22 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[inline]
|
2020-03-24 05:45:38 -05:00
|
|
|
|
unsafe fn shrink(
|
|
|
|
|
&mut self,
|
2020-03-25 15:12:12 -05:00
|
|
|
|
ptr: NonNull<u8>,
|
|
|
|
|
layout: Layout,
|
2020-03-24 05:45:38 -05:00
|
|
|
|
new_size: usize,
|
|
|
|
|
placement: ReallocPlacement,
|
2020-03-25 15:12:12 -05:00
|
|
|
|
) -> Result<MemoryBlock, AllocErr> {
|
2020-03-28 14:21:26 -05:00
|
|
|
|
let size = layout.size();
|
2020-03-24 05:45:38 -05:00
|
|
|
|
debug_assert!(
|
2020-03-28 14:21:26 -05:00
|
|
|
|
new_size <= size,
|
2020-03-26 11:11:47 -05:00
|
|
|
|
"`new_size` must be smaller than or equal to `memory.size()`"
|
2020-03-24 05:45:38 -05:00
|
|
|
|
);
|
|
|
|
|
|
2020-03-28 14:21:26 -05:00
|
|
|
|
if size == new_size {
|
|
|
|
|
return Ok(MemoryBlock { ptr, size });
|
2020-03-24 05:45:38 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
match placement {
|
2020-03-25 15:12:12 -05:00
|
|
|
|
ReallocPlacement::InPlace => Err(AllocErr),
|
2020-03-26 11:11:47 -05:00
|
|
|
|
ReallocPlacement::MayMove if new_size == 0 => {
|
2020-03-25 15:12:12 -05:00
|
|
|
|
self.dealloc(ptr, layout);
|
2020-03-28 14:21:26 -05:00
|
|
|
|
Ok(MemoryBlock { ptr: layout.dangling(), size: 0 })
|
2020-03-26 11:11:47 -05:00
|
|
|
|
}
|
2020-03-24 05:45:38 -05:00
|
|
|
|
ReallocPlacement::MayMove => {
|
2020-03-28 19:47:05 -05:00
|
|
|
|
// `realloc` probably checks for `new_size < size` or something similar.
|
2020-03-28 14:21:26 -05:00
|
|
|
|
intrinsics::assume(new_size < size);
|
2020-03-25 15:12:12 -05:00
|
|
|
|
let ptr = realloc(ptr.as_ptr(), layout, new_size);
|
2020-03-28 14:21:26 -05:00
|
|
|
|
Ok(MemoryBlock { ptr: NonNull::new(ptr).ok_or(AllocErr)?, size: new_size })
|
2020-03-07 05:04:40 -06:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-04-04 12:15:22 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-05-06 21:03:14 -05:00
|
|
|
|
/// The allocator for unique pointers.
|
2018-05-08 08:10:16 -05:00
|
|
|
|
// This function must not unwind. If it does, MIR codegen will fail.
|
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 {
|
2020-03-24 05:45:38 -05:00
|
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
|
|
|
|
match Global.alloc(layout, AllocInit::Uninitialized) {
|
2020-03-28 14:21:26 -05:00
|
|
|
|
Ok(memory) => memory.ptr.as_ptr(),
|
2020-03-24 05:45:38 -05:00
|
|
|
|
Err(_) => handle_alloc_error(layout),
|
2014-05-06 21:03:14 -05:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-10 13:02:19 -05:00
|
|
|
|
#[cfg_attr(not(test), lang = "box_free")]
|
2016-01-28 15:59:00 -06:00
|
|
|
|
#[inline]
|
2020-02-11 06:02:51 -06:00
|
|
|
|
// This signature has to be the same as `Box`, otherwise an ICE will happen.
|
|
|
|
|
// When an additional parameter to `Box` is added (like `A: AllocRef`), this has to be added here as
|
|
|
|
|
// well.
|
|
|
|
|
// For example if `Box` is changed to `struct Box<T: ?Sized, A: AllocRef>(Unique<T>, A)`,
|
|
|
|
|
// this function has to be changed to `fn box_free<T: ?Sized, A: AllocRef>(Unique<T>, A)` as well.
|
2018-04-19 20:24:53 -05:00
|
|
|
|
pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
2020-02-11 06:02:51 -06:00
|
|
|
|
let size = size_of_val(ptr.as_ref());
|
|
|
|
|
let align = min_align_of_val(ptr.as_ref());
|
2020-03-24 05:45:38 -05:00
|
|
|
|
let layout = Layout::from_size_align_unchecked(size, align);
|
2020-03-25 15:12:12 -05:00
|
|
|
|
Global.dealloc(ptr.cast().into(), layout)
|
2016-01-28 15:59:00 -06:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-31 11:23:42 -05: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-13 17:32:30 -05:00
|
|
|
|
/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
|
2018-06-01 02:18:25 -05:00
|
|
|
|
///
|
2018-06-13 17:32:30 -05: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 12:13:57 -05:00
|
|
|
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
2018-05-24 14:03:05 -05:00
|
|
|
|
#[rustc_allocator_nounwind]
|
2018-06-13 17:32:30 -05:00
|
|
|
|
pub fn handle_alloc_error(layout: Layout) -> ! {
|
2018-05-14 19:56:46 -05:00
|
|
|
|
extern "Rust" {
|
2018-04-20 23:05:13 -05:00
|
|
|
|
#[lang = "oom"]
|
2018-05-14 19:56:46 -05:00
|
|
|
|
fn oom_impl(layout: Layout) -> !;
|
2018-04-20 23:05:13 -05:00
|
|
|
|
}
|
2018-05-14 19:56:46 -05:00
|
|
|
|
unsafe { oom_impl(layout) }
|
2018-04-20 23:05:13 -05:00
|
|
|
|
}
|