Page 1 of 2

chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 9:05 am
by JoVM
@previous post:I gave up on the libgc alloc functions because it's returning NULL pointers.

But I notice chibiOS is providing a heap alloc function I want to try.

Following code blocks my core:

Code: Select all

typedef struct
{
   int data;
}Element;

typedef struct
{
   int current;
   int size;
   int increment_rate;
   Element* elements;
}DeviceList;

void initWithSizeAndIncRate(DeviceList* const list, int size, int rate)
{
   list->size = size;
   list->increment_rate = rate;
   list->elements = (Element*) chHeapAlloc( NULL, list->size * sizeof(Element));
   list->current = -1;
}


I just want to allocate 16 blocks of size Element on the heap.

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 9:07 am
by Giovanni
Define "block", an assertion? a crash? and endless loop somewhere?

Giovanni

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 9:28 am
by JoVM
@Giovanni: It wan't return from this code:

Code: Select all

H_UNLOCK(heapp);


after the while loop in chHeapAlloc().

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 9:38 am
by Giovanni
Could you provide a stack trace after stopping execution?

Please make sure to enable checks, assertions, stack check and state checker in chconf.h.

Giovanni

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 9:46 am
by JoVM
Do I need to use chHeapObjectInit before allocation?




@ps: I'm not using gdb now. Only SWV ITM trace. I need to look if I can make a trace in st utility.

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 11:22 am
by Giovanni
No need to init because you are using the default heap (NULL as first paramenter).

Giovanni

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 11:32 am
by JoVM
My debug output:

Code: Select all

$ arm-none-eabi-gdb ch.elf
GNU gdb (GNU Tools for ARM Embedded Processors) 7.8.1.20141128-cvs
Copyright (C) 2014 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "--host=i686-w64-mingw32 --target=arm-none-eabi".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ch.elf...done.
(gdb) target extended localhost:4242
Remote debugging using localhost:4242
0x08001148 in _heap_init () at ../../../os/rt/src/chheap.c:95
95        chMtxObjectInit(&default_heap.h_mtx);
(gdb) backtrace
#0  0x08001148 in _heap_init () at ../../../os/rt/src/chheap.c:95
#1  0xffffffff in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
(gdb)

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 12:34 pm
by JoVM
@Giovanni: discard the above.

This is the stepping through the program:

Code: Select all

Breakpoint 1, chMtxUnlock (mp=mp@entry=0x200011a8 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:319
319       thread_t *ctp = currp;
(gdb) next
318     void chMtxUnlock(mutex_t *mp) {
(gdb) next
319       thread_t *ctp = currp;
(gdb) step
324       chSysLock();
(gdb) step
chSysLock () at ../../../os/rt/src/chmtx.c:324
324       chSysLock();
(gdb) step
port_lock () at ../../../os/rt/src/chmtx.c:324
324       chSysLock();
(gdb) step
__set_BASEPRI (value=32) at ../../../os/ext/CMSIS/include/core_cmFunc.h:546
546       __ASM volatile ("MSR basepri, %0" : : "r" (value) : "memory");
(gdb) step
chMtxUnlock (mp=mp@entry=0x200011a8 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:342
342         if (chMtxQueueNotEmptyS(mp)) {
(gdb) step
chMtxQueueNotEmptyS (mp=<optimized out>) at ../../../os/rt/src/chmtx.c:342
342         if (chMtxQueueNotEmptyS(mp)) {
(gdb) step
queue_notempty (tqp=<optimized out>) at ../../../os/rt/include/chschd.h:600
600       return (bool)(tqp->p_next != (thread_t *)tqp);
(gdb) step
chMtxUnlock (mp=mp@entry=0x200011a8 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:339
339         ctp->p_mtxlist = mp->m_next;
(gdb) step
342         if (chMtxQueueNotEmptyS(mp)) {
(gdb) step
347           tprio_t newprio = ctp->p_realprio;
(gdb) step
349           while (lmp != NULL) {
(gdb) step
353             if (chMtxQueueNotEmptyS(lmp) &&
(gdb) step
chMtxQueueNotEmptyS (mp=0x80004d1 <_unhandled_exception>)
    at ../../../os/rt/src/chmtx.c:353
353             if (chMtxQueueNotEmptyS(lmp) &&
(gdb) step
queue_notempty (tqp=0x80004d1 <_unhandled_exception>)
    at ../../../os/rt/include/chschd.h:600
600       return (bool)(tqp->p_next != (thread_t *)tqp);
(gdb) step
chMtxUnlock (mp=mp@entry=0x200011a8 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:353
353             if (chMtxQueueNotEmptyS(lmp) &&
(gdb) step
354                 (lmp->m_queue.p_next->p_prio > newprio)) {
(gdb) step
_unhandled_exception ()
    at ../../../os/common/ports/ARMCMx/compilers/GCC/vectors.c:79
79          ;
(gdb) step

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 12:37 pm
by Giovanni
Are the debug option that I mentioned before enabled?

Giovanni

Re: chibiOS 3 'chHeapAlloc' usage

Posted: Thu Apr 02, 2015 12:54 pm
by JoVM
Sorry forgot.

Code: Select all

(gdb) next
185       H_UNLOCK(heapp);
(gdb) step
chMtxUnlock (mp=mp@entry=0x20001630 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:319
319       thread_t *ctp = currp;
(gdb) step
322       chDbgCheck(mp != NULL);
(gdb) step
319       thread_t *ctp = currp;
(gdb) step
322       chDbgCheck(mp != NULL);
(gdb) step
324       chSysLock();
(gdb) step
chSysLock () at ../../../os/rt/src/chmtx.c:324
324       chSysLock();
(gdb) step
port_lock () at ../../../os/rt/src/chmtx.c:324
324       chSysLock();
(gdb) step
__set_BASEPRI (value=32) at ../../../os/ext/CMSIS/include/core_cmFunc.h:546
546       __ASM volatile ("MSR basepri, %0" : : "r" (value) : "memory");
(gdb) step
chSysLock () at ../../../os/rt/include/chsys.h:300
300       _stats_start_measure_crit_thd();
(gdb) step
_stats_start_measure_crit_thd () at ../../../os/rt/src/chstats.c:95
95        chTMStartMeasurementX(&ch.kernel_stats.m_crit_thd);
(gdb) step
chTMStartMeasurementX (tmp=0x200014f0 <ch+1200>)
    at ../../../os/rt/src/chtm.c:116
116       tmp->last = chSysGetRealtimeCounterX();
(gdb) step
port_rt_get_counter_value () at ../../../os/rt/ports/ARMCMx/chcore_v7m.h:547
547       return DWT->CYCCNT;
(gdb) step
chTMStartMeasurementX (tmp=0x200014f0 <ch+1200>)
    at ../../../os/rt/src/chtm.c:116
116       tmp->last = chSysGetRealtimeCounterX();
(gdb) step
chSysLock () at ../../../os/rt/include/chsys.h:301
301       _dbg_check_lock();
(gdb) step
_dbg_check_lock () at ../../../os/rt/src/chdebug.c:152
152       if ((ch.dbg.isr_cnt != 0) || (ch.dbg.lock_cnt != 0)) {
(gdb) step
155       _dbg_enter_lock();
(gdb) step
chMtxUnlock (mp=mp@entry=0x20001630 <default_heap+16>)
    at ../../../os/rt/src/chmtx.c:326
326       chDbgAssert(ctp->p_mtxlist != NULL, "owned mutexes list empty");
(gdb) step
327       chDbgAssert(ctp->p_mtxlist->m_owner == ctp, "ownership failure");
(gdb) step
chSysHalt (reason=reason@entry=0x80036f0 <__func__.6584> "chMtxUnlock")
    at ../../../os/rt/src/chsys.c:164
164       ch.dbg.panic_msg = reason;
(gdb) step
168       }
(gdb) step