Stream: t-compiler/help

Topic: how __rust_alloc and friends are defined


Jason Hu (Jul 24 2020 at 14:50, on Zulip):

Hi all,

I am looking into dynamic memory of rust and I see that they eventually go to __rust_alloc and friends. but the LLVM code simply leaves them declared. how are these function implemented?

lzutao (Jul 24 2020 at 14:54, on Zulip):

They are defined here: https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/alloc.rs#L326-L379

bjorn3 (Jul 24 2020 at 14:58, on Zulip):

The __rust_* functions are defined in https://github.com/rust-lang/rust/blob/master/src/librustc_codegen_llvm/allocator.rs. They dispatch to either __rdl_alloc or __rg_alloc depending on if #[global_allocator] is used or the default libstd implementation.

bjorn3 (Jul 24 2020 at 14:58, on Zulip):

The __rg_alloc methods are expanded from #[global_allocator] by https://github.com/rust-lang/rust/blob/8534be72fc3b9c5f2f2dc4e4ee7b651a008e9a3e/src/librustc_builtin_macros/global_allocator.rs

Jason Hu (Jul 24 2020 at 15:01, on Zulip):

thx for the pointer. then it seems to go to GlobalAlloc::alloc but it's just a trait. how this function gets injected then?

bjorn3 (Jul 24 2020 at 15:03, on Zulip):

In the __rdl_alloc case, System.alloc() is called. In the __rg_alloc case .alloc() is called on the static that has the #[global_allocator] attribute.

lzutao (Jul 24 2020 at 15:04, on Zulip):

For system allocators, on Unix, it is listed here: https://github.com/rust-lang/rust/blob/900869371e13cead086f4f9809419daa6a63cfaf/src/libstd/sys/unix/alloc.rs#L6-L24

Jason Hu (Jul 24 2020 at 15:07, on Zulip):

thx, that's really cool

Last update: Sep 28 2020 at 16:30UTC