A C++ fixed allocator

Dynamic memory management on small footprint embedded systems is often a bad practice. Granted: small footprint systems are getting to be less common. But when you can perform the task at hand without the complexity of dynamic memory allocation an engineer should be biased toward doing so.

If the device you are working on is safety critical then it may be mandatory. Certainly the MISRA rules state exactly so:

MISRA C++:2008 – Guidelines for the use of the C++ language in critical systems

6.18.4 Dynamic memory management

Blah, blah, blah .. prohibited.

Is it possible to use the standard C++ container library without using dynamic memory management? Yes: you will have to write a simple custom allocator to do so.

This must be a common practice; and yet finding an example which suits my needs was not a simple google search away. The closest I found was from Howard Hinnant and his code provided me with a nice starting point. This is a derivative work of his class short_alloc and documentation from cppreference.com.

Hinnant’s class has a dependency on his arena class and that is not something I need. The arena class seems to be concerned with alignment and provides the actual memory to the allocator.

I would like to take a different approach: Use C language arrays and their types to provide the memory allocation and provide them to the allocator for use. By providing the template parameter data_type to the fixed_allocator the allocation alignment is guaranteed for that data type. If customized alignments were required I would use the GNU extension __attribute__((aligned(n))). I have not found any need for this in practice.

Rather than copy/paste source code, here is the link to my fixed_allocator.h implementation and its google unit test.

There is one dependency baked into the class fixed_allocator that you probably will not want: #include "project_assert.h". You can replace this with good old <cassert> like so:

#include <cassert>

static inline void ASSERT(bool test) { assert(test); }

The unit test provides an example of how to use the fixed_allocator class. Here is the gist of it:

  • An underlying C language array is allocated.
    • The allocation may be on the stack.
    • The allocation may be static – typically within the module being used.
    • Note that a C++ std::array could be used here.
    • The data type of the array will determine alignment.
  • The allocated array is passed into the fixed_allocator constructor.
  • The fixed allocator may only be used once.
    • For a vector, this means making a call the the member function reserve().
    • If more than once call is made to the allocator then an ASSERT() will fail.
  • Being targeted at small embedded systems, rtti and exceptions are disabled.
    • Size profiling with exceptions enabled shows ~20% code size growth.
    • Size profiling with rtti enabled shows yet another 20% code size growth.
    • I would love to exploit these features but this level of code size is not worth the price.

Published by natersoz

Electrical Engineer from Rose-Hulman

Leave a comment