Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C++

Fixed Memory Pool Allocation with Custom Class

5.00/5 (2 votes)
30 Nov 2018CPOL3 min read 9.9K   162  
A fixed memory pool class written in C++ for generic usage

Introduction

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 InitPool method.

So first, declare a pool object like:

C++
MemoryPool m_Pool;

Then, call the InitPool method with your required parameters:

C++
// Pool initializer, this function must be called after pool object is declared
// you should pass the size of the pool you want to manage
// base is the "address" you want to start the pool usually will be the address
// of your large buffer if you want to get addresses when invoking Allocate
// or 0 in which case values returned by Allocate will be equivalent to
// relative offsets measured in bytes
// to the real address of your big buffer

virtual bool InitPool(uint64_t size, uint64_t base =0, uint64_t minSplitSize=1024);

For example:

C++
m_Pool.InitPool(1024*1000,0);

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:

C++
uint64_t offset =m_Pool.Allocate(2048);

Then to release this memory:

C++
m_Pool.Free(offset);

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)