Allocator::allocate() metadata


I was wondering how feasible would it be to add an additional metadata value to the allocate() call of the Allocator trait, something like

meta: Option<&'static str>`

(alternatively the type could be *const () to be more general...)

This would be quite useful for example for a custom memory-leak detecting allocator to keep track of the allocation context - Box::new() could pass the type name here.

Apologies if this has been discussed before, I haven't been keeping up with the talks around custom allocators...

see Add optional typed variants of allocation and deallocation routines · Issue #89 · rust-lang/wg-allocators · GitHub

In practice, I think the way this is typically solved is by recording the stack trace at the point of allocation. The stack-trace implicitly includes type information (Vec::<MyType>::grow).

Practically, I've found GitHub - koute/bytehound: A memory profiler for Linux. to just work for these kinds of things, highly recommend!


Yeah, getting it from the backtrace also occurred to me (and tracking allocator needs to save it anyway), but it seemed a bit clunky (different for various containers). But I guess it ends up being a better solution than additions to the alloc api...

bytehound looks cool, thanks

Using #[track_caller] on your implementation of allocate might work. Not sure how it deals with concrete traits.

1 Like