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.

CCS/CC2640R2F: What's the difference between ICall_allocMsg and ICall_malloc and the associated free functions

Part Number: CC2640R2F

Tool/software: Code Composer Studio

Recently i encountered with a problem in my project, details:

Finally i found the key is i misused a function, where it should use ICall_allocMsg , i called ICall_malloc ,and this action leads to the project breakdown at certain case.

So i'm wondering the difference between the two functions and their free functions. In what situation should i call ICall_malloc as well as ICall_allocMsg.

Looking forward to anything helpful.

  • Hello.The important thing is that only ICall_freeMsg is used to free memory allocated with ICall_allocMsg and only ICall_free is used to free memory allocated with ICall_malloc

    The ICall alloc / free MSG functions are just helper functions for the normal ICall alloc / free functions. The source code for both is available in icall.c:

    void *ICall_allocMsg(size_t size)
    {
      ICall_MsgHdr *hdr =
          (ICall_MsgHdr *) ICall_heapMalloc(sizeof(ICall_MsgHdr) + size);
    
      if (!hdr)
      {
        return NULL;
      }
      hdr->len = size;
      hdr->next = NULL;
      hdr->dest_id = ICALL_UNDEF_DEST_ID;
      return ((void *) (hdr + 1));
    
    }
    
    void *ICall_malloc(uint_least16_t size)
    {
      return (ICall_heapMalloc(size));
    }

    As you can see, ICall_allocMsg adds a message header to the allocation. You could of course accomplish the same thing by adding the header size to your alloc size yourself and then using ICall_malloc. 

    The problem arises if, for example, you use ICall_FreeMsg to free memory that was allocated with ICall_Malloc.  In this case, since you freeing size + header when really only size was allocated, you will end up freeing memory that is still being used. Similarly you will run into problems in the reverse scenario.

  • Your explanation is very clear, but there's still an issue i'm confused about. In theory, we can user ICall_allocMsg or ICall_malloc only if free the memory with right function, but what happened when the memory allocated is transferred to lower layer? Since different way to allocate memory will get different size and different construct of result, is the lower layer with the ability to distinguish two memory buffer?
    From the phenomenon, we know it does be able to distinguish, but i dont know the principle. And when using ICall_allocMsg, is the header used by the lower layer?

    Thank you for your time.
  • Hi. You should almost never use ICall_FreeMsg. It is only used when an ICall message is received from the stack in the application. In the sample apps (simple_peripheral, multi_role, etc), this is only done in the main task function after a message is received with ICall_fetchServiceMsg().

    Note that there were bugs in previous installers where ICall_freeMsg() was used incorrectly. The most recent installers from www.ti.com/blestack have fixed the problem.
  • 1. I viewed code in simple peripheral, but still confusing about when to use ICall_FreeMsg and when to use the other...
    2.Since i have installed the ble sdk of version 2.20 and complete project on it, if i install the latest version, how to move the project to the new version? where to change the configuration?
    Thank you for your time.
  • Hello. Please see the migration guide included in the User's Guide either included with your installer or on dev.ti.com (for example here: dev.ti.com/.../migration.html)

    If you can post the snippet of code that you are worried about, I can tell you which API to use. It is almost always ICall_free
  • Thank you for your answers, but i am not very clear about your words.
    I open the address and it's migration between BLE stack version and different chips, but what i want to know is migration between different versions of SDKs. For example, i am using CC2640R2F SDK 2.0 version and i find that SDK 2.3 is released, and i want to migrate my project to the new SDK, what should i do?
    Since we know that a project can divide into two parts: app project part and library project part, in the case described above, may i simply combine the app project part with the new SDK version library project part?
  • Whatever you are trying to migrate between should be covered in the migration guide. I still don't think this is required. If the above information was not enough for you to know which API to use, please just post the snippet of code that you are worried about and I will tell you which one to use.
  • OK, thank you for your time and patience, thank you very much.