Heap_4: clarification

The doc[1] says: “heap_4.c is particularly useful for applications that want to use the portable layer memory allocation schemes directly in the application code (rather than just indirectly by calling API functions that themselves call pvPortMalloc() and vPortFree()).”

Is there an example/further explanation somewhere as to how to use this memory allocation scheme without pvPortMalloc() and vPortFree() in application code?

[1] FreeRTOS - Memory management options for the FreeRTOS small footprint, professional grade, real time kernel (scheduler)

If you need to allocate memory dynamically in your application code and you want that memory to come from heap_4, then you need to call pvPortMalloc. What is the motivation of not calling it?

I thought that I would need to call pvPortMalloc, but "heap_4.c is particularly useful for applications that want to use the portable layer memory allocation schemes directly in the application code (rather than just indirectly by calling API functions that themselves call pvPortMalloc() and vPortFree()).” suggests otherwise.

But maybe I am just confused by the documentation :wink:

No, that does not suggest otherwise. FreeRTOS heap is used in the following 2 ways:

  1. Indirectly - When you call a FreeRTOS API which needs to allocate memory internally like xTaskCreate.
  2. Directly - When you allocate memory in application code by calling pvPortMalloc.

The note you mentioned just suggests that heap_4.c is useful for application using the direct method.

I could do exactly the same with heap_2.c (which is what I am actually doing).

Why is heap_4.c better than heap_2.c?

Why is heap_4.c better than heap_2.c?

Because it combines adjacent free memory blocks and therefore, results in less memory fragmentation.

I think now I’m getting it. The usual methods (vPortMalloc() and vPortFree()) are used, but it’s especially well suited for applications using the direct method (vPortMalloc() and vPortFree()) due to it’s algorithm which combines adjacent free memory blocks…

Thanks!