The RTOS kernel has to allocate RAM each time a task, queue or semaphore is created. The malloc() and free() functions can
sometimes be used for this purpose, but ...
- they are not always available on embedded systems,
- take up valuable code space,
- are not thread safe, and
- are not deterministic (the amount of time taken to execute the function will differ from call to call)
... so more often than not an alternative scheme is required.
One embedded / real time system can have very different RAM and timing requirements to another - so a single RAM allocation
algorithm will only ever be appropriate for a subset of applications.
To get around this problem the
memory allocation API is included in the RTOS portable layer - where an application specific implementation appropriate for the
real time system being developed can be provided. When the real time kernel requires RAM, instead of calling malloc() it makes a call
to pvPortMalloc(). When RAM is being freed, instead of calling free() the real time kernel makes a call to vPortFree().
Schemes included in the source code download
Three sample RAM allocation schemes are included in the FreeRTOS source code download (V2.5.0 onwards). These are used by
the various demo applications as appropriate. The following subsections describe the available schemes, when they should be
used, and highlight the demo applications that demonstrate their use.
Each scheme is contained in a separate source file (heap_1.c,
heap_2.c and heap_3.c respectively) which can be located in the Source/Portable/MemMang directory. Other schemes can be
added if required.
Scheme 1 - heap_1.c
This is the simplest scheme of all. It does not permit memory to be freed once it has been allocated, but despite this
is suitable for a surprisingly large number of applications.
The algorithm simply subdivides a single array into smaller blocks as requests for RAM are made. The total size of the array
is set by the definition configTOTAL_HEAP_SIZE - which is defined in FreeRTOSConfig.h.
This scheme:
- Can be used if your application never deletes a task or queue (no calls to vTaskDelete() or vQueueDelete() are ever made).
- Is always deterministic (always takes the same amount of time to return a block).
- Is used by the PIC, AVR and 8051 demo applications - as these do not dynamically create or delete tasks after vTaskStartScheduler() has been called.
heap_1.c is suitable for a lot of small real time systems provided that all tasks and queues are created before the
kernel is started.
Scheme 2 - heap_2.c
This scheme uses a best fit algorithm and, unlike scheme 1, allows previously allocated blocks to be freed.
It does not however combine adjacent free blocks into a single large block.
Again the total amount of available RAM is set by the definition configTOTAL_HEAP_SIZE - which is
defined in FreeRTOSConfig.h.
This scheme:
- Can be used even when the application repeatedly calls vTaskCreate()/vTaskDelete() or vQueueCreate()/vQueueDelete()
(causing multiple calls to pvPortMalloc() and vPortFree()).
- Should not be used if the memory being allocated and freed is of a random size - this would only be
the case if tasks being deleted each had a different stack depth, or queues being deleted were of different lengths.
- Could possible result in memory fragmentation problems should your application create blocks of queues and tasks
in an unpredictable order. This would be unlikely for nearly all applications but should be kept in mind.
- Is not deterministic - but is also not particularly inefficient.
- Is used by the ARM7, and Flashlite demo applications - as these dynamically create and delete tasks.
heap_2.c is suitable for most small real time systems that have to dynamically create tasks.
Scheme 3 - heap_3.c
This is just a wrapper for the standard malloc() and free() functions. It makes them thread safe.
This scheme:
- Requires the linker to setup a heap, and the compiler library to provide malloc() and free() implementations.
- Is not deterministic.
- Will probably considerably increase the kernel code size.
- Is used by the PC (x86 single board computer) demo application.
Copyright (C) 2003 - 2008 Richard Barry
Any and all data, files, source code, html content and documentation included in the FreeRTOS distribution or available on this site are the exclusive property of Richard Barry.
See the files license.txt (included in the distribution) and this
copyright notice for more information. FreeRTOS
TM and FreeRTOS.org
TM are trade marks of Richard Barry.