Is there a way to free all of the objects in a boost::singleton_pool
at once?
I see that I have access to these methods:
static void free(void *const);
static void ordered_free(void *const);
static void free(void *const, const size_type);
static void ordered_free(void *const, const size_type);
I imagine I could use them to free all the objects by iterating through them manually. However, is there any way to say "free all the objects" so that the pool becomes completely free again?
Essentially I would like something like purge_memory()
but that wouldn't release the memory back to the system, but keep it allocated in the pool.
The reason for this is that I build a large object inside the pool (~2GB) which I want to get rid of very quickly and start building a new one. Calling purge_memory()
currently takes around 2s and also releases memory to the system (in diagnostics, or task manager, you can see the Process Memory decreasing). My hope is that by simply freeing things in the pool without actually deallocating, I can blast away the first object very quickly and start building the next.
Let's say you allocated 1000 objects in the singleton_pool totalling 2 GB. If there were a "free_all()" function (which there is not), how would you ensure you do not still have pointers to the data that was contained in the pool? You'd need to iterate over all such pointers to reset them. While you're doing that you might as well call singleton_pool::free(ptr)
on each.
Basically, the API you're asking for does not exist, and probably should not exist. If you want the most flexible (and in my opinion most useful) API, use the regular boost::pool, not the fancier layers on top which often thwart optimizations. It still has no "free_all()" function, however, because again that would be a bug farm.