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.

questions about sysBios heap memory management

Other Parts Discussed in Thread: SYSBIOS

I have a question about the heap memory managements in sysBios.

Generally, in sysBios, if I use HeapMem, I can use Memory_alloc and Memory_free to allocate and free memory. I just have to remember from which heap I allocated the memory and free it to where it came from -- what happens if I allocate a memory from HeapMem1 and return it to HeapMem2?

Now to HeapBuf. From SysBios user manual and online wiki, I could not find the API for HeapBuf or HeapMultiBuf alloc/free. Should I use teh same Memory_alloc and Memory_free -- as the handle types for HeapMem and HeapBuf are different. Do I also have to remember from which HeapBuf I allocated the memory and return to it when I free it?

thanks

Weichun

  • Weichun,

    If you have asserts enabled and you free memory to the wrong HeapMem, you'll get an assert (HeapMem_A_invalidFree).

    If you don't have asserts enabled...it might not blow up if you are only using HeapMem. Of course this might depend on the version of SYS/BIOS you are using. You'll probably start having fragmentation problems though. ROV will probably not work properly. I would try to avoid freeing to the wrong heap:)

    In general we recommend using Memory_alloc/free instead of the heap APIs directly (except create()). We have generic default alignment and error handling in the Memory calls. This allows the Heap implementations to make assumptions and have simpler (and easier to maintain) code.

    You always need to keep track of the heap you are using when you call Memory_free.

    Note: there are functions to safely cast the heap handles up to an IHeap_Handle (which Memory uses). For example to use a HeapBuf in Memory_alloc

    HeapBuf_Handle h1;
    IHeap_Handle heap;


    h1 = HeapBuf_create(...);
    heap = HeapBuf_Handle_upCast(h1);
    buf = Memory_alloc(heap, ...);

    Todd