Skip to content

Commit 002aa8e

Browse files
committed
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.
1 parent db3faa7 commit 002aa8e

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/allocator.rs

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@
33

44
use crate::prelude::*;
55

6-
use rustc_ast::expand::allocator::{AllocatorKind, AllocatorTy, ALLOCATOR_METHODS};
6+
use rustc_ast::expand::allocator::{
7+
alloc_error_handler_name, AllocatorKind, AllocatorTy, ALLOCATOR_METHODS,
8+
};
79
use rustc_codegen_ssa::base::allocator_kind_for_codegen;
810
use rustc_session::config::OomStrategy;
9-
use rustc_span::symbol::sym;
1011

1112
/// Returns whether an allocator shim was created
1213
pub(crate) fn codegen(
@@ -34,41 +35,43 @@ fn codegen_inner(
3435
) {
3536
let usize_ty = module.target_config().pointer_type();
3637

37-
for method in ALLOCATOR_METHODS {
38-
let mut arg_tys = Vec::with_capacity(method.inputs.len());
39-
for ty in method.inputs.iter() {
40-
match *ty {
41-
AllocatorTy::Layout => {
42-
arg_tys.push(usize_ty); // size
43-
arg_tys.push(usize_ty); // align
44-
}
45-
AllocatorTy::Ptr => arg_tys.push(usize_ty),
46-
AllocatorTy::Usize => arg_tys.push(usize_ty),
38+
if kind == AllocatorKind::Default {
39+
for method in ALLOCATOR_METHODS {
40+
let mut arg_tys = Vec::with_capacity(method.inputs.len());
41+
for ty in method.inputs.iter() {
42+
match *ty {
43+
AllocatorTy::Layout => {
44+
arg_tys.push(usize_ty); // size
45+
arg_tys.push(usize_ty); // align
46+
}
47+
AllocatorTy::Ptr => arg_tys.push(usize_ty),
48+
AllocatorTy::Usize => arg_tys.push(usize_ty),
4749

48-
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
50+
AllocatorTy::ResultPtr | AllocatorTy::Unit => panic!("invalid allocator arg"),
51+
}
4952
}
50-
}
51-
let output = match method.output {
52-
AllocatorTy::ResultPtr => Some(usize_ty),
53-
AllocatorTy::Unit => None,
53+
let output = match method.output {
54+
AllocatorTy::ResultPtr => Some(usize_ty),
55+
AllocatorTy::Unit => None,
5456

55-
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
56-
panic!("invalid allocator output")
57-
}
58-
};
57+
AllocatorTy::Layout | AllocatorTy::Usize | AllocatorTy::Ptr => {
58+
panic!("invalid allocator output")
59+
}
60+
};
5961

60-
let sig = Signature {
61-
call_conv: module.target_config().default_call_conv,
62-
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
63-
returns: output.into_iter().map(AbiParam::new).collect(),
64-
};
65-
crate::common::create_wrapper_function(
66-
module,
67-
unwind_context,
68-
sig,
69-
&format!("__rust_{}", method.name),
70-
&kind.fn_name(method.name),
71-
);
62+
let sig = Signature {
63+
call_conv: module.target_config().default_call_conv,
64+
params: arg_tys.iter().cloned().map(AbiParam::new).collect(),
65+
returns: output.into_iter().map(AbiParam::new).collect(),
66+
};
67+
crate::common::create_wrapper_function(
68+
module,
69+
unwind_context,
70+
sig,
71+
&format!("__rust_{}", method.name),
72+
&AllocatorKind::Default.fn_name(method.name),
73+
);
74+
}
7275
}
7376

7477
let sig = Signature {
@@ -81,7 +84,7 @@ fn codegen_inner(
8184
unwind_context,
8285
sig,
8386
"__rust_alloc_error_handler",
84-
&alloc_error_handler_kind.fn_name(sym::oom),
87+
&alloc_error_handler_name(alloc_error_handler_kind),
8588
);
8689

8790
let data_id = module.declare_data(OomStrategy::SYMBOL, Linkage::Export, false, false).unwrap();

0 commit comments

Comments
 (0)