Hello,
I’ve been working on GUI application and I’m getting my head around the setting of functions for memory management. The documentation of SDL_SetMemoryFunctions says the following
It is not safe to call this function once any allocations have been made
Given that, I assume that the functions are to be defined in the very beginning. However, chances are my project comes across the problem of having these functions to be called safely from different threads. Given that, I need a mutex, but SDL_CreateMutex requires to allocate some memory. Therefore, the memory functions must be ready by the call of SDL_CreateMutex. Therefore, I have some dependency loop.
I have a toy project with physics simulation. It allocates memory once with mmap and gives this chunk of memory to the allocator which manages it with a free list (link) [1].
Within the project, I defined two global variables - the pointer to the allocator and the pointer to the mutex. The memory functions access both of the pointers. The allocator is initialized before the memory functions are set. However, the pointer to mutex remains null. As long as it’s null, the memory functions aren’t safe for the concurrent access - there’s a simple if which checks it. The mutex is created after the memory functions are set. After that the functions become concurrently safe.
It there any better approach to solve the problem?
Best regards,
Aleksei Markov