Skip to content

Commit

Permalink
Remove Alloc::oom
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Apr 22, 2018
1 parent e513c1b commit 9e8f683
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 115 deletions.
1 change: 0 additions & 1 deletion src/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 0 additions & 5 deletions src/liballoc/alloc.rs
Expand Up @@ -131,11 +131,6 @@ unsafe impl Alloc for Global {
unsafe fn alloc_zeroed(&mut self, layout: Layout) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::alloc_zeroed(self, layout)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
oom()
}
}

/// The allocator for unique pointers.
Expand Down
2 changes: 1 addition & 1 deletion src/liballoc/heap.rs
Expand Up @@ -59,7 +59,7 @@ unsafe impl<T> Alloc for T where T: CoreAlloc {
}

fn oom(&mut self, _: AllocErr) -> ! {
CoreAlloc::oom(self)
unsafe { ::core::intrinsics::abort() }
}

fn usable_size(&self, layout: &Layout) -> (usize, usize) {
Expand Down
14 changes: 7 additions & 7 deletions src/liballoc/raw_vec.rs
Expand Up @@ -14,7 +14,7 @@ use core::ops::Drop;
use core::ptr::{self, NonNull, Unique};
use core::slice;

use alloc::{Alloc, Layout, Global};
use alloc::{Alloc, Layout, Global, oom};
use alloc::CollectionAllocErr;
use alloc::CollectionAllocErr::*;
use boxed::Box;
Expand Down Expand Up @@ -101,7 +101,7 @@ impl<T, A: Alloc> RawVec<T, A> {
};
match result {
Ok(ptr) => ptr,
Err(_) => a.oom(),
Err(_) => oom(),
}
};

Expand Down Expand Up @@ -316,7 +316,7 @@ impl<T, A: Alloc> RawVec<T, A> {
new_size);
match ptr_res {
Ok(ptr) => (new_cap, ptr.cast().into()),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
None => {
Expand All @@ -325,7 +325,7 @@ impl<T, A: Alloc> RawVec<T, A> {
let new_cap = if elem_size > (!0) / 8 { 1 } else { 4 };
match self.a.alloc_array::<T>(new_cap) {
Ok(ptr) => (new_cap, ptr.into()),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
};
Expand Down Expand Up @@ -442,7 +442,7 @@ impl<T, A: Alloc> RawVec<T, A> {
pub fn reserve_exact(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve_exact(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
Err(AllocErr) => self.a.oom(),
Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
Expand Down Expand Up @@ -552,7 +552,7 @@ impl<T, A: Alloc> RawVec<T, A> {
pub fn reserve(&mut self, used_cap: usize, needed_extra_cap: usize) {
match self.try_reserve(used_cap, needed_extra_cap) {
Err(CapacityOverflow) => capacity_overflow(),
Err(AllocErr) => self.a.oom(),
Err(AllocErr) => oom(),
Ok(()) => { /* yay */ }
}
}
Expand Down Expand Up @@ -667,7 +667,7 @@ impl<T, A: Alloc> RawVec<T, A> {
old_layout,
new_size) {
Ok(p) => self.ptr = p.cast().into(),
Err(_) => self.a.oom(),
Err(_) => oom(),
}
}
self.cap = amount;
Expand Down
1 change: 0 additions & 1 deletion src/liballoc_jemalloc/Cargo.toml
Expand Up @@ -12,7 +12,6 @@ test = false
doc = false

[dependencies]
alloc_system = { path = "../liballoc_system" }
core = { path = "../libcore" }
libc = { path = "../rustc/libc_shim" }
compiler_builtins = { path = "../rustc/compiler_builtins_shim" }
Expand Down
5 changes: 2 additions & 3 deletions src/liballoc_jemalloc/lib.rs
Expand Up @@ -14,7 +14,7 @@
reason = "this library is unlikely to be stabilized in its current \
form or name",
issue = "27783")]
#![feature(alloc_system)]
#![feature(core_intrinsics)]
#![feature(libc)]
#![feature(linkage)]
#![feature(staged_api)]
Expand All @@ -23,7 +23,6 @@
#![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
#![rustc_alloc_kind = "exe"]

extern crate alloc_system;
extern crate libc;

#[cfg(not(dummy_jemalloc))]
Expand Down Expand Up @@ -102,7 +101,7 @@ mod contents {
#[no_mangle]
#[rustc_std_internal_symbol]
pub unsafe extern fn __rde_oom() -> ! {
::alloc_system::oom()
::core::intrinsics::abort();
}

#[no_mangle]
Expand Down
70 changes: 0 additions & 70 deletions src/liballoc_system/lib.rs
Expand Up @@ -71,11 +71,6 @@ unsafe impl Alloc for System {
new_size: usize) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
::oom()
}
}

#[cfg(stage0)]
Expand Down Expand Up @@ -103,11 +98,6 @@ unsafe impl<'a> Alloc for &'a System {
new_size: usize) -> Result<NonNull<Opaque>, AllocErr> {
NonNull::new(GlobalAlloc::realloc(*self, ptr.as_ptr(), layout, new_size)).ok_or(AllocErr)
}

#[inline]
fn oom(&mut self) -> ! {
::oom()
}
}

#[cfg(any(windows, unix, target_os = "cloudabi", target_os = "redox"))]
Expand Down Expand Up @@ -366,63 +356,3 @@ mod platform {
}
}
}

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

#[cfg(any(unix, target_os = "redox"))]
#[inline]
fn write_to_stderr(s: &str) {
extern crate libc;

unsafe {
libc::write(libc::STDERR_FILENO,
s.as_ptr() as *const libc::c_void,
s.len());
}
}

#[cfg(windows)]
#[inline]
fn write_to_stderr(s: &str) {
use core::ptr;

type LPVOID = *mut u8;
type HANDLE = LPVOID;
type DWORD = u32;
type BOOL = i32;
type LPDWORD = *mut DWORD;
type LPOVERLAPPED = *mut u8;

const STD_ERROR_HANDLE: DWORD = -12i32 as DWORD;

extern "system" {
fn WriteFile(hFile: HANDLE,
lpBuffer: LPVOID,
nNumberOfBytesToWrite: DWORD,
lpNumberOfBytesWritten: LPDWORD,
lpOverlapped: LPOVERLAPPED)
-> BOOL;
fn GetStdHandle(which: DWORD) -> HANDLE;
}

unsafe {
// WriteFile silently fails if it is passed an invalid
// handle, so there is no need to check the result of
// GetStdHandle.
WriteFile(GetStdHandle(STD_ERROR_HANDLE),
s.as_ptr() as LPVOID,
s.len() as DWORD,
ptr::null_mut(),
ptr::null_mut());
}
}

#[cfg(not(any(windows, unix, target_os = "redox")))]
#[inline]
fn write_to_stderr(_: &str) {}
26 changes: 0 additions & 26 deletions src/libcore/alloc.rs
Expand Up @@ -603,32 +603,6 @@ pub unsafe trait Alloc {
/// to allocate that block of memory.
unsafe fn dealloc(&mut self, ptr: NonNull<Opaque>, layout: Layout);

/// Allocator-specific method for signaling an out-of-memory
/// condition.
///
/// `oom` 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.
///
/// Implementations of the `oom` method are discouraged from
/// infinitely regressing in nested calls to `oom`. In
/// practice this means implementors should eschew allocating,
/// especially from `self` (directly or indirectly).
///
/// Implementations of the allocation and reallocation methods
/// (e.g. `alloc`, `alloc_one`, `realloc`) are discouraged from
/// panicking (or aborting) in the event of memory exhaustion;
/// instead they should return an appropriate error from the
/// invoked method, and let the client decide whether to invoke
/// this `oom` method in response.
fn oom(&mut self) -> ! {
unsafe { ::intrinsics::abort() }
}

// == ALLOCATOR-SPECIFIC QUANTITIES AND LIMITS ==
// usable_size

Expand Down
1 change: 0 additions & 1 deletion src/test/compile-fail/allocator/not-an-allocator.rs
Expand Up @@ -16,6 +16,5 @@ static A: usize = 0;
//~| the trait bound `usize:
//~| the trait bound `usize:
//~| the trait bound `usize:
//~| the trait bound `usize:

fn main() {}

0 comments on commit 9e8f683

Please sign in to comment.