Don't use an allocator shim for #[global_allocator]
This makes it possible to use liballoc/libstd in combination with `--emit obj` if you use `#[global_allocator]`. Making it work for the default libstd allocator would require weak functions, which are not well supported on all systems.
This commit is contained in:
parent
db3faa78d0
commit
002aa8ed90
@ -3,10 +3,11 @@
|
|||||||
|
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
|
|
||||||
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
|
use rustc_ast::expand::allocator::{
|
||||||
|
alloc_error_handler_name, AllocatorKind, AllocatorTy, ALLOCATOR_METHODS,
|
||||||
|
};
|
||||||
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
|
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
|
||||||
use rustc_session::config::OomStrategy;
|
use rustc_session::config::OomStrategy;
|
||||||
use rustc_span::symbol::sym;
|
|
||||||
|
|
||||||
/// Returns whether an allocator shim was created
|
/// Returns whether an allocator shim was created
|
||||||
pub(crate) fn codegen(
|
pub(crate) fn codegen(
|
||||||
@ -34,41 +35,43 @@ fn codegen_inner(
|
|||||||
) {
|
) {
|
||||||
let usize_ty = module.target_config().pointer_type();
|
let usize_ty = module.target_config().pointer_type();
|
||||||
|
|
||||||
for method in ALLOCATOR_METHODS {
|
if kind == AllocatorKind::Default {
|
||||||
let mut arg_tys = Vec::with_capacity(method.inputs.len());
|
for method in ALLOCATOR_METHODS {
|
||||||
for ty in method.inputs.iter() {
|
let mut arg_tys = Vec::with_capacity(method.inputs.len());
|
||||||
match *ty {
|
for ty in method.inputs.iter() {
|
||||||
AllocatorTy::Layout => {
|
match *ty {
|
||||||
arg_tys.push(usize_ty); // size
|
AllocatorTy::Layout => {
|
||||||
arg_tys.push(usize_ty); // align
|
arg_tys.push(usize_ty); // size
|
||||||
|
arg_tys.push(usize_ty); // align
|
||||||
|
}
|
||||||
|
AllocatorTy::Ptr => arg_tys.push(usize_ty),
|
||||||
|
AllocatorTy::Usize => arg_tys.push(usize_ty),
|
||||||
|
|
||||||
|
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
||||||
}
|
}
|
||||||
AllocatorTy::Ptr => arg_tys.push(usize_ty),
|
|
||||||
AllocatorTy::Usize => arg_tys.push(usize_ty),
|
|
||||||
|
|
||||||
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
|
|
||||||
}
|
}
|
||||||
|
let output = match method.output {
|
||||||
|
AllocatorTy::ResultPtr => Some(usize_ty),
|
||||||
|
AllocatorTy::Unit => None,
|
||||||
|
|
||||||
|
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
||||||
|
panic!("invalid allocator output")
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let sig = Signature {
|
||||||
|
call_conv: module.target_config().default_call_conv,
|
||||||
|
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
||||||
|
returns: output.into_iter().map(AbiParam::new).collect(),
|
||||||
|
};
|
||||||
|
crate::common::create_wrapper_function(
|
||||||
|
module,
|
||||||
|
unwind_context,
|
||||||
|
sig,
|
||||||
|
&format!("__rust_{}", method.name),
|
||||||
|
&AllocatorKind::Default.fn_name(method.name),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
let output = match method.output {
|
|
||||||
AllocatorTy::ResultPtr => Some(usize_ty),
|
|
||||||
AllocatorTy::Unit => None,
|
|
||||||
|
|
||||||
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
|
|
||||||
panic!("invalid allocator output")
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let sig = Signature {
|
|
||||||
call_conv: module.target_config().default_call_conv,
|
|
||||||
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
|
|
||||||
returns: output.into_iter().map(AbiParam::new).collect(),
|
|
||||||
};
|
|
||||||
crate::common::create_wrapper_function(
|
|
||||||
module,
|
|
||||||
unwind_context,
|
|
||||||
sig,
|
|
||||||
&format!("__rust_{}", method.name),
|
|
||||||
&kind.fn_name(method.name),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let sig = Signature {
|
let sig = Signature {
|
||||||
@ -81,7 +84,7 @@ fn codegen_inner(
|
|||||||
unwind_context,
|
unwind_context,
|
||||||
sig,
|
sig,
|
||||||
"__rust_alloc_error_handler",
|
"__rust_alloc_error_handler",
|
||||||
&alloc_error_handler_kind.fn_name(sym::oom),
|
&alloc_error_handler_name(alloc_error_handler_kind),
|
||||||
);
|
);
|
||||||
|
|
||||||
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
|
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user