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.

RTOS/AM5728: How can i use malloc() to alloc memory from a HeapBuf

Part Number: AM5728

Tool/software: TI-RTOS

I have create a HeapBuf, and I want to use malloc() function to alloc memory from the HeapBuf. So, what should I do?

The .cfg is follow:

var heapBuf0Params = new HeapBuf.Params();
heapBuf0Params.instance.name = "heapBuf0";
heapBuf0Params.numBlocks = 16;
heapBuf0Params.blockSize = 16777216;
Program.global.heapBuf0 = HeapBuf.create(heapBuf0Params);
HeapBuf.common$.instanceHeap = null;
Memory.defaultHeapInstance = Program.global.heapBuf0;
Memory.common$.instanceHeap = null;

  • Part Number: AM5728

    Tool/software: TI-RTOS

    Following the user's guide, I have created a heapbuf dynamically. And the code is following:

        HeapBuf_Params prms;
        static char buf[0x10000000];
        HeapBuf_Handle heap1;
        Error_Block eb;
        Error_init(&eb);
        HeapBuf_Params_init(&prms);
        prms.blockSize=16;
        prms.numBlocks=0x01000000;
        prms.buf=(Ptr)buf;
        prms.bufSize=0x10000000;
        heap1=HeapBuf_create(&prms,&eb);
        if(heap1==NULL){
            System_abort("HeapBuf create failed");
        }
     
    And I use the Memory_alloc() to alloc the memory,but there is a warning.
     
    code:
     
    codes_socket_buffer.address=(unsigned char*)Memory_alloc(heap1,(1+4+1+CODES_PER_IMG*(65536+sizeof(bzCodeData))+4),0,NULL);
     
    warning:
     
    passing argument 1 of 'xdc_runtime_Memory_alloc__E' from incompatible pointer type [-Wincompatible-pointer-types]
     
    And I simulate the program on the board, there is a termination of the program. The information is following:
     
    Can't find a source file at "/db/ztree/library/trees/newlib/newlib-a00/src/linaro/gcc-arm-none-eabi-7-2017-q4-major/src/newlib/libgloss/arm/swi.h"
     
    But when i use BIOS.heap everything is right. So what should I do?

  • Part Number: AM5728

    Tool/software: TI-RTOS

    when i creat a heapbuf staticlly, how should i use it ? Because when i use memory_alloc(), there is a warning like following:

    passing argument 1 of 'xdc_runtime_Memory_alloc__E' from incompatible pointer type [-Wincompatible-pointer-types]

    And i find people use" IHeap_Handle heap=HeapBuf_Handle_upCast(heapBuf0);" to correct it ,but on me , there is a mistake:

    'heapBuf0' undeclared here (not in a function); did you mean 'heap'?

    So, can you help me?

     

  • There are plenty of older E2E threads that reference this issue. Can you please search and check for responses from out RTOS team

    Example:
    e2e.ti.com/.../103991


    Refer section 7..7.5 in BIOS USer gudie
    dev.ti.com/.../Bios_User_Guide.pdf
  • Thanks for your reply.

    I have searched and checked the threads, but it didn't help me.

    The problem is my algorithm use plenty of malloc() to alloc the memory and I don't want to change it. And I reffered the user's guide which said I can specify the default system heap by the following code:

    var heapBuf0Params = new HeapBuf.Params();
    heapBuf0Params.instance.name = "otherHeap";
    heapBuf0Params.blockSize = 32;
    heapBuf0Params.numBlocks = 10;
    Program.global.otherHeap = HeapBuf.create(heapBuf0Params);
    Memory.defaultHeapInstance = Program.global.otherHeap;

    Then there is a problem:

    The compiler successed, but abnormal program termination happened when run it. So, how can I change the default system heap to make malloc() alloc the memory from the heapbuf which I created? Or, there are some mistakes in the user's guide or I made wrong assumption?

    Following is the screenshot showed the mistake:

  • Could you try the following script :

    /* Create a default system heap using ti.bios.HeapMem. */
    var heapMemParams1 = new HeapMem.Params;
    heapMemParams1.size = 0x50000;
    heapMemParams1.sectionName = "systemHeap";
    Program.global.heap0 = HeapMem.create(heapMemParams1);

    /* This is the default memory heap. */
    Memory.defaultHeapInstance = Program.global.heap0;
    Program.sectMap["systemHeap"] = Program.platform.stackMemory;


    I have tested this on C66x on K2H DSP so should work for you in you application . I believe, I had used this based on guidance from TI RTOS team provided here:
    e2e.ti.com/.../231575

    Regards,
    Rahul
  • Thank you for your replay!
    And it didn't work. When i add the code as you showed, the .out file is too big. So i change the code as follows:


    /* Create a default system heap using ti.bios.HeapMem. */
    var heapMemParams1 = new HeapMem.Params;
    heapMemParams1.size = 0x50000;
    //heapMemParams1.sectionName = "systemHeap";
    Program.global.heap0 = HeapMem.create(heapMemParams1);

    /* This is the default memory heap. */
    Memory.defaultHeapInstance = Program.global.heap0;
    //Program.sectMap["systemHeap"] = Program.platform.stackMemory;



    Then the program stoped when run to malloc(), so what can I do to resolve it. And your code is change the heapmem, so the heapbuf can't be the default memory?
  • Can you help me
  • Hi,

    If you have the following in the .cfg

    var HeapBuf = xdc.useModule('ti.sysbios.heaps.HeapBuf');
    var heapBuf0Params = new HeapBuf.Params();
    heapBuf0Params.instance.name = "heapBuf0";
    heapBuf0Params.numBlocks = 16;
    heapBuf0Params.blockSize = 32;
    Program.global.heapBuf0 = HeapBuf.create(heapBuf0Params);
    Memory.defaultHeapInstance = Program.global.heapBuf0;

    Any of the following will allocate from that default system heap

    #include <stdlib.h>
    #include <xdc/std.h>
    #include <xdc/cfg/Global.h>
    #include <xdc/runtime/Error.h>
    #include <xdc/runtime/Memory.h>
    #include <ti/sysbios/heaps/HeapBuf.h>
    
    
        block[0] = Memory_alloc(NULL, 16, 0, &eb);
        block[1] = Memory_alloc(Memory_defaultHeapInstance, 16, 0, &eb);
        block[2] = Memory_alloc(HeapBuf_Handle_upCast(heapBuf0), 16, 0, &eb);
        block[3] = malloc(16);

    Note: HeapBuf is a fixed block size allocator. So in my example all the blocks are 32 bytes. If you tried to allocate anything larger, the allocation will fail. HeapBuf is nice when you have allocations be approximately the same size and you want a deterministic allocation with no external fragmentation (albeit there might be internal fragmentation). Do you really want to use HeapBuf?

    SYS/BIOS creates a malloc that uses the default heap. The malloc (and related functions) are in the big generated C file in the basement of the project (e.g. Debug\configPkg\package\cfg\<.cfg name>_<target>.c.

    You cannot plug the default heap at runtime...only in the.cfg file. 

    Todd

    [Note: I hit reply on a post too earlier. I tried to delete it, but just in case please ignore that one]

  • Thanks for you replay!
    I have tried it, but the alloc result is error. Can you give me your priject to help me to get the different?
  • Sorry for the delay. I'll post my project tomorrow. It's on a different device, but the idea is the same.
  • Again, it is for a different device, but the code at the bottom of the .cfg and the code in main() shows the HeapBuf creation and usage.

    HeapBufExample.zip

  • Thank you very much, the code worked.