提交 051abae8 编写于 作者: A Alex Crichton

alloc: Refactor OOM into a common routine

上级 4cd932f9
...@@ -130,7 +130,6 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint, ...@@ -130,7 +130,6 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
#[cfg(jemalloc)] #[cfg(jemalloc)]
mod imp { mod imp {
use core::intrinsics::abort;
use core::option::{None, Option}; use core::option::{None, Option};
use core::ptr::{RawPtr, mut_null, null}; use core::ptr::{RawPtr, mut_null, null};
use core::num::Bitwise; use core::num::Bitwise;
...@@ -163,7 +162,7 @@ fn mallocx_align(a: uint) -> c_int { a.trailing_zeros() as c_int } ...@@ -163,7 +162,7 @@ fn mallocx_align(a: uint) -> c_int { a.trailing_zeros() as c_int }
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 { pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8; let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8;
if ptr.is_null() { if ptr.is_null() {
abort() ::oom()
} }
ptr ptr
} }
...@@ -174,7 +173,7 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint, ...@@ -174,7 +173,7 @@ pub unsafe fn reallocate(ptr: *mut u8, size: uint, align: uint,
let ptr = je_rallocx(ptr as *mut c_void, size as size_t, let ptr = je_rallocx(ptr as *mut c_void, size as size_t,
mallocx_align(align)) as *mut u8; mallocx_align(align)) as *mut u8;
if ptr.is_null() { if ptr.is_null() {
abort() ::oom()
} }
ptr ptr
} }
......
...@@ -94,6 +94,14 @@ ...@@ -94,6 +94,14 @@
pub mod arc; pub mod arc;
pub mod rc; pub mod rc;
/// Common OOM routine used by liballoc
fn oom() -> ! {
// FIXME(#14674): This really needs to do something other than just abort
// here, but any printing done must be *guaranteed* to not
// allocate.
unsafe { core::intrinsics::abort() }
}
// FIXME(#14344): When linking liballoc with libstd, this library will be linked // FIXME(#14344): When linking liballoc with libstd, this library will be linked
// as an rlib (it only exists as an rlib). It turns out that an // as an rlib (it only exists as an rlib). It turns out that an
// optimized standard library doesn't actually use *any* symbols // optimized standard library doesn't actually use *any* symbols
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
use libc::{c_void, size_t, free, malloc, realloc}; use libc::{c_void, size_t, free, malloc, realloc};
use core::ptr::{RawPtr, mut_null}; use core::ptr::{RawPtr, mut_null};
use core::intrinsics::abort;
/// A wrapper around libc::malloc, aborting on out-of-memory /// A wrapper around libc::malloc, aborting on out-of-memory
#[inline] #[inline]
...@@ -25,8 +24,7 @@ pub unsafe fn malloc_raw(size: uint) -> *mut u8 { ...@@ -25,8 +24,7 @@ pub unsafe fn malloc_raw(size: uint) -> *mut u8 {
} else { } else {
let p = malloc(size as size_t); let p = malloc(size as size_t);
if p.is_null() { if p.is_null() {
// we need a non-allocating way to print an error here ::oom();
abort();
} }
p as *mut u8 p as *mut u8
} }
...@@ -43,8 +41,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 { ...@@ -43,8 +41,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
} else { } else {
let p = realloc(ptr as *mut c_void, size as size_t); let p = realloc(ptr as *mut c_void, size as size_t);
if p.is_null() { if p.is_null() {
// we need a non-allocating way to print an error here ::oom();
abort();
} }
p as *mut u8 p as *mut u8
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册