Microchip-MPLAB-Harmony / net

Harmony 3 Network library
https://onlinedocs.microchip.com/v2/keyword-lookup?keyword=MH3_net&redirect=true
29 stars 9 forks source link

MALLOC Question #30

Closed Skaptor closed 1 year ago

Skaptor commented 3 years ago

Hello this is not an issue, rather a question regarding the malloc usage in the TCP/IP stack. I'm currently using heap4 in freeRTOS in a PIC32MZ. Why is it using the standard malloc instead of the "safer" OS alternative?

adrian-aur commented 2 years ago

The TCP/IP stack uses memory allocation just at the initialization to allocate its own heap. After that it will use only its own private heap. This will work fine as long as you initialize the stack at system start up and then just use it. If you intend to turn it down and then up again at run time, then definitely a thread safe allocation function should be used. And that's why you can specify the allocation function when you initialize the stack.

marcFriesen commented 2 years ago

Isn't that only true when the TCPIP core is configured for Internal Heap? This article suggests using External Heap: https://microchipsupport.force.com/s/article/Handling-heap-configuration-for-TCP-IP-CORE--harmony-component-in-PIC32MZW1-WFI32-device

adrian-aur commented 2 years ago

I see the article you mention. It is written by users of the TCP/IP stack on Wi-Fi platforms. The overhead of an internal heap isn't too big, the allocation routines are relatively small. I think it should be comparable with the external one. But I haven't really done a comparison for this. Even for the external heap there is some internally maintained data.

What's said above is true for the internal heap, yes. Only then at the stack initialization it tries to allocate its own heap. When using an external heap the stack will use it directly and it will create a semaphore for multi-threaded protection, if the flag TCPIP_STACK_HEAP_FLAG_NO_MTHREAD_SYNC is cleared.