ESP-IDF vs FreeRTOS heap allocation

NRollo
Posts: 21
Joined: Fri Jan 22, 2021 1:04 pm

ESP-IDF vs FreeRTOS heap allocation

Postby NRollo » Fri Jan 22, 2021 1:43 pm

Hi,

The FreeRTOS documentation suggest to use the pvPortMalloc() / pvPortFree() and vApplicationMallocFailedHook() when handling memory allocation. The ESP-IDF documentation points to heap_caps_malloc() / heap_caps_free() and heap_caps_register_failed_alloc_callback() for the same memory handeling procedures.
So, what would be the correct way of using these functions in a ESP-IDF/FreeRTOS application or maybe the question should be, what memory allocation/handling is included in the ESP-IDF FreeRTOS port?

:?:

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: ESP-IDF vs FreeRTOS heap allocation

Postby ESP_Dazz » Fri Jan 22, 2021 6:59 pm

NRollo wrote: Hi,

The FreeRTOS documentation suggest to use the pvPortMalloc() / pvPortFree() and vApplicationMallocFailedHook() when handling memory allocation. The ESP-IDF documentation points to heap_caps_malloc() / heap_caps_free() and heap_caps_register_failed_alloc_callback() for the same memory handeling procedures.
So, what would be the correct way of using these functions in a ESP-IDF/FreeRTOS application or maybe the question should be, what memory allocation/handling is included in the ESP-IDF FreeRTOS port?

:?:
If you need to allocate some general object (e.g., some buffer), you can just use C stdlib malloc. This will just allocate memory with default capabilities. If you need to allocate memory with a specific capability (e.g., DMA capable), then you'll need to call heap_caps_malloc() and specify the capability. FreeRTOS uses heap_caps_malloc() because task stacks must be placed in internal RAM.

NRollo
Posts: 21
Joined: Fri Jan 22, 2021 1:04 pm

Re: ESP-IDF vs FreeRTOS heap allocation

Postby NRollo » Fri Jan 22, 2021 7:27 pm

Thanks for the reply!

So does FreeRTOS pvPortMalloc() also use heap_caps_malloc()? What about the malloc hook/callback functions, which one should be used if you want to cover all "bases"?

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: ESP-IDF vs FreeRTOS heap allocation

Postby ESP_Dazz » Fri Jan 22, 2021 8:00 pm

NRollo wrote: So does FreeRTOS pvPortMalloc() also use heap_caps_malloc()?
No, because pvPortMalloc() itself is never called. Based on the enabled FreeRTOS configs, pvPortMallocTcbMem() and pvPortMallocStackMem() are called instead. All of these macros are defined in port_macro.h
NRollo wrote: What about the malloc hook/callback functions, which one should be used if you want to cover all "bases"?
Do you mean GNU C malloc hooks?

NRollo
Posts: 21
Joined: Fri Jan 22, 2021 1:04 pm

Re: ESP-IDF vs FreeRTOS heap allocation

Postby NRollo » Fri Jan 22, 2021 11:26 pm

I am thinking of the FreeRTOS vApplicationMallocFailedHook() and the ESP-IDF heap_caps_register_failed_alloc_callback().

Who is online

Users browsing this forum: ESP_Roland and 262 guests