]> git.lizzy.rs Git - rust.git/commitdiff
Restore Global.oom() functionality
authorSimon Sapin <simon.sapin@exyr.org>
Wed, 4 Apr 2018 16:57:48 +0000 (18:57 +0200)
committerSimon Sapin <simon.sapin@exyr.org>
Thu, 12 Apr 2018 20:53:21 +0000 (22:53 +0200)
… now that #[global_allocator] does not define a symbol for it

12 files changed:
src/Cargo.lock
src/liballoc/alloc.rs
src/liballoc/lib.rs
src/liballoc_jemalloc/Cargo.toml
src/liballoc_jemalloc/lib.rs
src/liballoc_system/lib.rs
src/libcore/alloc.rs
src/librustc_allocator/expand.rs
src/librustc_allocator/lib.rs
src/librustc_trans/allocator.rs
src/libstd/alloc.rs
src/test/compile-fail/allocator/not-an-allocator.rs

index 2e969f4ec2bbcdd0313995473e1118e2b2eee1d0..e5297d1482e8b57b711e53f72ee33bf0d450f136 100644 (file)
@@ -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",
index a6fc8d5004c29346697ac50dd2107fb0c64a6427..beae52726a6ecf1a0f51428e58aab2c52aa3dcaf 100644 (file)
@@ -26,6 +26,9 @@
     #[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 @@ fn __rust_realloc(ptr: *mut u8,
     #[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 fn alloc_zeroed(&mut self, layout: Layout) -> Result<*mut u8, AllocErr> {
             Err(AllocErr)
         }
     }
+
+    #[inline]
+    fn oom(&mut self) -> ! {
+        unsafe {
+            #[cfg(not(stage0))]
+            __rust_oom();
+            #[cfg(stage0)]
+            __rust_oom(&mut 0);
+        }
+    }
 }
 
 /// The allocator for unique pointers.
index f6598fe5e89695549a10dde933de4fc8a125e191..a10820ebefd00adfc140c18b0a7fbab3d4f26b2b 100644 (file)
@@ -97,6 +97,7 @@
 #![feature(from_ref)]
 #![feature(fundamental)]
 #![feature(lang_items)]
+#![feature(libc)]
 #![feature(needs_allocator)]
 #![feature(nonzero)]
 #![feature(optin_builtin_traits)]
index 7986d5dd2eb54b69a12990dc83891ba320a7e838..02435170374c503c81b7111974ebfc0266e2d3a1 100644 (file)
@@ -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" }
index 661d7ab78da01d54bf3809cc2c16918edd5c0f13..2b66c293f21a025b16075103a640fa36c61b7d29 100644 (file)
@@ -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)]
 #![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
@@ -96,6 +100,12 @@ fn align_to_flags(align: usize, size: usize) -> c_int {
         ptr
     }
 
+    #[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,
index 4516664e97c55cad7782b3ea7f03429578bb46d5..c6507282b244d1409b93b9cf5fdb130bd2eedf86 100644 (file)
@@ -367,6 +367,7 @@ unsafe fn realloc(&self, ptr: *mut Void, layout: Layout, new_size: usize) -> *mu
     }
 }
 
+#[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 WriteFile(hFile: HANDLE,
 }
 
 #[cfg(not(any(windows, unix, target_os = "redox")))]
+#[inline]
 fn write_to_stderr(_: &str) {}
index cfa7df06a40e17e9e62089cdaa6f57a518798eee..7334f986f2baac3e1a179b901e2ceeefb62ad332 100644 (file)
@@ -438,6 +438,10 @@ unsafe fn realloc(&self, ptr: *mut Void, old_layout: Layout, new_size: usize) ->
         }
         new_ptr
     }
+
+    fn oom(&self) -> ! {
+        unsafe { ::intrinsics::abort() }
+    }
 }
 
 /// An implementation of `Alloc` can allocate, reallocate, and
index ce41fe1f3bc9e0683db1f0805fef2425f55da414..58d4c7f289c39a400a0d67604b5b0c9c9b0ccb45 100644 (file)
@@ -231,6 +231,7 @@ fn arg_ty(&self,
             }
 
             AllocatorTy::ResultPtr |
+            AllocatorTy::Bang |
             AllocatorTy::Unit => {
                 panic!("can't convert AllocatorTy to an argument")
             }
@@ -248,6 +249,10 @@ fn ret_ty(&self, ty: &AllocatorTy, expr: P<Expr>) -> (P<Ty>, P<Expr>) {
                 (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)
             }
index 969086815ded4777c3cc6939d9e3be4fc58be6b0..706eab72d44cc8af296a9fec233d3bf5babeb347 100644 (file)
         inputs: &[AllocatorTy::Layout],
         output: AllocatorTy::ResultPtr,
     },
+    AllocatorMethod {
+        name: "oom",
+        inputs: &[],
+        output: AllocatorTy::Bang,
+    },
     AllocatorMethod {
         name: "dealloc",
         inputs: &[AllocatorTy::Ptr, AllocatorTy::Layout],
@@ -47,6 +52,7 @@ pub struct AllocatorMethod {
 }
 
 pub enum AllocatorTy {
+    Bang,
     Layout,
     Ptr,
     ResultPtr,
index ffebb959ebfde494abdd58d680e008da35f76f18..f2dd2ed8460ebbfa65d1776cae8d703d1a5c8d1b 100644 (file)
@@ -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,
 
index 335dc7e0412326f44a88ab99460af4b142792d9c..4e728df010a47cae5ce4166531dd8b0942d89714 100644 (file)
@@ -35,6 +35,12 @@ pub mod __default_lib_allocator {
         System.alloc(layout) as *mut u8
     }
 
+    #[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,
index 140cad22f34e42ae52b5b94ffe1b071fd9c3ff41..1479d0b62642a76cc8267994d270e196ce0eade0 100644 (file)
@@ -16,5 +16,6 @@
 //~| the trait bound `usize:
 //~| the trait bound `usize:
 //~| the trait bound `usize:
+//~| the trait bound `usize:
 
 fn main() {}