Aspects of using the RTOS heap

Technical Note 90383



9/9/2015 8:17 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).

Solution 1:

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):

--redirect malloc=OS_malloc
--redirect free=OS_free
--redirect realloc=_not_implemented
--redirect calloc=_not_implemented

The following code example illustrates an implementation for the OS_malloc and OS_free functions.

void * OS_malloc(size_t size)
char *p;

// ...

return p;

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.

Solution 2:

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:

#include "iar_dynamic_init.h"

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

Related Tech Notes

© IAR Systems 1995-2018 - All rights reserved.

We use cookies on this website to provide you with a better experience. You need to accept cookies to continue using this site. Cookies