Skip to content

Commit e513c1b

Browse files
committed
Replace GlobalAlloc::oom with a lang item
1 parent 8887396 commit e513c1b

File tree

16 files changed

+53
-59
lines changed

16 files changed

+53
-59
lines changed

src/liballoc/alloc.rs

+18-17
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@ extern "Rust" {
4848
#[allocator]
4949
#[rustc_allocator_nounwind]
5050
fn __rust_alloc(size: usize, align: usize) -> *mut u8;
51-
#[cold]
52-
#[rustc_allocator_nounwind]
53-
fn __rust_oom() -> !;
5451
#[rustc_allocator_nounwind]
5552
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
5653
#[rustc_allocator_nounwind]
@@ -107,16 +104,6 @@ unsafe impl GlobalAlloc for Global {
107104
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
108105
ptr as *mut Opaque
109106
}
110-
111-
#[inline]
112-
fn oom(&self) -> ! {
113-
unsafe {
114-
#[cfg(not(stage0))]
115-
__rust_oom();
116-
#[cfg(stage0)]
117-
__rust_oom(&mut 0);
118-
}
119-
}
120107
}
121108

122109
unsafe impl Alloc for Global {
@@ -147,7 +134,7 @@ unsafe impl Alloc for Global {
147134

148135
#[inline]
149136
fn oom(&mut self) -> ! {
150-
GlobalAlloc::oom(self)
137+
oom()
151138
}
152139
}
153140

@@ -165,7 +152,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
165152
if !ptr.is_null() {
166153
ptr as *mut u8
167154
} else {
168-
Global.oom()
155+
oom()
169156
}
170157
}
171158
}
@@ -182,19 +169,33 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
182169
}
183170
}
184171

172+
#[cfg(stage0)]
173+
pub fn oom() -> ! {
174+
unsafe { ::core::intrinsics::abort() }
175+
}
176+
177+
#[cfg(not(stage0))]
178+
pub fn oom() -> ! {
179+
extern {
180+
#[lang = "oom"]
181+
fn oom_impl() -> !;
182+
}
183+
unsafe { oom_impl() }
184+
}
185+
185186
#[cfg(test)]
186187
mod tests {
187188
extern crate test;
188189
use self::test::Bencher;
189190
use boxed::Box;
190-
use alloc::{Global, Alloc, Layout};
191+
use alloc::{Global, Alloc, Layout, oom};
191192

192193
#[test]
193194
fn allocate_zeroed() {
194195
unsafe {
195196
let layout = Layout::from_size_align(1024, 1).unwrap();
196197
let ptr = Global.alloc_zeroed(layout.clone())
197-
.unwrap_or_else(|_| Global.oom());
198+
.unwrap_or_else(|_| oom());
198199

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

src/liballoc/arc.rs

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

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

555555
let mem = Global.alloc(layout)
556-
.unwrap_or_else(|_| Global.oom());
556+
.unwrap_or_else(|_| oom());
557557

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

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, Opaque, box_free};
262+
use alloc::{Global, Alloc, Layout, Opaque, box_free, oom};
263263
use string::String;
264264
use vec::Vec;
265265

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

670670
let mem = Global.alloc(layout)
671-
.unwrap_or_else(|_| Global.oom());
671+
.unwrap_or_else(|_| oom());
672672

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

src/liballoc_jemalloc/lib.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ extern crate libc;
3030
pub use contents::*;
3131
#[cfg(not(dummy_jemalloc))]
3232
mod contents {
33-
use core::alloc::GlobalAlloc;
34-
use alloc_system::System;
3533
use libc::{c_int, c_void, size_t};
3634

3735
// Note that the symbols here are prefixed by default on macOS and Windows (we
@@ -100,10 +98,11 @@ mod contents {
10098
ptr
10199
}
102100

101+
#[cfg(stage0)]
103102
#[no_mangle]
104103
#[rustc_std_internal_symbol]
105104
pub unsafe extern fn __rde_oom() -> ! {
106-
System.oom()
105+
::alloc_system::oom()
107106
}
108107

109108
#[no_mangle]

src/liballoc_system/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ mod platform {
368368
}
369369

370370
#[inline]
371-
fn oom() -> ! {
371+
pub fn oom() -> ! {
372372
write_to_stderr("fatal runtime error: memory allocation failed");
373373
unsafe {
374374
::core::intrinsics::abort();

src/libcore/alloc.rs

-11
Original file line numberDiff line numberDiff line change
@@ -451,17 +451,6 @@ pub unsafe trait GlobalAlloc {
451451
}
452452
new_ptr
453453
}
454-
455-
/// Aborts the thread or process, optionally performing
456-
/// cleanup or logging diagnostic information before panicking or
457-
/// aborting.
458-
///
459-
/// `oom` is meant to be used by clients unable to cope with an
460-
/// unsatisfied allocation request, and wish to abandon
461-
/// computation rather than attempt to recover locally.
462-
fn oom(&self) -> ! {
463-
unsafe { ::intrinsics::abort() }
464-
}
465454
}
466455

467456
/// An implementation of `Alloc` can allocate, reallocate, and

src/librustc/middle/lang_items.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ language_item_table! {
303303

304304
ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
305305
BoxFreeFnLangItem, "box_free", box_free_fn;
306-
DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
306+
DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
307+
OomLangItem, "oom", oom;
307308

308309
StartFnLangItem, "start", start_fn;
309310

src/librustc/middle/weak_lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,5 @@ weak_lang_items! {
151151
panic_fmt, PanicFmtLangItem, rust_begin_unwind;
152152
eh_personality, EhPersonalityLangItem, rust_eh_personality;
153153
eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume;
154+
oom, OomLangItem, rust_oom;
154155
}

src/librustc_allocator/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,6 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
2323
inputs: &[AllocatorTy::Layout],
2424
output: AllocatorTy::ResultPtr,
2525
},
26-
AllocatorMethod {
27-
name: "oom",
28-
inputs: &[],
29-
output: AllocatorTy::Bang,
30-
},
3126
AllocatorMethod {
3227
name: "dealloc",
3328
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],

src/libstd/alloc.rs

+11-2
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
#![unstable(issue = "32838", feature = "allocator_api")]
1414

1515
#[doc(inline)] #[allow(deprecated)] pub use alloc_crate::alloc::Heap;
16-
#[doc(inline)] pub use alloc_crate::alloc::Global;
16+
#[doc(inline)] pub use alloc_crate::alloc::{Global, oom};
1717
#[doc(inline)] pub use alloc_system::System;
1818
#[doc(inline)] pub use core::alloc::*;
1919

20+
#[cfg(not(stage0))]
21+
#[cfg(not(test))]
22+
#[doc(hidden)]
23+
#[lang = "oom"]
24+
pub extern fn rust_oom() -> ! {
25+
rtabort!("memory allocation failed");
26+
}
27+
2028
#[cfg(not(test))]
2129
#[doc(hidden)]
2230
#[allow(unused_attributes)]
@@ -35,10 +43,11 @@ pub mod __default_lib_allocator {
3543
System.alloc(layout) as *mut u8
3644
}
3745

46+
#[cfg(stage0)]
3847
#[no_mangle]
3948
#[rustc_std_internal_symbol]
4049
pub unsafe extern fn __rdl_oom() -> ! {
41-
System.oom()
50+
super::oom()
4251
}
4352

4453
#[no_mangle]

src/libstd/collections/hash/map.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
use self::Entry::*;
1212
use self::VacantEntryState::*;
1313

14-
use alloc::{Global, Alloc, CollectionAllocErr};
14+
use alloc::{CollectionAllocErr, oom};
1515
use cell::Cell;
1616
use borrow::Borrow;
1717
use cmp::max;
@@ -784,7 +784,7 @@ impl<K, V, S> HashMap<K, V, S>
784784
pub fn reserve(&mut self, additional: usize) {
785785
match self.try_reserve(additional) {
786786
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
787-
Err(CollectionAllocErr::AllocErr) => Global.oom(),
787+
Err(CollectionAllocErr::AllocErr) => oom(),
788788
Ok(()) => { /* yay */ }
789789
}
790790
}

src/libstd/collections/hash/table.rs

+3-3
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, CollectionAllocErr};
11+
use alloc::{Global, Alloc, Layout, CollectionAllocErr, oom};
1212
use cmp;
1313
use hash::{BuildHasher, Hash, Hasher};
1414
use marker;
@@ -770,7 +770,7 @@ impl<K, V> RawTable<K, V> {
770770
unsafe fn new_uninitialized(capacity: usize) -> RawTable<K, V> {
771771
match Self::try_new_uninitialized(capacity) {
772772
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
773-
Err(CollectionAllocErr::AllocErr) => Global.oom(),
773+
Err(CollectionAllocErr::AllocErr) => oom(),
774774
Ok(table) => { table }
775775
}
776776
}
@@ -809,7 +809,7 @@ impl<K, V> RawTable<K, V> {
809809
pub fn new(capacity: usize) -> RawTable<K, V> {
810810
match Self::try_new(capacity) {
811811
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
812-
Err(CollectionAllocErr::AllocErr) => Global.oom(),
812+
Err(CollectionAllocErr::AllocErr) => oom(),
813813
Ok(table) => { table }
814814
}
815815
}

src/libstd/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,6 @@ pub mod path;
482482
pub mod process;
483483
pub mod sync;
484484
pub mod time;
485-
pub mod alloc;
486485

487486
#[unstable(feature = "allocator_api", issue = "32838")]
488487
#[rustc_deprecated(since = "1.27.0", reason = "module renamed to `alloc`")]
@@ -496,6 +495,8 @@ pub mod heap {
496495
mod sys_common;
497496
mod sys;
498497

498+
pub mod alloc;
499+
499500
// Private support modules
500501
mod panicking;
501502
mod memchr;

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

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

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

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

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

src/test/run-pass/realloc-16687.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
#![feature(heap_api, allocator_api)]
1717

18-
use std::alloc::{Global, Alloc, Layout};
18+
use std::alloc::{Global, Alloc, Layout, oom};
1919
use std::ptr::{self, NonNull};
2020

2121
fn main() {
@@ -50,7 +50,7 @@ unsafe fn test_triangle() -> bool {
5050
println!("allocate({:?})", layout);
5151
}
5252

53-
let ret = Global.alloc(layout.clone()).unwrap_or_else(|_| Global.oom());
53+
let ret = Global.alloc(layout.clone()).unwrap_or_else(|_| oom());
5454

5555
if PRINT {
5656
println!("allocate({:?}) = {:?}", layout, ret);
@@ -73,7 +73,7 @@ unsafe fn test_triangle() -> bool {
7373
}
7474

7575
let ret = Global.realloc(NonNull::new_unchecked(ptr).as_opaque(), old.clone(), new.size())
76-
.unwrap_or_else(|_| Global.oom());
76+
.unwrap_or_else(|_| oom());
7777

7878
if PRINT {
7979
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",

src/test/run-pass/regions-mock-trans.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212

1313
#![feature(allocator_api)]
1414

15-
use std::alloc::{Alloc, Global, Layout};
15+
use std::alloc::{Alloc, Global, Layout, oom};
1616
use std::ptr::NonNull;
1717

1818
struct arena(());
@@ -33,7 +33,7 @@ struct Ccx {
3333
fn alloc<'a>(_bcx : &'a arena) -> &'a Bcx<'a> {
3434
unsafe {
3535
let ptr = Global.alloc(Layout::new::<Bcx>())
36-
.unwrap_or_else(|_| Global.oom());
36+
.unwrap_or_else(|_| oom());
3737
&*(ptr.as_ptr() as *const _)
3838
}
3939
}

0 commit comments

Comments
 (0)