Memory allocation strategies in UGens

Regarding your first example: I’ve been in a similar situation (a RT safe std::make_shared replacement, see https://git.iem.at/pd/vstplugin/-/blob/master/sc/src/rt_shared_ptr.hpp) and one possible solution is to make an extern forward declaration of the interface table, i.e. extern InterfaceTable *ft;. In the actual .cpp file of your plugin, you must define the interface table as non-static, i.e. InterfaceTable *ft; instead of static InterfaceTable *ft. Since the plugin build system should only export symbols which are explicitly marked as SC_API_EXPORT, this is not a problem.

The same technique can be used for custom RT allocators (see the linked code), but honestly, for simple containers with a clear lifetime, like the buffers in your example, custom allocators are probably overkill. Note that such allocators always have to be stateful (store a reference to the corresponding World), so every container using this allocator needs extra 8 bytes (on a 64-bit system). Writing a custom allocator just for std::vector would be over-engineering, because dynamic arrays can be easily managed manually. In VSTPlugin, for example, I only use my custom allocator for my shared_ptr replacement.

2 Likes