Click here to Skip to main content
15,612,351 members
Articles / Programming Languages / C++
Posted 30 Nov 2018

Tagged as


3 bookmarked

Fixed Memory Pool Allocation with Custom Class

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
30 Nov 2018CPOL3 min read
A fixed memory pool class written in C++ for generic usage


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:

MemoryPool m_Pool;

Then, call the InitPool method with your required parameters:

// 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:


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 article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Spain Spain
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionWhat is base address for? Pin
Stefan_Lang3-Dec-18 21:09
mveStefan_Lang3-Dec-18 21:09 
AnswerRe: What is base address for? Pin
yafan4-Dec-18 11:25
yafan4-Dec-18 11:25 
GeneralRe: What is base address for? Pin
Stefan_Lang4-Dec-18 21:59
mveStefan_Lang4-Dec-18 21:59 
GeneralRe: What is base address for? Pin
mrsilver10-Dec-18 8:32
mrsilver10-Dec-18 8:32 
QuestionNo benchmark? Pin
Shao Voon Wong30-Nov-18 23:05
mvaShao Voon Wong30-Nov-18 23:05 
AnswerRe: No benchmark? Pin
mrsilver3-Dec-18 1:07
mrsilver3-Dec-18 1:07 
GeneralRe: No benchmark? Pin
Stefan_Lang3-Dec-18 5:18
mveStefan_Lang3-Dec-18 5:18 
GeneralRe: No benchmark? Pin
mrsilver10-Dec-18 8:42
mrsilver10-Dec-18 8:42 
GeneralRe: No benchmark? Pin
Stefan_Lang10-Dec-18 21:34
mveStefan_Lang10-Dec-18 21:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.