Where is __rg_alloc and __rdl_alloc defined?


I am new to rustc codes, when reading alloc codes, it mention the alloc call forward to __rg_alloc, etc, but neither I can find the definition of the function nor from google :frowning:

Have you taken a look at this place in the standard library implementation yet?

__rdl_alloc can be found here. I don’t know about __rg_alloc, but judging by that documentation, it sounds like it might be generated automatically when the #[global_allocator] attribute is used, and would be implemented as calling the alloc function of the provided GlobalAllocator instance.

Thanks for pointing our __rdl_alloc which I did find it, but cannot find the __rg_alloc, likely as you said, it might be generated.

I did find the comments

 // 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.

__rg_alloc is defined in the expansion of #[global_allocator]. It doesn't exist if you don't use #[global_expansion].

Where is the code of #[global_allocator] that defines __rg_alloc? Is it a macro or is it generated by the compiler itself?

edit: it's a macro, but I can't find __rg_aloc in the macro's code, because it's.. empty, lol. So, somewhere in the compiler, there is a code that this macro expands to, with the precise definition of __rg_alloc, right?

The #[rustc_builtin_macro] indicates that the macro definition is built into rustc itself. For #[global_allocator] this is in

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.