Memory pools allow memory allocation with constant execution time. The memory release for thousands of objects in a pool is just one operation (chunks), not one by one if malloc is used to allocate memory for each object.
Memory pools can be grouped in hierarchical tree structures (like this one), which is suitable for special programming structures like loops and recursions.
Fixed-size block memory pools do not need to store allocation metadata for each allocation, describing characteristics like the size of the allocated block. Particularly for small allocations, this provides substantial space savings.
Allows deterministic behavior on real-time systems avoiding the out of memory errors.
Where This Can Fit
Say you have a large buffer of preallocated memory that we will call the pool.
Say you want to put things on this pool but you want to keep control where everything is located and you don't know how to do it or don't have time to do it.
Say you need something that tells you where you have place to put each small buffer inside the pool knowing its size.
But you don't care about how and where, you just need something nice that tells you where.
Then this class may fit your needs.
For example, in an OpenGL environment, you allocate a buffer large enough and init the pool with this buffer size (say for a vbo buffer), then you can use this class to manage your big buffer into smallest blocks (which is in fact one of my current uses for this class) to manage a large preallocated buffer (pool) in smallest memory areas (or windows) nothing in memory is changed by this class, is up to you if you want to move things around.
In this kind of environment, you can access gpu memory but the gpu does not keep track of the memory areas
So this class will fit your needs.
Another possible use is to manage a large mapped memory area shared or not shared between processes such a memory mapped file
Using the Code
Using this class is pretty simple. You must first initialize the pool and call the
So first, declare a
pool object like:
Then, call the
InitPool method with your required parameters:
virtual bool InitPool(uint64_t size, uint64_t base =0, uint64_t minSplitSize=1024);
This will create a pool of 1MB starting at base address 0. Then to request space, say for example, you need 2048 bytes from the pool:
uint64_t offset =m_Pool.Allocate(2048);
Then to release this memory:
And that's all!!
Points of Interests
Included with the sources of the class is a test suite application when you can get a grasp of how this works, a visual map of the pool is presented to the user, this is done with a custom control which of course could be a nice thing to have for other uses.
This test suite reports the memory pool status and allows to test all the features of the class.
To keep track of allocated chunks and
std::map is used, the pool tries to re-use freed chunks when possible and also tries to split them too if required, this way, we can avoid memory fragmentation.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.