Rename OOM to allocation error
The acronym is not descriptive unless one has seen it before. * Rename the `oom` function to `handle_alloc_error`. It was **stabilized in 1.28**, so if we do this at all we need to land it this cycle. * Rename `set_oom_hook` to `set_alloc_error_hook` * Rename `take_oom_hook` to `take_alloc_error_hook` Bikeshed: `alloc` v.s. `allocator`, `error` v.s. `failure`
This commit is contained in:
parent
b36917b331
commit
2b789bd057
|
@ -158,7 +158,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
|
||||||
if !ptr.is_null() {
|
if !ptr.is_null() {
|
||||||
ptr
|
ptr
|
||||||
} else {
|
} else {
|
||||||
oom(layout)
|
handle_alloc_error(layout)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -184,13 +184,13 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
|
||||||
///
|
///
|
||||||
/// The default behavior of this function is to print a message to standard error
|
/// The default behavior of this function is to print a message to standard error
|
||||||
/// and abort the process.
|
/// and abort the process.
|
||||||
/// It can be replaced with [`set_oom_hook`] and [`take_oom_hook`].
|
/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
|
||||||
///
|
///
|
||||||
/// [`set_oom_hook`]: ../../std/alloc/fn.set_oom_hook.html
|
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
|
||||||
/// [`take_oom_hook`]: ../../std/alloc/fn.take_oom_hook.html
|
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
|
||||||
#[stable(feature = "global_alloc", since = "1.28.0")]
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
||||||
#[rustc_allocator_nounwind]
|
#[rustc_allocator_nounwind]
|
||||||
pub fn oom(layout: Layout) -> ! {
|
pub fn handle_alloc_error(layout: Layout) -> ! {
|
||||||
#[allow(improper_ctypes)]
|
#[allow(improper_ctypes)]
|
||||||
extern "Rust" {
|
extern "Rust" {
|
||||||
#[lang = "oom"]
|
#[lang = "oom"]
|
||||||
|
@ -204,14 +204,14 @@ mod tests {
|
||||||
extern crate test;
|
extern crate test;
|
||||||
use self::test::Bencher;
|
use self::test::Bencher;
|
||||||
use boxed::Box;
|
use boxed::Box;
|
||||||
use alloc::{Global, Alloc, Layout, oom};
|
use alloc::{Global, Alloc, Layout, handle_alloc_error};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allocate_zeroed() {
|
fn allocate_zeroed() {
|
||||||
unsafe {
|
unsafe {
|
||||||
let layout = Layout::from_size_align(1024, 1).unwrap();
|
let layout = Layout::from_size_align(1024, 1).unwrap();
|
||||||
let ptr = Global.alloc_zeroed(layout.clone())
|
let ptr = Global.alloc_zeroed(layout.clone())
|
||||||
.unwrap_or_else(|_| oom(layout));
|
.unwrap_or_else(|_| handle_alloc_error(layout));
|
||||||
|
|
||||||
let mut i = ptr.cast::<u8>().as_ptr();
|
let mut i = ptr.cast::<u8>().as_ptr();
|
||||||
let end = i.offset(layout.size() as isize);
|
let end = i.offset(layout.size() as isize);
|
||||||
|
|
|
@ -32,7 +32,7 @@ use core::hash::{Hash, Hasher};
|
||||||
use core::{isize, usize};
|
use core::{isize, usize};
|
||||||
use core::convert::From;
|
use core::convert::From;
|
||||||
|
|
||||||
use alloc::{Global, Alloc, Layout, box_free, oom};
|
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
|
||||||
use boxed::Box;
|
use boxed::Box;
|
||||||
use string::String;
|
use string::String;
|
||||||
use vec::Vec;
|
use vec::Vec;
|
||||||
|
@ -554,7 +554,7 @@ impl<T: ?Sized> Arc<T> {
|
||||||
let layout = Layout::for_value(&*fake_ptr);
|
let layout = Layout::for_value(&*fake_ptr);
|
||||||
|
|
||||||
let mem = Global.alloc(layout)
|
let mem = Global.alloc(layout)
|
||||||
.unwrap_or_else(|_| oom(layout));
|
.unwrap_or_else(|_| handle_alloc_error(layout));
|
||||||
|
|
||||||
// Initialize the real ArcInner
|
// Initialize the real ArcInner
|
||||||
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner<T>;
|
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner<T>;
|
||||||
|
|
|
@ -14,7 +14,7 @@ use core::ops::Drop;
|
||||||
use core::ptr::{self, NonNull, Unique};
|
use core::ptr::{self, NonNull, Unique};
|
||||||
use core::slice;
|
use core::slice;
|
||||||
|
|
||||||
use alloc::{Alloc, Layout, Global, oom};
|
use alloc::{Alloc, Layout, Global, handle_alloc_error};
|
||||||
use alloc::CollectionAllocErr;
|
use alloc::CollectionAllocErr;
|
||||||
use alloc::CollectionAllocErr::*;
|
use alloc::CollectionAllocErr::*;
|
||||||
use boxed::Box;
|
use boxed::Box;
|
||||||
|
@ -104,7 +104,7 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
};
|
};
|
||||||
match result {
|
match result {
|
||||||
Ok(ptr) => ptr.cast(),
|
Ok(ptr) => ptr.cast(),
|
||||||
Err(_) => oom(layout),
|
Err(_) => handle_alloc_error(layout),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -319,7 +319,9 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
new_size);
|
new_size);
|
||||||
match ptr_res {
|
match ptr_res {
|
||||||
Ok(ptr) => (new_cap, ptr.cast().into()),
|
Ok(ptr) => (new_cap, ptr.cast().into()),
|
||||||
Err(_) => oom(Layout::from_size_align_unchecked(new_size, cur.align())),
|
Err(_) => handle_alloc_error(
|
||||||
|
Layout::from_size_align_unchecked(new_size, cur.align())
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
@ -328,7 +330,7 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
|
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
|
||||||
match self.a.alloc_array::<T>(new_cap) {
|
match self.a.alloc_array::<T>(new_cap) {
|
||||||
Ok(ptr) => (new_cap, ptr.into()),
|
Ok(ptr) => (new_cap, ptr.into()),
|
||||||
Err(_) => oom(Layout::array::<T>(new_cap).unwrap()),
|
Err(_) => handle_alloc_error(Layout::array::<T>(new_cap).unwrap()),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -611,7 +613,9 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
old_layout,
|
old_layout,
|
||||||
new_size) {
|
new_size) {
|
||||||
Ok(p) => self.ptr = p.cast().into(),
|
Ok(p) => self.ptr = p.cast().into(),
|
||||||
Err(_) => oom(Layout::from_size_align_unchecked(new_size, align)),
|
Err(_) => handle_alloc_error(
|
||||||
|
Layout::from_size_align_unchecked(new_size, align)
|
||||||
|
),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
self.cap = amount;
|
self.cap = amount;
|
||||||
|
@ -673,7 +677,7 @@ impl<T, A: Alloc> RawVec<T, A> {
|
||||||
};
|
};
|
||||||
|
|
||||||
match (&res, fallibility) {
|
match (&res, fallibility) {
|
||||||
(Err(AllocErr), Infallible) => oom(new_layout),
|
(Err(AllocErr), Infallible) => handle_alloc_error(new_layout),
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -259,7 +259,7 @@ use core::ops::CoerceUnsized;
|
||||||
use core::ptr::{self, NonNull};
|
use core::ptr::{self, NonNull};
|
||||||
use core::convert::From;
|
use core::convert::From;
|
||||||
|
|
||||||
use alloc::{Global, Alloc, Layout, box_free, oom};
|
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
|
||||||
use string::String;
|
use string::String;
|
||||||
use vec::Vec;
|
use vec::Vec;
|
||||||
|
|
||||||
|
@ -662,7 +662,7 @@ impl<T: ?Sized> Rc<T> {
|
||||||
let layout = Layout::for_value(&*fake_ptr);
|
let layout = Layout::for_value(&*fake_ptr);
|
||||||
|
|
||||||
let mem = Global.alloc(layout)
|
let mem = Global.alloc(layout)
|
||||||
.unwrap_or_else(|_| oom(layout));
|
.unwrap_or_else(|_| handle_alloc_error(layout));
|
||||||
|
|
||||||
// Initialize the real RcBox
|
// Initialize the real RcBox
|
||||||
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox<T>;
|
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox<T>;
|
||||||
|
|
|
@ -492,10 +492,10 @@ pub unsafe trait GlobalAlloc {
|
||||||
/// library that aborts on memory exhaustion.)
|
/// library that aborts on memory exhaustion.)
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
#[stable(feature = "global_alloc", since = "1.28.0")]
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
||||||
unsafe fn alloc(&self, layout: Layout) -> *mut u8;
|
unsafe fn alloc(&self, layout: Layout) -> *mut u8;
|
||||||
|
|
||||||
|
@ -529,10 +529,10 @@ pub unsafe trait GlobalAlloc {
|
||||||
/// just as in `alloc`.
|
/// just as in `alloc`.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
#[stable(feature = "global_alloc", since = "1.28.0")]
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
||||||
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
|
||||||
let size = layout.size();
|
let size = layout.size();
|
||||||
|
@ -589,10 +589,10 @@ pub unsafe trait GlobalAlloc {
|
||||||
/// library that aborts on memory exhaustion.)
|
/// library that aborts on memory exhaustion.)
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to a
|
/// Clients wishing to abort computation in response to a
|
||||||
/// reallocation error are encouraged to call the [`oom`] function,
|
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
#[stable(feature = "global_alloc", since = "1.28.0")]
|
#[stable(feature = "global_alloc", since = "1.28.0")]
|
||||||
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
|
||||||
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
|
||||||
|
@ -733,10 +733,10 @@ pub unsafe trait Alloc {
|
||||||
/// library that aborts on memory exhaustion.)
|
/// library that aborts on memory exhaustion.)
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr>;
|
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr>;
|
||||||
|
|
||||||
/// Deallocate the memory referenced by `ptr`.
|
/// Deallocate the memory referenced by `ptr`.
|
||||||
|
@ -843,10 +843,10 @@ pub unsafe trait Alloc {
|
||||||
/// library that aborts on memory exhaustion.)
|
/// library that aborts on memory exhaustion.)
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to a
|
/// Clients wishing to abort computation in response to a
|
||||||
/// reallocation error are encouraged to call the [`oom`] function,
|
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn realloc(&mut self,
|
unsafe fn realloc(&mut self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
layout: Layout,
|
layout: Layout,
|
||||||
|
@ -889,10 +889,10 @@ pub unsafe trait Alloc {
|
||||||
/// constraints, just as in `alloc`.
|
/// constraints, just as in `alloc`.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
|
||||||
let size = layout.size();
|
let size = layout.size();
|
||||||
let p = self.alloc(layout);
|
let p = self.alloc(layout);
|
||||||
|
@ -917,10 +917,10 @@ pub unsafe trait Alloc {
|
||||||
/// constraints, just as in `alloc`.
|
/// constraints, just as in `alloc`.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
|
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
|
||||||
let usable_size = self.usable_size(&layout);
|
let usable_size = self.usable_size(&layout);
|
||||||
self.alloc(layout).map(|p| Excess(p, usable_size.1))
|
self.alloc(layout).map(|p| Excess(p, usable_size.1))
|
||||||
|
@ -941,10 +941,10 @@ pub unsafe trait Alloc {
|
||||||
/// constraints, just as in `realloc`.
|
/// constraints, just as in `realloc`.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to a
|
/// Clients wishing to abort computation in response to a
|
||||||
/// reallocation error are encouraged to call the [`oom`] function,
|
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn realloc_excess(&mut self,
|
unsafe fn realloc_excess(&mut self,
|
||||||
ptr: NonNull<u8>,
|
ptr: NonNull<u8>,
|
||||||
layout: Layout,
|
layout: Layout,
|
||||||
|
@ -986,7 +986,7 @@ pub unsafe trait Alloc {
|
||||||
/// unable to assert that the memory block referenced by `ptr`
|
/// unable to assert that the memory block referenced by `ptr`
|
||||||
/// could fit `layout`.
|
/// could fit `layout`.
|
||||||
///
|
///
|
||||||
/// Note that one cannot pass `CannotReallocInPlace` to the `oom`
|
/// Note that one cannot pass `CannotReallocInPlace` to the `handle_alloc_error`
|
||||||
/// function; clients are expected either to be able to recover from
|
/// function; clients are expected either to be able to recover from
|
||||||
/// `grow_in_place` failures without aborting, or to fall back on
|
/// `grow_in_place` failures without aborting, or to fall back on
|
||||||
/// another reallocation method before resorting to an abort.
|
/// another reallocation method before resorting to an abort.
|
||||||
|
@ -1041,7 +1041,7 @@ pub unsafe trait Alloc {
|
||||||
/// unable to assert that the memory block referenced by `ptr`
|
/// unable to assert that the memory block referenced by `ptr`
|
||||||
/// could fit `layout`.
|
/// could fit `layout`.
|
||||||
///
|
///
|
||||||
/// Note that one cannot pass `CannotReallocInPlace` to the `oom`
|
/// Note that one cannot pass `CannotReallocInPlace` to the `handle_alloc_error`
|
||||||
/// function; clients are expected either to be able to recover from
|
/// function; clients are expected either to be able to recover from
|
||||||
/// `shrink_in_place` failures without aborting, or to fall back
|
/// `shrink_in_place` failures without aborting, or to fall back
|
||||||
/// on another reallocation method before resorting to an abort.
|
/// on another reallocation method before resorting to an abort.
|
||||||
|
@ -1090,10 +1090,10 @@ pub unsafe trait Alloc {
|
||||||
/// will *not* yield undefined behavior.
|
/// will *not* yield undefined behavior.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
|
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
|
||||||
where Self: Sized
|
where Self: Sized
|
||||||
{
|
{
|
||||||
|
@ -1159,10 +1159,10 @@ pub unsafe trait Alloc {
|
||||||
/// Always returns `Err` on arithmetic overflow.
|
/// Always returns `Err` on arithmetic overflow.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to an
|
/// Clients wishing to abort computation in response to an
|
||||||
/// allocation error are encouraged to call the [`oom`] function,
|
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
|
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
|
||||||
where Self: Sized
|
where Self: Sized
|
||||||
{
|
{
|
||||||
|
@ -1206,10 +1206,10 @@ pub unsafe trait Alloc {
|
||||||
/// Always returns `Err` on arithmetic overflow.
|
/// Always returns `Err` on arithmetic overflow.
|
||||||
///
|
///
|
||||||
/// Clients wishing to abort computation in response to a
|
/// Clients wishing to abort computation in response to a
|
||||||
/// reallocation error are encouraged to call the [`oom`] function,
|
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
|
||||||
/// rather than directly invoking `panic!` or similar.
|
/// rather than directly invoking `panic!` or similar.
|
||||||
///
|
///
|
||||||
/// [`oom`]: ../../alloc/alloc/fn.oom.html
|
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
|
||||||
unsafe fn realloc_array<T>(&mut self,
|
unsafe fn realloc_array<T>(&mut self,
|
||||||
ptr: NonNull<T>,
|
ptr: NonNull<T>,
|
||||||
n_old: usize,
|
n_old: usize,
|
||||||
|
|
|
@ -88,38 +88,38 @@ pub use alloc_system::System;
|
||||||
|
|
||||||
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
|
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
|
||||||
|
|
||||||
/// Registers a custom OOM hook, replacing any that was previously registered.
|
/// Registers a custom allocation error hook, replacing any that was previously registered.
|
||||||
///
|
///
|
||||||
/// The OOM hook is invoked when an infallible memory allocation fails, before
|
/// The allocation error hook is invoked when an infallible memory allocation fails, before
|
||||||
/// the runtime aborts. The default hook prints a message to standard error,
|
/// the runtime aborts. The default hook prints a message to standard error,
|
||||||
/// but this behavior can be customized with the [`set_oom_hook`] and
|
/// but this behavior can be customized with the [`set_alloc_error_hook`] and
|
||||||
/// [`take_oom_hook`] functions.
|
/// [`take_alloc_error_hook`] functions.
|
||||||
///
|
///
|
||||||
/// The hook is provided with a `Layout` struct which contains information
|
/// The hook is provided with a `Layout` struct which contains information
|
||||||
/// about the allocation that failed.
|
/// about the allocation that failed.
|
||||||
///
|
///
|
||||||
/// The OOM hook is a global resource.
|
/// The allocation error hook is a global resource.
|
||||||
#[unstable(feature = "oom_hook", issue = "51245")]
|
#[unstable(feature = "alloc_error_hook", issue = "51245")]
|
||||||
pub fn set_oom_hook(hook: fn(Layout)) {
|
pub fn set_alloc_error_hook(hook: fn(Layout)) {
|
||||||
HOOK.store(hook as *mut (), Ordering::SeqCst);
|
HOOK.store(hook as *mut (), Ordering::SeqCst);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Unregisters the current OOM hook, returning it.
|
/// Unregisters the current allocation error hook, returning it.
|
||||||
///
|
///
|
||||||
/// *See also the function [`set_oom_hook`].*
|
/// *See also the function [`set_alloc_error_hook`].*
|
||||||
///
|
///
|
||||||
/// If no custom hook is registered, the default hook will be returned.
|
/// If no custom hook is registered, the default hook will be returned.
|
||||||
#[unstable(feature = "oom_hook", issue = "51245")]
|
#[unstable(feature = "alloc_error_hook", issue = "51245")]
|
||||||
pub fn take_oom_hook() -> fn(Layout) {
|
pub fn take_alloc_error_hook() -> fn(Layout) {
|
||||||
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
|
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
|
||||||
if hook.is_null() {
|
if hook.is_null() {
|
||||||
default_oom_hook
|
default_alloc_error_hook
|
||||||
} else {
|
} else {
|
||||||
unsafe { mem::transmute(hook) }
|
unsafe { mem::transmute(hook) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn default_oom_hook(layout: Layout) {
|
fn default_alloc_error_hook(layout: Layout) {
|
||||||
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
|
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -130,7 +130,7 @@ fn default_oom_hook(layout: Layout) {
|
||||||
pub extern fn rust_oom(layout: Layout) -> ! {
|
pub extern fn rust_oom(layout: Layout) -> ! {
|
||||||
let hook = HOOK.load(Ordering::SeqCst);
|
let hook = HOOK.load(Ordering::SeqCst);
|
||||||
let hook: fn(Layout) = if hook.is_null() {
|
let hook: fn(Layout) = if hook.is_null() {
|
||||||
default_oom_hook
|
default_alloc_error_hook
|
||||||
} else {
|
} else {
|
||||||
unsafe { mem::transmute(hook) }
|
unsafe { mem::transmute(hook) }
|
||||||
};
|
};
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
// option. This file may not be copied, modified, or distributed
|
// option. This file may not be copied, modified, or distributed
|
||||||
// except according to those terms.
|
// except according to those terms.
|
||||||
|
|
||||||
use alloc::{Global, Alloc, Layout, LayoutErr, CollectionAllocErr, oom};
|
use alloc::{Global, Alloc, Layout, LayoutErr, CollectionAllocErr, handle_alloc_error};
|
||||||
use hash::{BuildHasher, Hash, Hasher};
|
use hash::{BuildHasher, Hash, Hasher};
|
||||||
use marker;
|
use marker;
|
||||||
use mem::{size_of, needs_drop};
|
use mem::{size_of, needs_drop};
|
||||||
|
@ -699,7 +699,7 @@ impl<K, V> RawTable<K, V> {
|
||||||
// point into it.
|
// point into it.
|
||||||
let (layout, _) = calculate_layout::<K, V>(capacity)?;
|
let (layout, _) = calculate_layout::<K, V>(capacity)?;
|
||||||
let buffer = Global.alloc(layout).map_err(|e| match fallibility {
|
let buffer = Global.alloc(layout).map_err(|e| match fallibility {
|
||||||
Infallible => oom(layout),
|
Infallible => handle_alloc_error(layout),
|
||||||
Fallible => e,
|
Fallible => e,
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
|
|
|
@ -10,11 +10,13 @@
|
||||||
|
|
||||||
#![feature(allocator_api, nonnull)]
|
#![feature(allocator_api, nonnull)]
|
||||||
|
|
||||||
use std::alloc::{Alloc, Global, Layout, oom};
|
use std::alloc::{Alloc, Global, Layout, handle_alloc_error};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
unsafe {
|
unsafe {
|
||||||
let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| oom(Layout::new::<i32>()));
|
let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| {
|
||||||
|
handle_alloc_error(Layout::new::<i32>())
|
||||||
|
});
|
||||||
*ptr.as_ptr() = 4;
|
*ptr.as_ptr() = 4;
|
||||||
assert_eq!(*ptr.as_ptr(), 4);
|
assert_eq!(*ptr.as_ptr(), 4);
|
||||||
Global.dealloc_one(ptr);
|
Global.dealloc_one(ptr);
|
||||||
|
|
|
@ -15,7 +15,7 @@
|
||||||
|
|
||||||
#![feature(heap_api, allocator_api)]
|
#![feature(heap_api, allocator_api)]
|
||||||
|
|
||||||
use std::alloc::{Global, Alloc, Layout, oom};
|
use std::alloc::{Global, Alloc, Layout, handle_alloc_error};
|
||||||
use std::ptr::{self, NonNull};
|
use std::ptr::{self, NonNull};
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
|
@ -50,7 +50,7 @@ unsafe fn test_triangle() -> bool {
|
||||||
println!("allocate({:?})", layout);
|
println!("allocate({:?})", layout);
|
||||||
}
|
}
|
||||||
|
|
||||||
let ret = Global.alloc(layout).unwrap_or_else(|_| oom(layout));
|
let ret = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||||
|
|
||||||
if PRINT {
|
if PRINT {
|
||||||
println!("allocate({:?}) = {:?}", layout, ret);
|
println!("allocate({:?}) = {:?}", layout, ret);
|
||||||
|
@ -73,7 +73,9 @@ unsafe fn test_triangle() -> bool {
|
||||||
}
|
}
|
||||||
|
|
||||||
let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
|
let ret = Global.realloc(NonNull::new_unchecked(ptr), old, new.size())
|
||||||
.unwrap_or_else(|_| oom(Layout::from_size_align_unchecked(new.size(), old.align())));
|
.unwrap_or_else(|_| handle_alloc_error(
|
||||||
|
Layout::from_size_align_unchecked(new.size(), old.align())
|
||||||
|
));
|
||||||
|
|
||||||
if PRINT {
|
if PRINT {
|
||||||
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
|
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",
|
||||||
|
|
|
@ -12,7 +12,7 @@
|
||||||
|
|
||||||
#![feature(allocator_api)]
|
#![feature(allocator_api)]
|
||||||
|
|
||||||
use std::alloc::{Alloc, Global, Layout, oom};
|
use std::alloc::{Alloc, Global, Layout, handle_alloc_error};
|
||||||
use std::ptr::NonNull;
|
use std::ptr::NonNull;
|
||||||
|
|
||||||
struct arena(());
|
struct arena(());
|
||||||
|
@ -33,7 +33,7 @@ struct Ccx {
|
||||||
fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
|
fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let layout = Layout::new::<Bcx>();
|
let layout = Layout::new::<Bcx>();
|
||||||
let ptr = Global.alloc(layout).unwrap_or_else(|_| oom(layout));
|
let ptr = Global.alloc(layout).unwrap_or_else(|_| handle_alloc_error(layout));
|
||||||
&*(ptr.as_ptr() as *const _)
|
&*(ptr.as_ptr() as *const _)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue