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.

malloc and sysbios

Other Parts Discussed in Thread: SYSBIOS

Dear support,

To make a long story short, I was provided two examples from TI in their MCSDK. One was using a linker script and the other was using a config file. Inside the code I am using, there is a flash test where there is a serie of memory allocations of huge size (65536 bytes). When I ported this code from the place it was using linker to the one using a cfg file, the code does not work anymore. Here is the cfg file: 5282.mts.cfg

I found this very interesting post (http://e2e.ti.com/support/embedded/bios/f/355/p/103991/367927.aspx#367927) but I still have a few questions:

1) In the cfg file I provided, is the heap allocated (BIOS.heapSize = 0x3000) common to all the threads created within the application?

2) If I load the same program in different cores, will all the core use the same section in memory to use malloc and free?

3) according the the mapping of the memory, in which section of the memory is this heap located?

Program.sectMap["systemHeap"]         = {loadSegment: "MSMCSRAM", loadAlign:128};    /* XDC Heap .. eg Memory_alloc ()         */
Program.sectMap[".sysmem"]         = "MSMCSRAM";                    /* Malloc heap        */    

4) Is this a better way to allocate a head?

/*
** Create a Heap.
*/
var HeapMem = xdc.useModule('ti.sysbios.heaps.HeapMem');
var heapMemParams = new HeapMem.Params();
heapMemParams.size = 0x300000;
heapMemParams.sectionName = "systemHeap";
Program.global.heap0 = HeapMem.create(heapMemParams);

/* This is the default memory heap. */
Memory.defaultHeapInstance  =   Program.global.heap0;

Thanks for your feedback

Aymeric

  • Sorry, I forgot to mention that I am reading the bios user guide section 6.7 and 6.8.

    5) I also forgot one question, can different trails be used at the same time?

    For instance messages would be using HeapMem to Allocate variable-size blocks and tasks would be using HeapBuf. Allocate fixed-size blocks

    Thanks

    Aymeric

  • Hi Aymeric,

    Good that you are learning about heaps!  As you noted in your next post, reading/studying the content of the SYS/BIOS User's Guide section on heaps will help you a lot.  Then you can post questions here for clarifications to further your understanding.

    Also, it may be useful to you to look up the configuration parameters in the SYS/BIOS API guide (such as your inquiry on BIOS.heapSize).  There you will find explanations about these parameters as well as SYS/BIOS API info.  You can find the API guide under SYS/BIOS in the CCS help menu (or, if you use XGCONF, you can click the "?" icon to automatically take you there).

    aymeric dupont said:
    1) In the cfg file I provided, is the heap allocated (BIOS.heapSize = 0x3000) common to all the threads created within the application?

    This is the size of the system heap.  From the SYS/BIOS API guide:

    "BIOS_heapSize - The system heap is, by default, used to allocate instance object state structures, such as Task objects and their stacks, Semaphore objects, etc.

    If the application configuration does not set Memory.defaultHeapInstance, then SYS/BIOS will create a HeapMem heap of this size. This heap will be assigned to Memory.defaultHeapInstance and will therefore be used as the default system heap. This heap will also be used by the SYS/BIOS version of the standard C library functions malloc(), calloc() and free()."

    aymeric dupont said:
    2) If I load the same program in different cores, will all the core use the same section in memory to use malloc and free?

    You will need to create more than one heap for this.  Then at run time, make sure each core uses a different heap in the code.  You can do this using the DNUM register in your code (it tells you which core the program is currently running on).  E.g.:

        extern volatile cregister Uns DNUM;

        if ((1 << DNUM) & (1)) {
            /* do something on core 0 */
        }

    aymeric dupont said:

    3) according the the mapping of the memory, in which section of the memory is this heap located?

    Program.sectMap["systemHeap"]         = {loadSegment: "MSMCSRAM", loadAlign:128};    /* XDC Heap .. eg Memory_alloc ()         */
    Program.sectMap[".sysmem"]         = "MSMCSRAM";                    /* Malloc heap        */    


    Note that the above code isn't creating any heap.  This code is simply placing the sections named "systemHeap" and ".sysmem" into the memory segment "MSMCSRAM".

    So in your above code, the memory sections will be "systemHeap" and ".sysmem".  If you look at the generated linker map file, you should be able to find those sections, and their addresses should fall into the range of the MSMCSRAM segment.

    Please see section 6.3 "Placing Sections into Memory Segments" of the SYS/BIOS User's Guide for more info on this.

    aymeric dupont said:

    4) Is this a better way to allocate a head?

    /*
    ** Create a Heap.
    */
    var HeapMem = xdc.useModule('ti.sysbios.heaps.HeapMem');
    var heapMemParams = new HeapMem.Params();
    heapMemParams.size = 0x300000;
    heapMemParams.sectionName = "systemHeap";
    Program.global.heap0 = HeapMem.create(heapMemParams);

    /* This is the default memory heap. */
    Memory.defaultHeapInstance  =   Program.global.heap0;


    This looks correct to me.  The code you have above is the way to create a HeapMem type heap (which is one of several types of heaps available in SYS/BIOS).   Also, this line:
    heapMemParams.sectionName = "systemHeap";

    Combined with this line
    Program.sectMap["systemHeap"]         = {loadSegment: "MSMCSRAM", loadAlign:128};    /* XDC Heap .. eg Memory_alloc ()         */

    Will cause the heap to be created in the MSMCSRAM memory segment.

    Note that you can also use the XGCONF GUI within CCS to configure your application graphically.

    Steve

  • Thanks so much Steve for this answer, There is a lot of info in the user guide a your clarification definitely helped me out.

    Regarding your comment:

    extern volatile cregister Uns DNUM;

        if ((1 << DNUM) & (1)) {
            /* do something on core 0 */
        }
    I am not sure what you mean, a function like MessageQ_alloc allows be to provide from what pool I want to allocate, but what about a function like malloc?
    Is it better to use Osal_platformMalloc and then map it to the pool, Is that what you meant?
    Aymeric
  • Aymeric,

    By default SYS/BIOS assigns malloc to use ti.sysbios.heaps.HeapMem and I believe it is the default heap for the application.

    But you don't have to use malloc() to allocate memory.  You could instead call HeapMem_alloc directly on the heap you want to allocate from.

    Let's say that you create 2 different HeapMem's for your application, heap0 and heap1, which will be used for allocations on core0 and core1, respectively.

    Then you could use the DNUM register to determine at run time, which core the code is actually running on.  Then you could have something like the following pseudo code:

    extern volatile cregister Uns DNUM;

        if ((1 << DNUM) & (1)) {
             /* allocate for core 0 */
    HeapMem_alloc(heap0, ...);
        }
        else if ((1 << DNUM) & (2)) {
             /* allocate for core 1 */
             HeapMem_alloc(heap1, ...);
        }

    Steve
  • thanks so much Steve