Is there any portable way to replace usage of malloc()/free() with wrappers around STL-like allocators?
The context: I have a C library that allows to specify custom malloc()/free()-like functions for memory management, and which is used in multi-threaded contexts. Looking around for a good multi-threaded allocator, I've found that GCC-libstdc++'s mt_alloc performs very well for my workloads. Now I would like to use it in said C library, but how to do it?
The major problem I see is in the deallocate() function, which, contrary to free(), takes the size of the allocated memory block in addition to its address. So I need somehow to keep track of the size associated to every memory allocation, so that it can be fed back to deallocate() when freeing the memory. The simplest solution I've thought about to solve this is to store the size of the allocated memory at the beginning of the memory block, but then I'm not sure how to solve alignment issues that could arise.
Is there any simple solution that I'm overlooking?