Skip to content

Commit 051abae

Browse files
committed
alloc: Refactor OOM into a common routine
1 parent 4cd932f commit 051abae

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

src/liballoc/heap.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint,
130130

131131
#[cfg(jemalloc)]
132132
mod imp {
133-
use core::intrinsics::abort;
134133
use core::option::{None, Option};
135134
use core::ptr::{RawPtr, mut_null, null};
136135
use core::num::Bitwise;
@@ -163,7 +162,7 @@ mod imp {
163162
pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 {
164163
let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8;
165164
if ptr.is_null() {
166-
abort()
165+
::oom()
167166
}
168167
ptr
169168
}
@@ -174,7 +173,7 @@ mod imp {
174173
let ptr = je_rallocx(ptr as *mut c_void, size as size_t,
175174
mallocx_align(align)) as *mut u8;
176175
if ptr.is_null() {
177-
abort()
176+
::oom()
178177
}
179178
ptr
180179
}

src/liballoc/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,14 @@ pub mod owned;
9494
pub mod arc;
9595
pub mod rc;
9696

97+
/// Common OOM routine used by liballoc
98+
fn oom() -> ! {
99+
// FIXME(#14674): This really needs to do something other than just abort
100+
// here, but any printing done must be *guaranteed* to not
101+
// allocate.
102+
unsafe { core::intrinsics::abort() }
103+
}
104+
97105
// FIXME(#14344): When linking liballoc with libstd, this library will be linked
98106
// as an rlib (it only exists as an rlib). It turns out that an
99107
// optimized standard library doesn't actually use *any* symbols

src/liballoc/libc_heap.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
1414
use libc::{c_void, size_t, free, malloc, realloc};
1515
use core::ptr::{RawPtr, mut_null};
16-
use core::intrinsics::abort;
1716

1817
/// A wrapper around libc::malloc, aborting on out-of-memory
1918
#[inline]
@@ -25,8 +24,7 @@ pub unsafe fn malloc_raw(size: uint) -> *mut u8 {
2524
} else {
2625
let p = malloc(size as size_t);
2726
if p.is_null() {
28-
// we need a non-allocating way to print an error here
29-
abort();
27+
::oom();
3028
}
3129
p as *mut u8
3230
}
@@ -43,8 +41,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 {
4341
} else {
4442
let p = realloc(ptr as *mut c_void, size as size_t);
4543
if p.is_null() {
46-
// we need a non-allocating way to print an error here
47-
abort();
44+
::oom();
4845
}
4946
p as *mut u8
5047
}

0 commit comments

Comments
 (0)