Because of the holidays, TI E2E™ design support forum responses will be delayed from Dec. 25 through Jan. 2. Thank you for your patience.

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.

sample code for HEAP tracking

Guru 15510 points
Other Parts Discussed in Thread: SYSBIOS
Hi,

I want to know how to track the heap size dynamically.
I know there are ROV debug tool which will display heap max size and peak average.
But I want to realize the tracking of heap size from the program.
Is there BIOS API for tracking the heap size which can be call from the program?

I guess HeapTrack API can be used, but I want sample program which using this API.

best regards,
g.f.

  • Hi g.f.,

    Yes, you can use HeapTrack to assert on invalid heap usage at runtime. Here's a code snippet I found that uses it to detect a Memory_free call with an invalid size:

    In the .cfg file:

    ...

    var HeapMem  = xdc.useModule('ti.sysbios.heaps.HeapMem');

    var HeapTrack = xdc.useModule('ti.sysbios.heaps.HeapTrack');

    var heapMemParams = new HeapMem.Params;

    heapMemParams.size = 512;

    heapMemParams.align = 8;

    var memHeap = HeapMem.create(heapMemParams);

    var heapTrackParams = new HeapTrack.Params;

    heapTrackParams.heap = memHeap;

    Program.global.task0Heap = HeapTrack.create(heapTrackParams);

    In the .c file:

    Void main()

    {

       Task_create(task0Fxn, NULL, NULL);

       System_printf("HeapTrack test started.\n");

       BIOS_start();

    }

    /*

    *  ======== task0Fxn ========

    */

    Void task0Fxn(UArg arg0, UArg arg1)

    {

       Ptr buf;

       IHeap_Handle heap;

       heap = HeapTrack_Handle_upCast(task0Heap);

       System_printf("Free wrong size\n");

       /* print initial heap status */

       printHeapStats(heap);

       /* allocate block from heap2 and free with a wrong size */

       buf = Memory_alloc(heap, TASK1BUFSIZE2, 0, NULL);

       Memory_free(heap, buf, TASK1BUFSIZE2 / 2);

       /* print heap0 status */

       printHeapStats(heap);

       System_printf("Task0 Complete\n");

       BIOS_exit(0);

    }

    static Void printHeapStats(IHeap_Handle heap)

    {

       Memory_Stats stats;

       Memory_getStats(heap, &stats);

       System_printf("largestFreeSize = %d\n", stats.largestFreeSize);

       System_printf("totalFreeSize = %d\n", stats.totalFreeSize);

       System_printf("totalSize = %d\n", stats.totalSize);

    }

     

    In addition, note that depending on what you want to achieve, the Memory_getStats API can be called directly on any IHeap instance to retrieve the heap's info programmatically.

    Best regards,

    Vincent

  • Hi Vincent,

    Thank you for the reply and the sample code.
    I will try the code.

    best regards,
    g.f.