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.

Memory allocation within a clock instance function

Other Parts Discussed in Thread: SYSBIOS

From reading this page:

http://processors.wiki.ti.com/index.php/GateMutex_Assert

My understanding is that malloc() cannot be called from within a clock instance function by default. The recommendation on that page is to either allocate memory prior to the clock instance or use an alternate heap implementation.

Is there a recommended alternative for memory location within a clock instance function? Are there any recommended settings that can be made in SYSBIOS for an alternate non-locked heap?

Chris

Signalogic

  • Hi Chris,

    As recommended on the link you posted, you can use a different heap provider.

    If you want to continue to use the same heap provider, you can change the C Standard Library Lock from GateMutex to GateSwi. I recommend GateSwi since you plan to call malloc() from a Clock function which runs in Swi context.

    Best,

    Ashish

  • Ashish,

    I tried changing the C Standard Library Lock to GateSwi, but malloc() calls in a clock function still block. I also tried GateHwi and NoLocking but no luck there either. Are any other SYSBIOS settings needed or could any SYSBIOS settings conflict with changing the Library Lock?

    Thanks,

    Chris

  • Ashish-

    It appears our call to malloc() from within a clock instance (SWI task) can't get the mutex, so it hangs.  As Chris mentions we've tried the various "C Standard Library Lock" options in SYSBIOS config.

    Question -- when our clock instance task runs the first time, who else has the mutex?  Is SYSBIOS need malloc at that point?  There are no other tasks, just the one clock instance task we create dynamically in main().

    Thanks.

    -Jeff

  • Hi Chris,

    Chris Johnson1 said:

    I tried changing the C Standard Library Lock to GateSwi, but malloc() calls in a clock function still block. I also tried GateHwi and NoLocking but no luck there either. Are any other SYSBIOS settings needed or could any SYSBIOS settings conflict with changing the Library Lock?

    If you change the lock type to GateSwi, then when you call malloc() from within your clock swi function, the rts_lock() function internally calls GateSwi_enter() which simply does a Swi_disable() and returns. If an interrupt occurs just before the Swi_disable() completes and posts a higher priority swi, the higher priority swi will execute first but the execution will return to the malloc() and the malloc() call will complete. The clock swi function cannot block if GateSwi is used as the library lock.

    How are you concluding that the clock swi function is blocking ?

    Best,

    Ashish

  • Ashish,

    I have a section of code that looks something like this:

    probe |= 1;

    if (ptr = malloc()){ 
       probe |= 2;
       ...
    }

    probe |= 4;
     
    When I read back the value of probe it is 1, so it doesn't look like it gets past the malloc() call. Could the issue be that I don't have the write memory/heap modules in use or setup properly? I am not using the Memory module or any of the Heap modules for SYS/BIOS. I just have the Heap size and Heap section set in the SYS/BIOS basic runtime options where it says "The heap configured above is used for standard C malloc() and free() functions...".

    Thanks,

    Chris

  • Ashish-

    A few more data points:

    1) If our code is not inside a clock instance task, it runs Ok.  No change in malloc() etc.  Mem allocation is done on the first run of the task only, after that we make calls to TI multimedia codecs.  Only in the clock instance task does malloc() hang.

    2) The task runs every 5 msec.  There are no other tasks.

    3) We assume "Add Memory Management" (in SYSBIOS Memory config) should not be checked.  We just want to use the standard heap (HeapStd).

    4) Should we see any reference to HeapStd in the .map file?

    5) What section name should be used for "BIOS.heapSection" in the SYSBIOS .cfg file?

    Thanks.

    -Jeff

  • Hi Chris, Jeff,

    I just realized that changing the C runtime library gate to GateSwi is not enough. SYS/BIOS provides its own implementation of malloc/free/realloc/calloc/memalign when using a heap manager other than HeapStd. Since you are only setting heapSize and not changing the BIOS.defaultHeapInstance, HeapMem (and not HeapStd) is going to be the default heap manager. HeapMem module uses GateMutex() as its gate provider by default which is bad when calling malloc from a swi. You can fix this by changing the HeapMem gate to GateSwi. Here's the code that you need to add to your cfg Script to do this:

    var HeapMem = xdc.useModule('ti.sysbios.heaps.HeapMem');
    var GateSwi = xdc.useModule('ti.sysbios.gates.GateSwi');
    HeapMem.common$.gate = GateSwi.create();

    With the above change I would expect the malloc() to work properly when called from within a swi function.

    Best,

    Ashish

  • Ashish-

    Thanks again for your reply, we will try this.  To clarify, is HeapMem required?  HeapStd doesn't support calling malloc() from an SWI?

    -Jeff

  • Thanks Ashish, that did the trick.

    Chris

  • Hi Jeff,

    Yes, I believe your application is using HeapMem as the heap manager. You could change the heap manager to HeapStd and make malloc() calls from Swi but I would recommend you to continue using HeapMem. We discourage using HeapStd and may even stop supporting HeapStd with SYS/BIOS in the future.

    Best,

    Ashish

  • Ashish-

    Ok got this.  We will stick with HeapMem from this point forward.  Thanks again.

    -Jeff
    Signalogic