RT Memory Management

Memory allocation is an optional feature of ChibiOS/RT. The kernel itself never allocates nor frees objects at runtime but memory management services are offered to the application if required.

Global Settings

CH_CFG_USE_MEMCORE This switch enables the core allocator module, this module is required by all the other memory management modules.
CH_CFG_MEMCORE_SIZE Size of the memory block to use as memory core area. If set to zero then the whole available RAM is used.
CH_CFG_USE_HEAP This switch enables the heap allocator module.
CH_CFG_USE_MEMPOOLS This switch enables the memory pool allocator module.
CH_CFG_USE_DYNAMIC This switch enables the dynamic threading services.

Memory Core Allocator

This module is responsible for allocating memory blocks from the system RAM, allocated blocks cannot be returned. The implementation is quite simple:

Memory Core

Note that the Core Allocator module expects two global symbols to be defined:

  • __heap_base__ is the base address of the unused RAM area.
  • __heap_end__ is the end address of the unused RAM area.

The symbols are usually exported by the linker thanks to a specially crafted scatter file. The core allocator allocates blocks starting from the base address up to the end address. The allocated blocks are guaranteed to be aligned to the most restrictive data type for the current architecture. Requesting more memory than the remaining size results in an error.

API

chCoreAlloc() Allocates a memory block of the specified size.
chCoreAllocI() Allocates a memory block of the specified size (I-Class variant).
chCoreGetStatusX() Returns the remaining memory size.

Heap Allocator

API

Memory Pool Allocator

API

Allocators Comparison

All the allocators can be used together but some are best suited in specific scenarios, this is a brief comparison:

Allocator Can Free Constant Time Variable Size From ISR Thread Safe
Core Allocator no yes yes yes yes
Heap Allocator yes no yes no yes
Memory Pool Allocator yes yes no yes yes
C Library Allocator yes no yes no no
  • Can Free indicates the ability of the allocator to return blocks to the available memory.
  • Constant Time is the ability to allocate/free blocks in a constant time, this is important for system determinism.
  • Variable Size is the ability to allocate/free blocks of variable size.
  • From ISR indicates that the allocator services can also be called from ISR context.
  • Thread Safe indicates that the allocator can be used in a multi-threaded environment.

The selection of the appropriate allocator depends on requirements of your application, there is not an universal best solution.

Dynamic Threading