How to manage memory

ChibiOS/RT is a static kernel so you don't need to manage memory at all if your application doesn't really require it. This doesn't mean that the OS is unable to manage memory but just that memory management is an optional part of the whole.
The OS offers three distinct ways to manage memory, each one with its weaknesses and strengths:

  • Core Memory Manager.
  • Heap Allocator.
  • Memory Pools.

The three mechanisms are able to coexist and are well integrated, for example the heap allocator uses the core memory manager in order to get more memory blocks, memory pools can optionally do the same thing. Please refer to the kernel reference manual for more details.

Subsystems comparison

This is a small comparison table regarding the three subsystems, C-runtime and static objects are thrown in there for comparison:

Subsystem Free capable Constant time Safe From ISR Notes
Static Objects N/A N/A YES YES Preferred solution for safety applications.
Core Memory Manager NO YES YES YES Fast and safe but unable to free allocated memory.
Heap Allocator YES NO NO NO Unsafe because fragmentation and not constant time, cannot be used from IRQ handlers.
Memory Pools YES YES YES YES Fast and safe but it can handle fixed size objects only, you may have multiple memory pools however.
C-Runtime YES NO NO NO Unsafe because fragmentation, not constant time, cannot be used from IRQ handlers and not thread safe. The C runtime must also be modified in order to work with the other allocators.

When designing a system it is recommended to proceed as follow:

  1. Use static objects and initializers whenever possible.
  2. Where dynamic allocation is required without having to free the allocated memory then use the Core Memory Manager allocation APIs.
  3. Where dynamic allocation is required evaluate if one or more memory pools can be used.
  4. If all the above points do not satisfy your requirements then use the heap allocator.
  5. Consider the C-runtime allocator only for legacy code.
 
chibios/howtos/manage_memory.txt · Last modified: 2011/10/03 20:49 by giovanni
 
Except where otherwise noted, content on this wiki is licensed under the following license:GNU Free Documentation License 1.3