How is __rust_dealloc function connectted to __rdl_dealloc function?

I am doing a investigation of memory management in rust. When referred to memory allocation and deallocation, there are too many wrapper above the real function that allocates or deallocates memory. In rustc source code, I found some snippets above __rust_dealloc and rust_alloc:

extern "Rust" {
    // These are the magic symbols to call the global allocator. rustc generates
    // them to call `__rg_alloc` etc. if there is a `#[global_allocator]` attribute
    // (the code expanding that attribute macro generates those functions), or to call
    // the default implementations in std (`__rdl_alloc` etc. in `library/std/src/alloc.rs`)
    // otherwise.
    // The rustc fork of LLVM 14 and earlier also special-cases these function names to be able to optimize them
    // like `malloc`, `realloc`, and `free`, respectively.
    #[rustc_allocator]
    #[rustc_nounwind]
    fn __rust_alloc(size: usize, align: usize) -> *mut u8;
    #[rustc_deallocator]
    #[rustc_nounwind]
    fn __rust_dealloc(ptr: *mut u8, size: usize, align: usize);
    #[rustc_reallocator]
    #[rustc_nounwind]
    fn __rust_realloc(ptr: *mut u8, old_size: usize, align: usize, new_size: usize) -> *mut u8;
    #[rustc_allocator_zeroed]
    #[rustc_nounwind]
    fn __rust_alloc_zeroed(size: usize, align: usize) -> *mut u8;

    static __rust_no_alloc_shim_is_unstable: u8;
}

Here they said __rdl_alloc and so on would be the default implementation of __rust_alloc. However, I failed to find the related code to manage this part of functionarity. Although I used objdump to dump the binary generated by rustc. I got the following instructions about the implementation of __rust__dealloc:

0000000000015680 <__rust_alloc>:
   15680:       e9 4b fb 01 00          jmp    351d0 <__rdl_alloc>

But I still hope to know where I could find what combinate them together.

This forum is about using the Rust programming language and not about how it's implemented.[1] You may have better luck on IRLO.[2] If you cross-post, let us know and leave a link here.


  1. aside from when that helps explain things underspecified in the docs ↩︎

  2. Or Zulip. ↩︎

2 Likes

The code responsible for this is: rust/compiler/rustc_codegen_llvm/src/allocator.rs at master · rust-lang/rust · GitHub