How to make multi #[global_allocator] in one binary or dylib?

Just as the title, but no solution has been found.
I thought of two possible scenarios:

  1. Take advantage of Cargo's rename feature, such as alloc2 = {package = "alloc"}, and then use another global_allocator for alloc2.
  2. Judge a certain condition on global_allocator and indirectly support multiple Alloc.

That's not possible, as we don't want to let Box<T> allocated from here and deallocated in there.

What are you trying to achieve here?

I feel like having multiple global allocators would contradict the "global" bit, because now you don't know which allocator you are using.

Are you looking for the experimental std::alloc::Alloc trait? This is a trait for something which can be used to allocate memory. For example if you want to allocate a group of objects in an local object pool for performance reasons.

1 Like

Because I now need to use a set of specially modified malloc / free provided by a third party, but also want to use the system malloc / free.

What about creating your own Box<T>-like smart pointer for things allocated by the special malloc/free?

I started putting together a rough example: playground

Thanks your suggestion!

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