This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2640R2F: What's the difference between malloc and ICall_malloc

Part Number: CC2640R2F

We are using dynamic memory.. I could not find whether or not using malloc, difference between ICall_malloc and regular malloc in the documents.. Could you elaborate on this topic? Can we interchangable use mallocs?

  • Hi,

    I have assigned an expert to help with your query.

    Best Regards,

    Jan

  • Hi Deniz,

    Thank you for posting in the E2E forums,

    I could not find whether or not using malloc, difference between ICall_malloc and regular malloc in the documents

    ICall_malloc handles the management of dynamic memory allocation between TI-RTOS, the protocol stack, and the application, whereas normal malloc does not.  

    Can we interchangable use mallocs?

    We recommend that applications use the ICall APIs to allocate any dynamic memory. 

    You can find more information about heap and memory management in the ICall section of the BLE5 User's Guide and the Dynamic Memory Allocation section.

    Hope that helps,

    Nima Behmanesh

  • Hi Deniz,

    I just wanted to follow up, was my reply helpful?

    Best,

    Nima Behmanesh

  • Hi Nima,

    I've already read the text you gave.. TI should expand those sections because heap is an important part.. TI should also gave details about block allocation, deallocation methods they used in implementation because there are many heap algorithms with different performance penalties..

    What I really want to learn is what would happen if I mix malloc with ICall_malloc.. If we use "new" operator, it seems to use malloc, instead of ICall_malloc.. Do we have to overload new operator?

    I wish TI redirected malloc to ICall_malloc to prevent misuse.. Or lets ask another question, if there is 2 malloc functions, when should I use standard malloc?

  • Hi Deniz,

    Thank you for your feedback! I will look into these sections and see how we can expand them in the future. 

    lets ask another question, if there is 2 malloc functions, when should I use standard malloc?

    You should not use the standard malloc() because ICall_malloc() handles memory allocation for all the stacks. Additionally, the ICall API allows you to use HeapMem and other heap tracking tools, and the ICall functions are non-blocking. 

    I will get back to you about the "new" operator tomorrow once I've done more research.

    Best,

    Nima Behmanesh

  • Hi Deniz,

    Here is an update about the "new" operator and the different versions of malloc().

    If you're using the BLE stack, then using ICall_malloc() is what is recommended. However, if you're not using TI-RTOS or any other stack then the use of malloc() is okay. If you're using TI-RTOS and not the entire BLE stack, then you would use Memory_alloc() and the associated free functions. Additionally, if you want to implement your own malloc(), you can define it in rtos_heapmem.h. More information can be found here.

    If you want to use the "new" operator, then overloading the "new" operator to use ICall_malloc() is recommended.

    Hope that helps,

    Nima Behmanesh

  • Hi Deniz,

    Just wanted to follow up, was my reply helpful?

    Best,

    Nima Behmanesh

  • Thank's for the clarification. One final question: Is ICall_malloc interrupt, SVC safe? I see examples that use ICall_malloc in interrupt callbacks.. For mailbox etc purporses.. Using dynamic memory in interrupt callbacks makes me nervous..

  • Hi Deniz,

    Yes the ICall_malloc interrupt is thread safe. However, memory allocation routines are not called from high priority threads such as hardware interrupts (HWI) or software interrupts (SWI) due to timing constraints. They may take too long to complete, disrupting the real-time processing of other lower priority tasks. 

    Best,

    Nima Behmanesh