Aspects of using the RTOS heap
Technical Note 90383
11/3/2015 8:07 AM
This Technical Note covers two situations where the application uses the heap. (The heap is managed by the RTOS).
1: Using RTOS heap memory
Your application uses an RTOS, and you want to use the RTOS-provided heap memory for all heap consumers (that is, the calls to malloc, new, printf and FILE operations).
Redirect the library functions malloc and free to the RTOS counterpart.
For example, to override "malloc" to call "OS_malloc", add the following lines to Linker options (Options -> Linker -> Extra Options -> Use command line options):
The following code example illustrates an implementation for the OS_malloc and OS_free functions.
void * OS_malloc(size_t size)
void OS_free(void* ptr)
2: Using an RTOS heap before the RTOS is started
Before it is possible to use an RTOS-provided heap, you often need to initialize the RTOS heap or even start the RTOS.
A common example of "early" heap usage is global C++ constructors that calls the 'new' operator. Global constructors are called at system startup, during "dynamic initialization", which may be before the RTOS heap has been initialized.
Also note that calling other RTOS functions (for example file system operations etc) before the RTOS is started often is a problem.
A. Suppress dynamic initialization during system startup. Use the Linker option (Options -> Linker -> Extra Options):
B. In your application source code, add a call to the library function:
With this solution, the dynamic initialization will be "delayed" until the time of the call to this initialization function.
For an example project, see the link: Example project (IAR Embedded Workbench for ARM 6.30.7).zip
Solution 2, MQX specific info:
According to the MQX 3.8 User Guide, chapter 6.3 Global Constructors: "Initialize the constructors from _bsp_enable_card()"
All product names are trademarks or registered trademarks of their respective owners