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.

HeapMem_free arguments

Void HeapMem_free(HeapMem_Handle handle, Ptr block, SizeT size);

Why free() needs so much arguments? Aren't size and handle determined by pointer?

Suppose I transmit pointer from CORE0 to CORE1 via MessageQ and need to free it from CORE1 after processing. How is CORE1 supposed to know from which heap the block was allocated?

  • Hi Sam,

    The handle contains module status information about the previously created HeapMem object.  It does not maintain a list of the pointers & sizes allocated, so you must specify the location of memory and how much to free.

    Sam Body said:
    Suppose I transmit pointer from CORE0 to CORE1 via MessageQ and need to free it from CORE1 after processing. How is CORE1 supposed to know from which heap the block was allocated?

    After registering a heap, MessageQ manages allocation with the MessageQ_alloc/free() functions.  You do not explicitly call the HeapMem_free (assuming it is the type of heap used), but rather use MessageQ_free().  The IPC User's Guide (link) has a pretty good explanation on how the heap is managed.

    Regards,

    -- Emmanuel

  • OK, I describe what I am trying to do in more detail (pseudocode)

    CORE0:

    1. payload = HeapMem_alloc(my_heap, 1MB);

    2. MessageQ msg = MessageQ_alloc(msgq_heap, sizeof(MyMessage) );

    3. ((MyMessage *)msg)->payload = payload;

    4. MessageQ_put(msg);

    CORE1:

    1. MessageQ msg = MessageQ_get();

    2. process ( ((MyMessage *)msg)->payload  )

    3. HeapMem_free( ???HEAP???, ((MyMessage *)msg)->payload???SIZE??? );

    4. MessageQ_free(msg);

    So if I want to transmit one single pointer I also include in MyMessage

    1. Size

    2. Heap handle

    only because HeapMem_free needs it. Not convinient at all, given that these information is defined by pointer itself. Usual free() from stdlib.h needs only the pointer.

    So without transmitting Heap handle CORE1 must determine the heap and size by pointer.

    I suppose HeapMem API may include for convinience a function like following:

    Heap HeapMem_heapByPtr(Ptr ptr);

    But better this:

    HeapMem_free(Ptr ptr)

  • You cannot HeapMem_free a pointer that was allocated on a different core.  There is a HeapMemMP module that would serve that purpose.

    There's good reason for the SYS/BIOS *_free() APIs to require a handle and size, in addition to the pointer.  They are tailored to an embedded RTOS environment where precise application control can be critical.  You could also just use malloc() and free() in your app, if you wish.

    Regards,

    - Rob 

  • I forgot to mention that you also can't malloc a pointer on one core and free it on the other.  I recommend using the HeapMemMP module.

    Regards,

    - Rob