Memory Pool

by Stephan Pareigis

Memory Pool

by Stephan Pareigis

The memory pool is really fast in allocating and deallocating memory (O(1)). For real-time systems this is the right choice. It works by preallocating a pool of memory blocks. These memory blocks must all have the same size. So this is the downside to live with: This memory pool will only provide memory blocks of a certain size. If you need another size: Simply make another memory pool with that other size. There is also another issue: Memory hogging. Since the memory pools work on preallocated memory, they will never give it back to the operating system.

This source code example shows how the memory pool works. One class is to overwrite new and delete. The other class is to implement the fairly simple memory pool algorithm. Essentially a free stack is used to remember the free memory blocks so that they can be immediately offered to the user upon his new call. A delete will simply put the memory block back to the free stack.

Related