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.

Memory allocation for MessageQ with data pointer



Hi,

If I want to use MessageQ and send a pointer to data together with the message I do something like:

Create the structure:

typedef struct
{
        MessageQ_MsgHeader header;
        dataType *dataPtr;
} DataMsg;

And in the core (supposing the heap has already been created, opened and the MessageQ registered with it):

dataMsg1 = (DataMsg *) MessageQ_alloc(HEAPID, sizeof(DataMsg));
dataMsg1->dataPtr = (dataType *) HeapBufMP_alloc(heapHandle, sizeof(dataType), align);

My questions are:

1 - I have to do a MessageQ_alloc in every core in which I want to use the message. Should I do a HeapBufMP_alloc to allocate memory for the pointer in all cores too?
2 - If I do HeapBufMP_alloc in all cores, the pointer that is returned will be the same in all cores, in the case that it is in shared memory?
3 - If I do an alloc() I have to do a free(), but if the pointers returned by HeapBufMP_alloc were the same I'd have a problem of freeing a memory region that has already been freed, wouldn't I?

Thanks

  • Hi,

    HeapBufMP manages blocks of memory that are in shared memory. If you do an alloc on core0, it will get one of the blocks. If you do alloc on a different core, you will get a different block of memory (assuming there are free blocks available). You will not get the same block if you do alloc on multiple cores.

    Passing a message with a pointer is fine. Please remember that you must perform cache coherency on the block on memory that the message points to (the HeapBufMP block in your case). MessageQ does not know anything about that block of memory, so it cannot maintain the cache coherency (like it does with the message itself).

    You can call MessageQ_alloc (and the heap alloc) and then send that message to another core. That core can call MessageQ_free (and heap free) or it can re-use the message (and HeapBufMP block).

    Todd

  • Hi Todd,

    Thanks for your answer, just one more thing.

    ToddMullanix said:
    If you do alloc on a different core, you will get a different block of memory

    Dos this also happen when we use HeapMultiBufMP or HeapMemMP?

    Thanks