Skip to content

Commit d692ab4

Browse files
committed
Auto merge of #51543 - SimonSapin:oom, r=SimonSapin
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: `on` v.s. `for`, `alloc` v.s. `allocator`, `error` v.s. `failure`
2 parents 6ec1b62 + 2b789bd commit d692ab4

File tree

10 files changed

+72
-64
lines changed

10 files changed

+72
-64
lines changed

src/liballoc/alloc.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
158158
if !ptr.is_null() {
159159
ptr
160160
} else {
161-
oom(layout)
161+
handle_alloc_error(layout)
162162
}
163163
}
164164
}
@@ -184,13 +184,13 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: Unique<T>) {
184184
///
185185
/// The default behavior of this function is to print a message to standard error
186186
/// and abort the process.
187-
/// It can be replaced with [`set_oom_hook`] and [`take_oom_hook`].
187+
/// It can be replaced with [`set_alloc_error_hook`] and [`take_alloc_error_hook`].
188188
///
189-
/// [`set_oom_hook`]: ../../std/alloc/fn.set_oom_hook.html
190-
/// [`take_oom_hook`]: ../../std/alloc/fn.take_oom_hook.html
189+
/// [`set_alloc_error_hook`]: ../../std/alloc/fn.set_alloc_error_hook.html
190+
/// [`take_alloc_error_hook`]: ../../std/alloc/fn.take_alloc_error_hook.html
191191
#[stable(feature = "global_alloc", since = "1.28.0")]
192192
#[rustc_allocator_nounwind]
193-
pub fn oom(layout: Layout) -> ! {
193+
pub fn handle_alloc_error(layout: Layout) -> ! {
194194
#[allow(improper_ctypes)]
195195
extern "Rust" {
196196
#[lang = "oom"]
@@ -204,14 +204,14 @@ mod tests {
204204
extern crate test;
205205
use self::test::Bencher;
206206
use boxed::Box;
207-
use alloc::{Global, Alloc, Layout, oom};
207+
use alloc::{Global, Alloc, Layout, handle_alloc_error};
208208

209209
#[test]
210210
fn allocate_zeroed() {
211211
unsafe {
212212
let layout = Layout::from_size_align(1024, 1).unwrap();
213213
let ptr = Global.alloc_zeroed(layout.clone())
214-
.unwrap_or_else(|_| oom(layout));
214+
.unwrap_or_else(|_| handle_alloc_error(layout));
215215

216216
let mut i = ptr.cast::<u8>().as_ptr();
217217
let end = i.offset(layout.size() as isize);

src/liballoc/arc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ use core::hash::{Hash, Hasher};
3232
use core::{isize, usize};
3333
use core::convert::From;
3434

35-
use alloc::{Global, Alloc, Layout, box_free, oom};
35+
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
3636
use boxed::Box;
3737
use string::String;
3838
use vec::Vec;
@@ -554,7 +554,7 @@ impl<T: ?Sized> Arc<T> {
554554
let layout = Layout::for_value(&*fake_ptr);
555555

556556
let mem = Global.alloc(layout)
557-
.unwrap_or_else(|_| oom(layout));
557+
.unwrap_or_else(|_| handle_alloc_error(layout));
558558

559559
// Initialize the real ArcInner
560560
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut ArcInner<T>;

src/liballoc/raw_vec.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use core::ops::Drop;
1414
use core::ptr::{self, NonNull, Unique};
1515
use core::slice;
1616

17-
use alloc::{Alloc, Layout, Global, oom};
17+
use alloc::{Alloc, Layout, Global, handle_alloc_error};
1818
use alloc::CollectionAllocErr;
1919
use alloc::CollectionAllocErr::*;
2020
use boxed::Box;
@@ -104,7 +104,7 @@ impl<T, A: Alloc> RawVec<T, A> {
104104
};
105105
match result {
106106
Ok(ptr) => ptr.cast(),
107-
Err(_) => oom(layout),
107+
Err(_) => handle_alloc_error(layout),
108108
}
109109
};
110110

@@ -319,7 +319,9 @@ impl<T, A: Alloc> RawVec<T, A> {
319319
new_size);
320320
match ptr_res {
321321
Ok(ptr) => (new_cap, ptr.cast().into()),
322-
Err(_) => oom(Layout::from_size_align_unchecked(new_size, cur.align())),
322+
Err(_) => handle_alloc_error(
323+
Layout::from_size_align_unchecked(new_size, cur.align())
324+
),
323325
}
324326
}
325327
None => {
@@ -328,7 +330,7 @@ impl<T, A: Alloc> RawVec<T, A> {
328330
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
329331
match self.a.alloc_array::<T>(new_cap) {
330332
Ok(ptr) => (new_cap, ptr.into()),
331-
Err(_) => oom(Layout::array::<T>(new_cap).unwrap()),
333+
Err(_) => handle_alloc_error(Layout::array::<T>(new_cap).unwrap()),
332334
}
333335
}
334336
};
@@ -611,7 +613,9 @@ impl<T, A: Alloc> RawVec<T, A> {
611613
old_layout,
612614
new_size) {
613615
Ok(p) => self.ptr = p.cast().into(),
614-
Err(_) => oom(Layout::from_size_align_unchecked(new_size, align)),
616+
Err(_) => handle_alloc_error(
617+
Layout::from_size_align_unchecked(new_size, align)
618+
),
615619
}
616620
}
617621
self.cap = amount;
@@ -673,7 +677,7 @@ impl<T, A: Alloc> RawVec<T, A> {
673677
};
674678

675679
match (&res, fallibility) {
676-
(Err(AllocErr), Infallible) => oom(new_layout),
680+
(Err(AllocErr), Infallible) => handle_alloc_error(new_layout),
677681
_ => {}
678682
}
679683

src/liballoc/rc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ use core::ops::CoerceUnsized;
259259
use core::ptr::{self, NonNull};
260260
use core::convert::From;
261261

262-
use alloc::{Global, Alloc, Layout, box_free, oom};
262+
use alloc::{Global, Alloc, Layout, box_free, handle_alloc_error};
263263
use string::String;
264264
use vec::Vec;
265265

@@ -662,7 +662,7 @@ impl<T: ?Sized> Rc<T> {
662662
let layout = Layout::for_value(&*fake_ptr);
663663

664664
let mem = Global.alloc(layout)
665-
.unwrap_or_else(|_| oom(layout));
665+
.unwrap_or_else(|_| handle_alloc_error(layout));
666666

667667
// Initialize the real RcBox
668668
let inner = set_data_ptr(ptr as *mut T, mem.as_ptr() as *mut u8) as *mut RcBox<T>;

src/libcore/alloc.rs

+24-24
Original file line numberDiff line numberDiff line change
@@ -492,10 +492,10 @@ pub unsafe trait GlobalAlloc {
492492
/// library that aborts on memory exhaustion.)
493493
///
494494
/// Clients wishing to abort computation in response to an
495-
/// allocation error are encouraged to call the [`oom`] function,
495+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
496496
/// rather than directly invoking `panic!` or similar.
497497
///
498-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
498+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
499499
#[stable(feature = "global_alloc", since = "1.28.0")]
500500
unsafe fn alloc(&self, layout: Layout) -> *mut u8;
501501

@@ -529,10 +529,10 @@ pub unsafe trait GlobalAlloc {
529529
/// just as in `alloc`.
530530
///
531531
/// Clients wishing to abort computation in response to an
532-
/// allocation error are encouraged to call the [`oom`] function,
532+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
533533
/// rather than directly invoking `panic!` or similar.
534534
///
535-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
535+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
536536
#[stable(feature = "global_alloc", since = "1.28.0")]
537537
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
538538
let size = layout.size();
@@ -589,10 +589,10 @@ pub unsafe trait GlobalAlloc {
589589
/// library that aborts on memory exhaustion.)
590590
///
591591
/// Clients wishing to abort computation in response to a
592-
/// reallocation error are encouraged to call the [`oom`] function,
592+
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
593593
/// rather than directly invoking `panic!` or similar.
594594
///
595-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
595+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
596596
#[stable(feature = "global_alloc", since = "1.28.0")]
597597
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
598598
let new_layout = Layout::from_size_align_unchecked(new_size, layout.align());
@@ -733,10 +733,10 @@ pub unsafe trait Alloc {
733733
/// library that aborts on memory exhaustion.)
734734
///
735735
/// Clients wishing to abort computation in response to an
736-
/// allocation error are encouraged to call the [`oom`] function,
736+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
737737
/// rather than directly invoking `panic!` or similar.
738738
///
739-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
739+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
740740
unsafe fn alloc(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr>;
741741

742742
/// Deallocate the memory referenced by `ptr`.
@@ -843,10 +843,10 @@ pub unsafe trait Alloc {
843843
/// library that aborts on memory exhaustion.)
844844
///
845845
/// Clients wishing to abort computation in response to a
846-
/// reallocation error are encouraged to call the [`oom`] function,
846+
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
847847
/// rather than directly invoking `panic!` or similar.
848848
///
849-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
849+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
850850
unsafe fn realloc(&mut self,
851851
ptr: NonNull<u8>,
852852
layout: Layout,
@@ -889,10 +889,10 @@ pub unsafe trait Alloc {
889889
/// constraints, just as in `alloc`.
890890
///
891891
/// Clients wishing to abort computation in response to an
892-
/// allocation error are encouraged to call the [`oom`] function,
892+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
893893
/// rather than directly invoking `panic!` or similar.
894894
///
895-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
895+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
896896
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<u8>, AllocErr> {
897897
let size = layout.size();
898898
let p = self.alloc(layout);
@@ -917,10 +917,10 @@ pub unsafe trait Alloc {
917917
/// constraints, just as in `alloc`.
918918
///
919919
/// Clients wishing to abort computation in response to an
920-
/// allocation error are encouraged to call the [`oom`] function,
920+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
921921
/// rather than directly invoking `panic!` or similar.
922922
///
923-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
923+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
924924
unsafe fn alloc_excess(&mut self, layout: Layout) -> Result<Excess, AllocErr> {
925925
let usable_size = self.usable_size(&layout);
926926
self.alloc(layout).map(|p| Excess(p, usable_size.1))
@@ -941,10 +941,10 @@ pub unsafe trait Alloc {
941941
/// constraints, just as in `realloc`.
942942
///
943943
/// Clients wishing to abort computation in response to a
944-
/// reallocation error are encouraged to call the [`oom`] function,
944+
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
945945
/// rather than directly invoking `panic!` or similar.
946946
///
947-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
947+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
948948
unsafe fn realloc_excess(&mut self,
949949
ptr: NonNull<u8>,
950950
layout: Layout,
@@ -986,7 +986,7 @@ pub unsafe trait Alloc {
986986
/// unable to assert that the memory block referenced by `ptr`
987987
/// could fit `layout`.
988988
///
989-
/// Note that one cannot pass `CannotReallocInPlace` to the `oom`
989+
/// Note that one cannot pass `CannotReallocInPlace` to the `handle_alloc_error`
990990
/// function; clients are expected either to be able to recover from
991991
/// `grow_in_place` failures without aborting, or to fall back on
992992
/// another reallocation method before resorting to an abort.
@@ -1041,7 +1041,7 @@ pub unsafe trait Alloc {
10411041
/// unable to assert that the memory block referenced by `ptr`
10421042
/// could fit `layout`.
10431043
///
1044-
/// Note that one cannot pass `CannotReallocInPlace` to the `oom`
1044+
/// Note that one cannot pass `CannotReallocInPlace` to the `handle_alloc_error`
10451045
/// function; clients are expected either to be able to recover from
10461046
/// `shrink_in_place` failures without aborting, or to fall back
10471047
/// on another reallocation method before resorting to an abort.
@@ -1090,10 +1090,10 @@ pub unsafe trait Alloc {
10901090
/// will *not* yield undefined behavior.
10911091
///
10921092
/// Clients wishing to abort computation in response to an
1093-
/// allocation error are encouraged to call the [`oom`] function,
1093+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
10941094
/// rather than directly invoking `panic!` or similar.
10951095
///
1096-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
1096+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
10971097
fn alloc_one<T>(&mut self) -> Result<NonNull<T>, AllocErr>
10981098
where Self: Sized
10991099
{
@@ -1159,10 +1159,10 @@ pub unsafe trait Alloc {
11591159
/// Always returns `Err` on arithmetic overflow.
11601160
///
11611161
/// Clients wishing to abort computation in response to an
1162-
/// allocation error are encouraged to call the [`oom`] function,
1162+
/// allocation error are encouraged to call the [`handle_alloc_error`] function,
11631163
/// rather than directly invoking `panic!` or similar.
11641164
///
1165-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
1165+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
11661166
fn alloc_array<T>(&mut self, n: usize) -> Result<NonNull<T>, AllocErr>
11671167
where Self: Sized
11681168
{
@@ -1206,10 +1206,10 @@ pub unsafe trait Alloc {
12061206
/// Always returns `Err` on arithmetic overflow.
12071207
///
12081208
/// Clients wishing to abort computation in response to a
1209-
/// reallocation error are encouraged to call the [`oom`] function,
1209+
/// reallocation error are encouraged to call the [`handle_alloc_error`] function,
12101210
/// rather than directly invoking `panic!` or similar.
12111211
///
1212-
/// [`oom`]: ../../alloc/alloc/fn.oom.html
1212+
/// [`handle_alloc_error`]: ../../alloc/alloc/fn.handle_alloc_error.html
12131213
unsafe fn realloc_array<T>(&mut self,
12141214
ptr: NonNull<T>,
12151215
n_old: usize,

src/libstd/alloc.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -88,38 +88,38 @@ pub use alloc_system::System;
8888

8989
static HOOK: AtomicPtr<()> = AtomicPtr::new(ptr::null_mut());
9090

91-
/// Registers a custom OOM hook, replacing any that was previously registered.
91+
/// Registers a custom allocation error hook, replacing any that was previously registered.
9292
///
93-
/// The OOM hook is invoked when an infallible memory allocation fails, before
93+
/// The allocation error hook is invoked when an infallible memory allocation fails, before
9494
/// the runtime aborts. The default hook prints a message to standard error,
95-
/// but this behavior can be customized with the [`set_oom_hook`] and
96-
/// [`take_oom_hook`] functions.
95+
/// but this behavior can be customized with the [`set_alloc_error_hook`] and
96+
/// [`take_alloc_error_hook`] functions.
9797
///
9898
/// The hook is provided with a `Layout` struct which contains information
9999
/// about the allocation that failed.
100100
///
101-
/// The OOM hook is a global resource.
102-
#[unstable(feature = "oom_hook", issue = "51245")]
103-
pub fn set_oom_hook(hook: fn(Layout)) {
101+
/// The allocation error hook is a global resource.
102+
#[unstable(feature = "alloc_error_hook", issue = "51245")]
103+
pub fn set_alloc_error_hook(hook: fn(Layout)) {
104104
HOOK.store(hook as *mut (), Ordering::SeqCst);
105105
}
106106

107-
/// Unregisters the current OOM hook, returning it.
107+
/// Unregisters the current allocation error hook, returning it.
108108
///
109-
/// *See also the function [`set_oom_hook`].*
109+
/// *See also the function [`set_alloc_error_hook`].*
110110
///
111111
/// If no custom hook is registered, the default hook will be returned.
112-
#[unstable(feature = "oom_hook", issue = "51245")]
113-
pub fn take_oom_hook() -> fn(Layout) {
112+
#[unstable(feature = "alloc_error_hook", issue = "51245")]
113+
pub fn take_alloc_error_hook() -> fn(Layout) {
114114
let hook = HOOK.swap(ptr::null_mut(), Ordering::SeqCst);
115115
if hook.is_null() {
116-
default_oom_hook
116+
default_alloc_error_hook
117117
} else {
118118
unsafe { mem::transmute(hook) }
119119
}
120120
}
121121

122-
fn default_oom_hook(layout: Layout) {
122+
fn default_alloc_error_hook(layout: Layout) {
123123
dumb_print(format_args!("memory allocation of {} bytes failed", layout.size()));
124124
}
125125

@@ -130,7 +130,7 @@ fn default_oom_hook(layout: Layout) {
130130
pub extern fn rust_oom(layout: Layout) -> ! {
131131
let hook = HOOK.load(Ordering::SeqCst);
132132
let hook: fn(Layout) = if hook.is_null() {
133-
default_oom_hook
133+
default_alloc_error_hook
134134
} else {
135135
unsafe { mem::transmute(hook) }
136136
};

src/libstd/collections/hash/table.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use alloc::{Global, Alloc, Layout, LayoutErr, CollectionAllocErr, oom};
11+
use alloc::{Global, Alloc, Layout, LayoutErr, CollectionAllocErr, handle_alloc_error};
1212
use hash::{BuildHasher, Hash, Hasher};
1313
use marker;
1414
use mem::{size_of, needs_drop};
@@ -699,7 +699,7 @@ impl<K, V> RawTable<K, V> {
699699
// point into it.
700700
let (layout, _) = calculate_layout::<K, V>(capacity)?;
701701
let buffer = Global.alloc(layout).map_err(|e| match fallibility {
702-
Infallible => oom(layout),
702+
Infallible => handle_alloc_error(layout),
703703
Fallible => e,
704704
})?;
705705

src/test/run-pass/allocator-alloc-one.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010

1111
#![feature(allocator_api, nonnull)]
1212

13-
use std::alloc::{Alloc, Global, Layout, oom};
13+
use std::alloc::{Alloc, Global, Layout, handle_alloc_error};
1414

1515
fn main() {
1616
unsafe {
17-
let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| oom(Layout::new::<i32>()));
17+
let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| {
18+
handle_alloc_error(Layout::new::<i32>())
19+
});
1820
*ptr.as_ptr() = 4;
1921
assert_eq!(*ptr.as_ptr(), 4);
2022
Global.dealloc_one(ptr);

0 commit comments

Comments
 (0)