Is there any way to wrap this in C ++ for a smart pointer ( std::unique_ptr ) or otherwise RAII wrappers?
Yes. Here you need a factory function that correctly creates objects that initialize the smart pointer (and ensures that you always correctly create the pointer instances):
std::shared_ptr<SAlloc> make_shared_salloc() { return std::shared_ptr<SAlloc>(new_salloc(), free_salloc); }
You can assign the result of calling these functions to other smart pointers (as needed), and the pointers will be deleted correctly.
Edit: Alternatively, you can specify std::make_shared for your SAlloc.
Edit 2: The second function ( make_unique_salloc ) does not compile. To support the implementation, an alternative delete functor must be implemented.
source share