Restore Global.oom() functionality

… now that #[global_allocator] does not define a symbol for it
This commit is contained in:
Simon Sapin 2018-04-04 18:57:48 +02:00
parent 96c9d225a9
commit eae0d46893
12 changed files with 57 additions and 0 deletions

1
src/Cargo.lock generated
View 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",

View file

@ -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.

View file

@ -97,6 +97,7 @@
#![feature(from_ref)]
#![feature(fundamental)]
#![feature(lang_items)]
#![feature(libc)]
#![feature(needs_allocator)]
#![feature(nonzero)]
#![feature(optin_builtin_traits)]

View 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" }

View 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)]
@ -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
@ -96,6 +100,12 @@ mod contents {
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,

View file

@ -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) {}

View file

@ -438,6 +438,10 @@ pub unsafe trait GlobalAlloc {
}
new_ptr
}
fn oom(&self) -> ! {
unsafe { ::intrinsics::abort() }
}
}
/// An implementation of `Alloc` can allocate, reallocate, and

View file

@ -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)
}

View file

@ -23,6 +23,11 @@ pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
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,

View 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,

View 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,

View file

@ -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() {}