views:

136

answers:

2

I need a pool struct that can reuse/recycle the memory after usage, i.e., DON'T do any allocation or deallocation on-the-fly (although you still need to allocate the memory at the moment when the program starts)

Boost.Pool does not support such a mechanism; is there any alternative?

+1  A: 

The standard heap mechanisms reuse memory after it is released; a single address range may be used by many different values over the lifetime of a program. However, as you suggest, such mechanisms do acquire memory from the system as needed at runtime.

If you need to preallocate a suitable size chunk of memory and then dole out allocations from that, you'd be looking at sensitive stuff like overloading global operator new and its friends to do allocation out of the preallocated space, presumably throwing exceptions when the preallocated space runs out. [Don't try this without adult supervision - and don't look to me for that supervision; I'm a chicken!]

Jonathan Leffler
+2  A: 

What about just allocating a lot of space for the pool as soon as you create it? The function pool::orderedMalloc(n) seems to do just that.

Yes, you're still allocating at "runtime", but only as the process starts.

drpepper