RTOS의 힙 사용

기술노트 90383






2018-02-28 오전 2:30


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

죄송하지만, 당사 사이트에서는 Internet Explorer를 지원하지 않습니다.보다 편안한 사이트를 위해 Chrome, Edge, Firefox 등과 같은 최신 브라우저를 사용해 주시길 부탁드립니다.