1
Fork 0

Replace GlobalAlloc::oom with a lang item

This commit is contained in:
Steven Fackler 2018-04-20 21:05:13 -07:00
parent 8887396513
commit e513c1bd31
16 changed files with 53 additions and 59 deletions

View file

@ -48,9 +48,6 @@ extern "Rust" {
#[allocator] #[allocator]
#[rustc_allocator_nounwind] #[rustc_allocator_nounwind]
fn __rust_alloc(size: usize, align: usize) -> *mut u8; fn __rust_alloc(size: usize, align: usize) -> *mut u8;
#[cold]
#[rustc_allocator_nounwind]
fn __rust_oom() -> !;
#[rustc_allocator_nounwind] #[rustc_allocator_nounwind]
fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize); fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
#[rustc_allocator_nounwind] #[rustc_allocator_nounwind]
@ -107,16 +104,6 @@ unsafe impl GlobalAlloc for Global {
let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0); let ptr = __rust_alloc_zeroed(layout.size(), layout.align(), &mut 0);
ptr as *mut Opaque ptr as *mut Opaque
} }
#[inline]
fn oom(&self) -> ! {
unsafe {
#[cfg(not(stage0))]
__rust_oom();
#[cfg(stage0)]
__rust_oom(&mut 0);
}
}
} }
unsafe impl Alloc for Global { unsafe impl Alloc for Global {
@ -147,7 +134,7 @@ unsafe impl Alloc for Global {
#[inline] #[inline]
fn oom(&mut self) -> ! { fn oom(&mut self) -> ! {
GlobalAlloc::oom(self) oom()
} }
} }
@ -165,7 +152,7 @@ unsafe fn exchange_malloc(size: usize, align: usize) -> *mut u8 {
if !ptr.is_null() { if !ptr.is_null() {
ptr as *mut u8 ptr as *mut u8
} else { } else {
Global.oom() oom()
} }
} }
} }
@ -182,19 +169,33 @@ pub(crate) unsafe fn box_free<T: ?Sized>(ptr: *mut T) {
} }
} }
#[cfg(stage0)]
pub fn oom() -> ! {
unsafe { ::core::intrinsics::abort() }
}
#[cfg(not(stage0))]
pub fn oom() -> ! {
extern {
#[lang = "oom"]
fn oom_impl() -> !;
}
unsafe { oom_impl() }
}
#[cfg(test)] #[cfg(test)]
mod tests { 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}; use alloc::{Global, Alloc, Layout, oom};
#[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(|_| Global.oom()); .unwrap_or_else(|_| oom());
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);

View file

@ -31,7 +31,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}; use alloc::{Global, Alloc, Layout, box_free, oom};
use boxed::Box; use boxed::Box;
use string::String; use string::String;
use vec::Vec; use vec::Vec;
@ -553,7 +553,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(|_| Global.oom()); .unwrap_or_else(|_| oom());
// 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>;

View file

@ -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, Opaque, box_free}; use alloc::{Global, Alloc, Layout, Opaque, box_free, oom};
use string::String; use string::String;
use vec::Vec; use vec::Vec;
@ -668,7 +668,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(|_| Global.oom()); .unwrap_or_else(|_| oom());
// 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>;

View file

@ -30,8 +30,6 @@ extern crate libc;
pub use contents::*; pub use contents::*;
#[cfg(not(dummy_jemalloc))] #[cfg(not(dummy_jemalloc))]
mod contents { mod contents {
use core::alloc::GlobalAlloc;
use alloc_system::System;
use libc::{c_int, c_void, size_t}; use libc::{c_int, c_void, size_t};
// Note that the symbols here are prefixed by default on macOS and Windows (we // Note that the symbols here are prefixed by default on macOS and Windows (we
@ -100,10 +98,11 @@ mod contents {
ptr ptr
} }
#[cfg(stage0)]
#[no_mangle] #[no_mangle]
#[rustc_std_internal_symbol] #[rustc_std_internal_symbol]
pub unsafe extern fn __rde_oom() -> ! { pub unsafe extern fn __rde_oom() -> ! {
System.oom() ::alloc_system::oom()
} }
#[no_mangle] #[no_mangle]

View file

@ -368,7 +368,7 @@ mod platform {
} }
#[inline] #[inline]
fn oom() -> ! { pub fn oom() -> ! {
write_to_stderr("fatal runtime error: memory allocation failed"); write_to_stderr("fatal runtime error: memory allocation failed");
unsafe { unsafe {
::core::intrinsics::abort(); ::core::intrinsics::abort();

View file

@ -451,17 +451,6 @@ pub unsafe trait GlobalAlloc {
} }
new_ptr new_ptr
} }
/// Aborts the thread or process, optionally performing
/// cleanup or logging diagnostic information before panicking or
/// aborting.
///
/// `oom` is meant to be used by clients unable to cope with an
/// unsatisfied allocation request, and wish to abandon
/// computation rather than attempt to recover locally.
fn oom(&self) -> ! {
unsafe { ::intrinsics::abort() }
}
} }
/// An implementation of `Alloc` can allocate, reallocate, and /// An implementation of `Alloc` can allocate, reallocate, and

View file

@ -304,6 +304,7 @@ language_item_table! {
ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn; ExchangeMallocFnLangItem, "exchange_malloc", exchange_malloc_fn;
BoxFreeFnLangItem, "box_free", box_free_fn; BoxFreeFnLangItem, "box_free", box_free_fn;
DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn; DropInPlaceFnLangItem, "drop_in_place", drop_in_place_fn;
OomLangItem, "oom", oom;
StartFnLangItem, "start", start_fn; StartFnLangItem, "start", start_fn;

View file

@ -151,4 +151,5 @@ weak_lang_items! {
panic_fmt, PanicFmtLangItem, rust_begin_unwind; panic_fmt, PanicFmtLangItem, rust_begin_unwind;
eh_personality, EhPersonalityLangItem, rust_eh_personality; eh_personality, EhPersonalityLangItem, rust_eh_personality;
eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume; eh_unwind_resume, EhUnwindResumeLangItem, rust_eh_unwind_resume;
oom, OomLangItem, rust_oom;
} }

View file

@ -23,11 +23,6 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
inputs: &[AllocatorTy::Layout], inputs: &[AllocatorTy::Layout],
output: AllocatorTy::ResultPtr, output: AllocatorTy::ResultPtr,
}, },
AllocatorMethod {
name: "oom",
inputs: &[],
output: AllocatorTy::Bang,
},
AllocatorMethod { AllocatorMethod {
name: "dealloc", name: "dealloc",
inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout], inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],

View file

@ -13,10 +13,18 @@
#![unstable(issue = "32838", feature = "allocator_api")] #![unstable(issue = "32838", feature = "allocator_api")]
#[doc(inline)] #[allow(deprecated)] pub use alloc_crate::alloc::Heap; #[doc(inline)] #[allow(deprecated)] pub use alloc_crate::alloc::Heap;
#[doc(inline)] pub use alloc_crate::alloc::Global; #[doc(inline)] pub use alloc_crate::alloc::{Global, oom};
#[doc(inline)] pub use alloc_system::System; #[doc(inline)] pub use alloc_system::System;
#[doc(inline)] pub use core::alloc::*; #[doc(inline)] pub use core::alloc::*;
#[cfg(not(stage0))]
#[cfg(not(test))]
#[doc(hidden)]
#[lang = "oom"]
pub extern fn rust_oom() -> ! {
rtabort!("memory allocation failed");
}
#[cfg(not(test))] #[cfg(not(test))]
#[doc(hidden)] #[doc(hidden)]
#[allow(unused_attributes)] #[allow(unused_attributes)]
@ -35,10 +43,11 @@ pub mod __default_lib_allocator {
System.alloc(layout) as *mut u8 System.alloc(layout) as *mut u8
} }
#[cfg(stage0)]
#[no_mangle] #[no_mangle]
#[rustc_std_internal_symbol] #[rustc_std_internal_symbol]
pub unsafe extern fn __rdl_oom() -> ! { pub unsafe extern fn __rdl_oom() -> ! {
System.oom() super::oom()
} }
#[no_mangle] #[no_mangle]

View file

@ -11,7 +11,7 @@
use self::Entry::*; use self::Entry::*;
use self::VacantEntryState::*; use self::VacantEntryState::*;
use alloc::{Global, Alloc, CollectionAllocErr}; use alloc::{CollectionAllocErr, oom};
use cell::Cell; use cell::Cell;
use borrow::Borrow; use borrow::Borrow;
use cmp::max; use cmp::max;
@ -784,7 +784,7 @@ impl<K, V, S> HashMap<K, V, S>
pub fn reserve(&mut self, additional: usize) { pub fn reserve(&mut self, additional: usize) {
match self.try_reserve(additional) { match self.try_reserve(additional) {
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
Err(CollectionAllocErr::AllocErr) => Global.oom(), Err(CollectionAllocErr::AllocErr) => oom(),
Ok(()) => { /* yay */ } Ok(()) => { /* yay */ }
} }
} }

View file

@ -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, CollectionAllocErr}; use alloc::{Global, Alloc, Layout, CollectionAllocErr, oom};
use cmp; use cmp;
use hash::{BuildHasher, Hash, Hasher}; use hash::{BuildHasher, Hash, Hasher};
use marker; use marker;
@ -770,7 +770,7 @@ impl<K, V> RawTable<K, V> {
unsafe fn new_uninitialized(capacity: usize) -> RawTable<K, V> { unsafe fn new_uninitialized(capacity: usize) -> RawTable<K, V> {
match Self::try_new_uninitialized(capacity) { match Self::try_new_uninitialized(capacity) {
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
Err(CollectionAllocErr::AllocErr) => Global.oom(), Err(CollectionAllocErr::AllocErr) => oom(),
Ok(table) => { table } Ok(table) => { table }
} }
} }
@ -809,7 +809,7 @@ impl<K, V> RawTable<K, V> {
pub fn new(capacity: usize) -> RawTable<K, V> { pub fn new(capacity: usize) -> RawTable<K, V> {
match Self::try_new(capacity) { match Self::try_new(capacity) {
Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"), Err(CollectionAllocErr::CapacityOverflow) => panic!("capacity overflow"),
Err(CollectionAllocErr::AllocErr) => Global.oom(), Err(CollectionAllocErr::AllocErr) => oom(),
Ok(table) => { table } Ok(table) => { table }
} }
} }

View file

@ -482,7 +482,6 @@ pub mod path;
pub mod process; pub mod process;
pub mod sync; pub mod sync;
pub mod time; pub mod time;
pub mod alloc;
#[unstable(feature = "allocator_api", issue = "32838")] #[unstable(feature = "allocator_api", issue = "32838")]
#[rustc_deprecated(since = "1.27.0", reason = "module renamed to `alloc`")] #[rustc_deprecated(since = "1.27.0", reason = "module renamed to `alloc`")]
@ -496,6 +495,8 @@ pub mod heap {
mod sys_common; mod sys_common;
mod sys; mod sys;
pub mod alloc;
// Private support modules // Private support modules
mod panicking; mod panicking;
mod memchr; mod memchr;

View file

@ -10,13 +10,11 @@
#![feature(allocator_api, nonnull)] #![feature(allocator_api, nonnull)]
use std::alloc::{Alloc, Global}; use std::alloc::{Alloc, Global, oom};
fn main() { fn main() {
unsafe { unsafe {
let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| { let ptr = Global.alloc_one::<i32>().unwrap_or_else(|_| oom());
Global.oom()
});
*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);

View file

@ -15,7 +15,7 @@
#![feature(heap_api, allocator_api)] #![feature(heap_api, allocator_api)]
use std::alloc::{Global, Alloc, Layout}; use std::alloc::{Global, Alloc, Layout, oom};
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.clone()).unwrap_or_else(|_| Global.oom()); let ret = Global.alloc(layout.clone()).unwrap_or_else(|_| oom());
if PRINT { if PRINT {
println!("allocate({:?}) = {:?}", layout, ret); println!("allocate({:?}) = {:?}", layout, ret);
@ -73,7 +73,7 @@ unsafe fn test_triangle() -> bool {
} }
let ret = Global.realloc(NonNull::new_unchecked(ptr).as_opaque(), old.clone(), new.size()) let ret = Global.realloc(NonNull::new_unchecked(ptr).as_opaque(), old.clone(), new.size())
.unwrap_or_else(|_| Global.oom()); .unwrap_or_else(|_| oom());
if PRINT { if PRINT {
println!("reallocate({:?}, old={:?}, new={:?}) = {:?}", println!("reallocate({:?}, old={:?}, new={:?}) = {:?}",

View file

@ -12,7 +12,7 @@
#![feature(allocator_api)] #![feature(allocator_api)]
use std::alloc::{Alloc, Global, Layout}; use std::alloc::{Alloc, Global, Layout, oom};
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 ptr = Global.alloc(Layout::new::<Bcx>()) let ptr = Global.alloc(Layout::new::<Bcx>())
.unwrap_or_else(|_| Global.oom()); .unwrap_or_else(|_| oom());
&*(ptr.as_ptr() as *const _) &*(ptr.as_ptr() as *const _)
} }
} }