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.

Probelm with dynamic management of heap create -> delete

Other Parts Discussed in Thread: SYSBIOS

Hello

Using HeapBuf object in my project I found very serious problem with dynamic deletion of dynamically created heap.

Dynamic creation of HeapBuf or HeapMem heaps requires passing of already allocated memory buffer and its size to be used actually by the heap. Passing of these parameters is done by HeapBuf_Params structure fields: 'buf' and 'bufSize' passed to HeapBuf_create() function as the first argument. Until now everything seems to be fine - HeapBuf object is created, visible in SYSBIOS resources and it works. But finally or for some reason when user wants to delete dynamically created heap using HeapBuf_delete() function, then big problem appears: function HeapBuf_delete() of course releases memory used to create instance of heap object but it does NOT release memory buffer passed to the function in HeapBuf_Params structure during construction. It is OK bacause this buffer doesn't have to be dynamically allocated, but if it is, then we have problem bacause HeapBuf and well HeapMem objects does NOT provide functions to get heap buffer parameters from the object, in this way we have no access to this buffer and its size, and cannot release this buffer to proper heap.

and here is my request to TI experts responsible for creation and maintanance of SYSBIOS - please provide a way to release internal heap buffer before deleting heap object. Few methods seems to be ok:

1. Implement Ptr HeapBuf_getBufSize(size_t * pSize);

or alternatively implement Ptr HeapBuf_getBuf(HeapBuf_Handle) and size_t HeapBuf_getBufSize(HeapBuf_Handle)

2. Add 2-nd parameter of IHeap_Handle to HeapBuf_delete() It should be heap used to allocate this buffer before heap creation; if it is not NULL then it should be used to free internal heap buffer

Best regards

Gregor

ps. this problem applies also to HeapMultiBuf object

  • Gregor,

    Thank you for the suggestion!

    But, these create functions don’t know enough about the pre-allocated user-supplied buffer that is passed in, so don’t know how to properly free the buffer.  For example, the buffer could possibly be a static buffer, or allocated by some application-supplied allocator.  

    So, it is the responsibility of the application that allocates and passes in the buffer, to free that buffer after the Heap*_delete() function is called.

    Scott

  • Scott

    I agree with you that app is responsible for freeing this buffer but is requires from me saving this buffer pointer and its size outside heap object whereas I know, that these parameters are already stored in Heap object, so why to create another copy of the same information? Even if I do this and save the copy of buffer pointer and its size for example in wrapper for Heap object - it would require creating of next wrapper type for Heap, increasing complexity of Heap usage.

    summarizing - of course there are many ways to enable releasing of dynamically created heap buffer, but only the one where buffer pointer and its size is read from Heap object does not require more memory and this is the simplest method, other solutions introduce additional complexity to the code.

    so again I ask you to extend interface to HeapBuf and HeapMem to enable releasing of internal heap buffer in the simplest possible way.

    Gregor

  • Gregor,

    I see what you are saying regarding duplicated storage of pointer and size, but on the other hand, adding these accessor functions will increase code footprint, so there is a tradeoff.

    I just filed an enhancement request for this on your behalf (SDOCM00096772), for consideration for a future release.

    Scott

  • Thank you Scott :)

    I am waiting for future release.

    I would like to explain you why it is so important for me - it is because dynamic usage of HeapBuf_create() requires that sizes of allocated blocks must be aligned to xdc.runtime.Memory.getMaxDefaultTypeAlign I hide this very specific requirement from the application in the creator function

    HeapBuf_Handle MyCreateHeap(blockSize, blockCount);

    This function allows the application to request any block size, for that reason application does not know what is the real size of blocks, it is only passed to HeapBuf object, which in turns is returned to the application. I think that many developers would do it in this way and this is the real cause of my request.

    Best regards

    Greg

  • Greg,

    OK, thanks for clarifying. 

    This info is now part of the feature request...

    Scott