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.

Sys/BIOS: Memory Allocation hang up

Other Parts Discussed in Thread: SYSBIOS

Hello,

CCS 4.2.0.6
XDCTools 3.20.4.68
C2000 Code Generation Tools 5.2.8
Sys/Bios 6.30.3.46

I am using the Sys/Bios and have some trouble with the Memory Allocation. The following Code shows that there are two Blocks with a Blocksize of 128 available. In standard usage when I try to allocate less than two Blocks, everything is fine and Memory_alloc returns the pointer to the allocated Block. But when I try to allocate more than two Blocks I expect that Memory_alloc will reutrn a NULL pointer but Memory_alloc will never return. I guess that the Memory_alloc-Function will wait until a used Block is freed up so that it can allocate a new Block.

in CFG-file:

/* Create a heap using HeapBuf */
var HeapBuf = xdc.useModule('ti.sysbios.heaps.HeapBuf');
var heapBufParams = new HeapBuf.Params;
heapBufParams.blockSize = 128;
heapBufParams.numBlocks = 2;
heapBufParams.align = 8;
heapBufParams.sectionName = "heapZone7";
Program.global.heapZone7 = HeapBuf.create(heapBufParams);
Program.sectMap["heapZone7"] = "ZONE7";

in C-file

    Ptr p;

    for(i=0;i<4;i++){
        p = Memory_alloc(heapZone7,128,0,NULL);
        if (p==NULL)
            return NULL;
    }

 

Thanks a lot

Markus

 

 

  • Hi Markus,

    The Memory_alloc is not waiting for a free block. It has terminated the program since no memory was available. The last parameter in the Memory_alloc is an error block. If this is NULL (like you have it) and there is no memory, the program is terminated.

    An Error_Block is a mechanism that allows a richer description of an error (instead of just a return code). Here's an example of how I how write your example.

    In C-file

        Ptr p;
        Error_Block eb;

        Error_init(&eb); // Need to init the small structure
        for(i=0;i<4;i++){
            p = Memory_alloc(heapZone7,128,0, &eb);
            if (p==NULL)
                Error_print(&eb); // if you wanted to send the error information to System_printf
                return NULL;
        }

    You could have done "if (Error_check(&eb)) {" instead of the "if (p = NULL) {", but since Memory_alloc returns NULL in the case of a failure, checking for NULL is more natural.

    One key thing to note when using Error blocks, you must re-init the structure if it is triggered. For example, if you did not return in the about example and just tried to alloc again, you would need to do a "Error_init(&eb)" again.

    Please take a look at the xdc.runtime.Error module for more details.

    Todd

  • Hi Todd,

    thanks, you're answer solves my problem. But I'm very wondering that the examples in the "Users Guide" (Chapter 5.7) show the usage without an error block and there is no annotation to this disadvantage.

    Markus

  • Glad to hear that fixed it. Good point about the "Users Guide". I opened an enhancement request for correct this.