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:

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

The __rust_* functions are defined in 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

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:

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

thx, that's really cool

Last update: Sep 28 2020 at 16:30UTC