Heap management with newlib

in freeRTOS, I enable the configUSE_NEWLIB_REENTRANT to 1 and also I am using the heap4.c but i have some queries about.

  1. what is the difference between heap4 and newlib?
  2. why heap4 and why not newlib?
  3. why newlib and why not heap4?
  4. which one is to be preferred?
  5. can we use both heap4 and newlib?

thank you.

  1. Descriptions of the various FreeRTOS-supported heaps can be found here. Heap4 aims to provide a small, threadsafe implementation of malloc/free functions. Newlib (and newlib-nano) provides a minimal-size library which includes malloc/free functions. Newlib includes additional functions that the FreeRTOS heap will not.
  2. The FreeRTOS supported heaps should be the most easy to integrate with a FreeRTOS solution on an embedded device.
  3. I don’t know all of what newlib offers but I believe this solution would be preferred if you needed many of the functions newlib provides. I was unable to find an exact API list in a quick search but you may find it on https://sourceware.org/newlib/docs.html.
  4. This is up to you to choose as the application developer. Newlib is popular which is why it has a configuration macro as mentioned here. I cannot say if it is more popular than a FreeRTOS-supported heap.
  5. It might be possible to override newlib to use the pvPortMalloc, vPortFree, etc. APIs. This might allow you to use newlib functions which call the FreeRTOS heap under the hood.
    Looks like its actually recommended NOT to use a FreeRTOS heap with newlib. See newlib and FreeRTOS
1 Like