alloc: Refactor OOM into a common routine
This commit is contained in:
parent
4cd932f94e
commit
051abae802
@ -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 @@ mod imp {
|
|||||||
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 @@ mod imp {
|
|||||||
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 @@ pub mod owned;
|
|||||||
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 @@
|
|||||||
|
|
||||||
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 {
|
|||||||
} 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 {
|
|||||||
} 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
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user