Understanding the Allocator API (for use in real-time contexts)

I'm trying to understand the allocator api better, and I wonder where I can find good resources on that.

I'm working with CPAL, and my goal is to have a separate, real-time compatible allocator such as Rlsf only for the audio thread, while having the non-realtime threads working with the global allocator.

Most allocator libraries seem to implement the global allocator, but don't provide facilities to work in a more fine-grained manner, such as using a custom allocator for certain data structures ...

If anyone could give me a hint on where to find resources, I'd be very grateful :slight_smile:

I'm trying to understand the allocator api better, and I wonder where I can find good resources on that.

...

Most allocator libraries seem to implement the global allocator, but don't provide facilities to work in a more fine-grained manner, such as using a custom allocator for certain data structures ...

This is simply because allocator_api is an unstable feature. Any library that used it would be only compatible with unstable Rust (nightly toolchains). Right now, your choice is between nightly-only and using allocator interfaces that are totally custom, not in std.

That said, in most cases you don't need to allocate memory in your realtime audio thread at all. You can have the non-realtime thread perform all needed allocations and deallocations, and merely transfer ownership to the audio thread when the configuration changes or a new sound is to be played (and back, when it's obsolete — don't deallocate on the audio thread either!).

1 Like

Well, I'm using quite a few nightly features already, so that wouldn't be a problem.

Also, I'm already trying to do the allocations on the non-realtime thread as much as I can, but controlling de-allocations can be tricky and having EVERYTHING allocated in the non-realtime thread might make (some) things extremely awkward and complicated.