rustc_ast/expand/
allocator.rs

1use rustc_macros::HashStable_Generic;
2use rustc_span::{Symbol, sym};
3
4#[derive(Clone, Debug, Copy, Eq, PartialEq, HashStable_Generic)]
5pub enum AllocatorKind {
6    Global,
7    Default,
8}
9
10pub fn global_fn_name(base: Symbol) -> String {
11    format!("__rust_{base}")
12}
13
14pub fn default_fn_name(base: Symbol) -> String {
15    format!("__rdl_{base}")
16}
17
18pub fn alloc_error_handler_name(alloc_error_handler_kind: AllocatorKind) -> &'static str {
19    match alloc_error_handler_kind {
20        AllocatorKind::Global => "__rg_oom",
21        AllocatorKind::Default => "__rdl_oom",
22    }
23}
24
25pub const NO_ALLOC_SHIM_IS_UNSTABLE: &str = "__rust_no_alloc_shim_is_unstable";
26
27pub enum AllocatorTy {
28    Layout,
29    Ptr,
30    ResultPtr,
31    Unit,
32    Usize,
33}
34
35pub struct AllocatorMethod {
36    pub name: Symbol,
37    pub inputs: &'static [AllocatorMethodInput],
38    pub output: AllocatorTy,
39}
40
41pub struct AllocatorMethodInput {
42    pub name: &'static str,
43    pub ty: AllocatorTy,
44}
45
46pub static ALLOCATOR_METHODS: &[AllocatorMethod] = &[
47    AllocatorMethod {
48        name: sym::alloc,
49        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
50        output: AllocatorTy::ResultPtr,
51    },
52    AllocatorMethod {
53        name: sym::dealloc,
54        inputs: &[
55            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
56            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
57        ],
58        output: AllocatorTy::Unit,
59    },
60    AllocatorMethod {
61        name: sym::realloc,
62        inputs: &[
63            AllocatorMethodInput { name: "ptr", ty: AllocatorTy::Ptr },
64            AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout },
65            AllocatorMethodInput { name: "new_size", ty: AllocatorTy::Usize },
66        ],
67        output: AllocatorTy::ResultPtr,
68    },
69    AllocatorMethod {
70        name: sym::alloc_zeroed,
71        inputs: &[AllocatorMethodInput { name: "layout", ty: AllocatorTy::Layout }],
72        output: AllocatorTy::ResultPtr,
73    },
74];