From 051abae802318d8401c9b5e6baa9ffc863f7f8eb Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Fri, 13 Jun 2014 23:35:54 -0700 Subject: [PATCH] alloc: Refactor OOM into a common routine --- src/liballoc/heap.rs | 5 ++--- src/liballoc/lib.rs | 8 ++++++++ src/liballoc/libc_heap.rs | 7 ++----- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/liballoc/heap.rs b/src/liballoc/heap.rs index 0e7445e737c..79a616b9555 100644 --- a/src/liballoc/heap.rs +++ b/src/liballoc/heap.rs @@ -130,7 +130,6 @@ unsafe fn closure_exchange_malloc(drop_glue: fn(*mut u8), size: uint, #[cfg(jemalloc)] mod imp { - use core::intrinsics::abort; use core::option::{None, Option}; use core::ptr::{RawPtr, mut_null, null}; use core::num::Bitwise; @@ -163,7 +162,7 @@ mod imp { pub unsafe fn allocate(size: uint, align: uint) -> *mut u8 { let ptr = je_mallocx(size as size_t, mallocx_align(align)) as *mut u8; if ptr.is_null() { - abort() + ::oom() } ptr } @@ -174,7 +173,7 @@ mod imp { let ptr = je_rallocx(ptr as *mut c_void, size as size_t, mallocx_align(align)) as *mut u8; if ptr.is_null() { - abort() + ::oom() } ptr } diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 0d8d25bff20..a947378f768 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -94,6 +94,14 @@ pub mod owned; pub mod arc; 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 // as an rlib (it only exists as an rlib). It turns out that an // optimized standard library doesn't actually use *any* symbols diff --git a/src/liballoc/libc_heap.rs b/src/liballoc/libc_heap.rs index 5b189bc672e..25938ba0d54 100644 --- a/src/liballoc/libc_heap.rs +++ b/src/liballoc/libc_heap.rs @@ -13,7 +13,6 @@ use libc::{c_void, size_t, free, malloc, realloc}; use core::ptr::{RawPtr, mut_null}; -use core::intrinsics::abort; /// A wrapper around libc::malloc, aborting on out-of-memory #[inline] @@ -25,8 +24,7 @@ pub unsafe fn malloc_raw(size: uint) -> *mut u8 { } else { let p = malloc(size as size_t); if p.is_null() { - // we need a non-allocating way to print an error here - abort(); + ::oom(); } p as *mut u8 } @@ -43,8 +41,7 @@ pub unsafe fn realloc_raw(ptr: *mut u8, size: uint) -> *mut u8 { } else { let p = realloc(ptr as *mut c_void, size as size_t); if p.is_null() { - // we need a non-allocating way to print an error here - abort(); + ::oom(); } p as *mut u8 }