about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorSimon Sapin <simon.sapin@exyr.org>2018-04-04 18:57:48 +0200
committerSimon Sapin <simon.sapin@exyr.org>2018-04-12 22:53:21 +0200
commiteae0d468932660ca383e35bb9d8b0cb4943a82ae (patch)
tree818c14771c58c3fe97bd5d6c18bb94e30fe2064e /src
parent96c9d225a9667bc5ffcbc1594d44c29b201e999c (diff)
downloadrust-eae0d468932660ca383e35bb9d8b0cb4943a82ae.tar.gz
rust-eae0d468932660ca383e35bb9d8b0cb4943a82ae.zip
Restore Global.oom() functionality
… now that #[global_allocator] does not define a symbol for it
Diffstat (limited to 'src')
-rw-r--r--src/Cargo.lock1
-rw-r--r--src/liballoc/alloc.rs16
-rw-r--r--src/liballoc/lib.rs1
-rw-r--r--src/liballoc_jemalloc/Cargo.toml1
-rw-r--r--src/liballoc_jemalloc/lib.rs10
-rw-r--r--src/liballoc_system/lib.rs4
-rw-r--r--src/libcore/alloc.rs4
-rw-r--r--src/librustc_allocator/expand.rs5
-rw-r--r--src/librustc_allocator/lib.rs6
-rw-r--r--src/librustc_trans/allocator.rs2
-rw-r--r--src/libstd/alloc.rs6
-rw-r--r--src/test/compile-fail/allocator/not-an-allocator.rs1
12 files changed, 57 insertions, 0 deletions
diff --git a/src/Cargo.lock b/src/Cargo.lock
index 2e969f4ec2b..e5297d1482e 100644
--- a/src/Cargo.lock
+++ b/src/Cargo.lock
@@ -19,6 +19,7 @@ dependencies = [
 name = "alloc_jemalloc"
 version = "0.0.0"
 dependencies = [
+ "alloc_system 0.0.0",
  "build_helper 0.1.0",
  "cc 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)",
  "compiler_builtins 0.0.0",
diff --git a/src/liballoc/alloc.rs b/src/liballoc/alloc.rs
index a6fc8d5004c..beae52726a6 100644
--- a/src/liballoc/alloc.rs
+++ b/src/liballoc/alloc.rs
@@ -26,6 +26,9 @@ extern "Rust" {
     #[allocator]
     #[rustc_allocator_nounwind]
     fn __rust_alloc(size: usize, align: usize, err: *mut u8) -> *mut u8;
+    #[cold]
+    #[rustc_allocator_nounwind]
+    fn __rust_oom(err: *const u8) -> !;
     #[rustc_allocator_nounwind]
     fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
     #[rustc_allocator_nounwind]
@@ -44,6 +47,9 @@ extern "Rust" {
     #[allocator]
     #[rustc_allocator_nounwind]
     fn __rust_alloc(size: usize, align: usize) -> *mut u8;
+    #[cold]
+    #[rustc_allocator_nounwind]
+    fn __rust_oom() -> !;
     #[rustc_allocator_nounwind]
     fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
     #[rustc_allocator_nounwind]
@@ -120,6 +126,16 @@ unsafe impl Alloc for Global {
             Err(AllocErr)
         }
     }
+
+    #[inline]
+    fn oom(&mut self) -> ! {
+        unsafe {
+            #[cfg(not(stage0))]
+            __rust_oom();
+            #[cfg(stage0)]
+            __rust_oom(&mut 0);
+        }
+    }
 }
 
 /// The allocator for unique pointers.
diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs
index f6598fe5e89..a10820ebefd 100644
--- a/src/liballoc/lib.rs
+++ b/src/liballoc/lib.rs
@@ -97,6 +97,7 @@
 #![feature(from_ref)]
 #![feature(fundamental)]
 #![feature(lang_items)]
+#![feature(libc)]
 #![feature(needs_allocator)]
 #![feature(nonzero)]
 #![feature(optin_builtin_traits)]
diff --git a/src/liballoc_jemalloc/Cargo.toml b/src/liballoc_jemalloc/Cargo.toml
index 7986d5dd2eb..02435170374 100644
--- a/src/liballoc_jemalloc/Cargo.toml
+++ b/src/liballoc_jemalloc/Cargo.toml
@@ -12,6 +12,7 @@ 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" }
diff --git a/src/liballoc_jemalloc/lib.rs b/src/liballoc_jemalloc/lib.rs
index 661d7ab78da..2b66c293f21 100644
--- a/src/liballoc_jemalloc/lib.rs
+++ b/src/liballoc_jemalloc/lib.rs
@@ -14,6 +14,7 @@
             reason = "this library is unlikely to be stabilized in its current \
                       form or name",
             issue = "27783")]
+#![feature(alloc_system)]
 #![feature(libc)]
 #![feature(linkage)]
 #![feature(staged_api)]
@@ -22,12 +23,15 @@
 #![cfg_attr(not(dummy_jemalloc), feature(allocator_api))]
 #![rustc_alloc_kind = "exe"]
 
+extern crate alloc_system;
 extern crate libc;
 
 #[cfg(not(dummy_jemalloc))]
 pub use contents::*;
 #[cfg(not(dummy_jemalloc))]
 mod contents {
+    use core::alloc::GlobalAlloc;
+    use alloc_system::System;
     use libc::{c_int, c_void, size_t};
 
     // Note that the symbols here are prefixed by default on macOS and Windows (we
@@ -98,6 +102,12 @@ mod contents {
 
     #[no_mangle]
     #[rustc_std_internal_symbol]
+    pub unsafe extern fn __rde_oom() -> ! {
+        System.oom()
+    }
+
+    #[no_mangle]
+    #[rustc_std_internal_symbol]
     pub unsafe extern fn __rde_dealloc(ptr: *mut u8,
                                        size: usize,
                                        align: usize) {
diff --git a/src/liballoc_system/lib.rs b/src/liballoc_system/lib.rs
index 4516664e97c..c6507282b24 100644
--- a/src/liballoc_system/lib.rs
+++ b/src/liballoc_system/lib.rs
@@ -367,6 +367,7 @@ mod platform {
     }
 }
 
+#[inline]
 fn oom() -> ! {
     write_to_stderr("fatal runtime error: memory allocation failed");
     unsafe {
@@ -375,6 +376,7 @@ fn oom() -> ! {
 }
 
 #[cfg(any(unix, target_os = "redox"))]
+#[inline]
 fn write_to_stderr(s: &str) {
     extern crate libc;
 
@@ -386,6 +388,7 @@ fn write_to_stderr(s: &str) {
 }
 
 #[cfg(windows)]
+#[inline]
 fn write_to_stderr(s: &str) {
     use core::ptr;
 
@@ -421,4 +424,5 @@ fn write_to_stderr(s: &str) {
 }
 
 #[cfg(not(any(windows, unix, target_os = "redox")))]
+#[inline]
 fn write_to_stderr(_: &str) {}
diff --git a/src/libcore/alloc.rs b/src/libcore/alloc.rs
index cfa7df06a40..7334f986f2b 100644
--- a/src/libcore/alloc.rs
+++ b/src/libcore/alloc.rs
@@ -438,6 +438,10 @@ pub unsafe trait GlobalAlloc {
         }
         new_ptr
     }
+
+    fn oom(&self) -> ! {
+        unsafe { ::intrinsics::abort() }
+    }
 }
 
 /// An implementation of `Alloc` can allocate, reallocate, and
diff --git a/src/librustc_allocator/expand.rs b/src/librustc_allocator/expand.rs
index ce41fe1f3bc..58d4c7f289c 100644
--- a/src/librustc_allocator/expand.rs
+++ b/src/librustc_allocator/expand.rs
@@ -231,6 +231,7 @@ impl<'a> AllocFnFactory<'a> {
             }
 
             AllocatorTy::ResultPtr |
+            AllocatorTy::Bang |
             AllocatorTy::Unit => {
                 panic!("can't convert AllocatorTy to an argument")
             }
@@ -248,6 +249,10 @@ impl<'a> AllocFnFactory<'a> {
                 (self.ptr_u8(), expr)
             }
 
+            AllocatorTy::Bang => {
+                (self.cx.ty(self.span, TyKind::Never), expr)
+            }
+
             AllocatorTy::Unit => {
                 (self.cx.ty(self.span, TyKind::Tup(Vec::new())), expr)
             }
diff --git a/src/librustc_allocator/lib.rs b/src/librustc_allocator/lib.rs
index 969086815de..706eab72d44 100644
--- a/src/librustc_allocator/lib.rs
+++ b/src/librustc_allocator/lib.rs
@@ -24,6 +24,11 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
         output: AllocatorTy::ResultPtr,
     },
     AllocatorMethod {
+        name: "oom",
+        inputs: &[],
+        output: AllocatorTy::Bang,
+    },
+    AllocatorMethod {
         name: "dealloc",
         inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
         output: AllocatorTy::Unit,
@@ -47,6 +52,7 @@ pub struct AllocatorMethod {
 }
 
 pub enum AllocatorTy {
+    Bang,
     Layout,
     Ptr,
     ResultPtr,
diff --git a/src/librustc_trans/allocator.rs b/src/librustc_trans/allocator.rs
index ffebb959ebf..f2dd2ed8460 100644
--- a/src/librustc_trans/allocator.rs
+++ b/src/librustc_trans/allocator.rs
@@ -43,11 +43,13 @@ pub(crate) unsafe fn trans(tcx: TyCtxt, mods: &ModuleLlvm, kind: AllocatorKind)
                 AllocatorTy::Ptr => args.push(i8p),
                 AllocatorTy::Usize => args.push(usize),
 
+                AllocatorTy::Bang |
                 AllocatorTy::ResultPtr |
                 AllocatorTy::Unit => panic!("invalid allocator arg"),
             }
         }
         let output = match method.output {
+            AllocatorTy::Bang => None,
             AllocatorTy::ResultPtr => Some(i8p),
             AllocatorTy::Unit => None,
 
diff --git a/src/libstd/alloc.rs b/src/libstd/alloc.rs
index 335dc7e0412..4e728df010a 100644
--- a/src/libstd/alloc.rs
+++ b/src/libstd/alloc.rs
@@ -37,6 +37,12 @@ pub mod __default_lib_allocator {
 
     #[no_mangle]
     #[rustc_std_internal_symbol]
+    pub unsafe extern fn __rdl_oom() -> ! {
+        System.oom()
+    }
+
+    #[no_mangle]
+    #[rustc_std_internal_symbol]
     pub unsafe extern fn __rdl_dealloc(ptr: *mut u8,
                                        size: usize,
                                        align: usize) {
diff --git a/src/test/compile-fail/allocator/not-an-allocator.rs b/src/test/compile-fail/allocator/not-an-allocator.rs
index 140cad22f34..1479d0b6264 100644
--- a/src/test/compile-fail/allocator/not-an-allocator.rs
+++ b/src/test/compile-fail/allocator/not-an-allocator.rs
@@ -16,5 +16,6 @@ static A: usize = 0;
 //~| the trait bound `usize:
 //~| the trait bound `usize:
 //~| the trait bound `usize:
+//~| the trait bound `usize:
 
 fn main() {}