Why my Heap is empty?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Why my Heap is empty?

Post by noether »

Hello,

I am integrating the DSP_LIB from ARM with ChibiOS, and I have a strange behavior (well, probably it is normal but I can not explain it).

I have the next main (is a ripped version of the demo for the STM32F4 Discovery).

Code: Select all

#include <math.h>
#include "ch.h"
#include "hal.h"
#include "./math/arm_math.h"
#include "./math/matrix.h"

static MemoryHeap *matricesHeap;
static float matricesBuff[4*1024*sizeof(float)];

static WORKING_AREA(waThread1, 128);
static msg_t Thread1(void *arg) {

  (void)arg;
  chRegSetThreadName("blinker");
  while (TRUE) {
    palSetPad(GPIOD, GPIOD_LED3);       /* Orange.  */
    chThdSleepMilliseconds(500);
    palClearPad(GPIOD, GPIOD_LED3);     /* Orange.  */
    chThdSleepMilliseconds(500);
  }
}

int main(void) {

    halInit();
    chSysInit();

    chThdCreateStatic(waThread1, sizeof(waThread1), NORMALPRIO, Thread1, NULL);

    float x = 0.3f;
    uint8_t led = 0;
    systime_t t1tick;
    systime_t t1lapse;

    chHeapInit(matricesHeap, matricesBuff, 4*1024*sizeof(float));
    arm_matrix_instance_f32 A, B, C;
    matrixInit(&A, 2, 2, matricesHeap);
    matrixInit(&B, 2, 2, matricesHeap);
    matrixInit(&C, 2, 2, matricesHeap);

    A.pData[0] = 1.1f;
    A.pData[1] = 2.1f;
    A.pData[2] = 4.1f;
    A.pData[3] = 5.1f;

    B.pData[0] = 3.1f;
    B.pData[1] = 4.1f;
    B.pData[2] = 5.1f;
    B.pData[3] = 7.1f;


    while (TRUE) {
        arm_status status;

        t1tick = chTimeNow();
        x = x + sinf(x);
        status = arm_mat_add_f32(&A, &B, &C);
        arm_copy_f32(C.pData, A.pData, A.numRows*A.numCols);

        led = !led;
        if(led){
            palSetPad(GPIOD, GPIOD_LED4);       /* Green.  */
        }
        else{
            palClearPad(GPIOD, GPIOD_LED4);     /* Green.  */
        }

        t1lapse = GetTimeInterval(&t1tick);

        if(t1lapse < MS2ST(500))
            chThdSleepUntil(t1tick + MS2ST(500));
        else
            palSetPad(GPIOD, GPIOD_LED5);
    }
}


The code is working as expected, just doing A+B = C, A = C, each iteration.
But, when I look at the Heap (matricesBuff) with gdb is just empty:

    (gdb) p matricesBuff
    $89 = {0, 2.29476637e-41, 0 <repeats 16382 times>}

Here it is the initialization code for the matrices

Code: Select all

void matrixInit(arm_matrix_instance_f32 *m, int rows, int cols, 
        MemoryHeap *memoryHeap) {
    float32_t *data;
    data = (float32_t *)chHeapAlloc(memoryHeap,
            rows*cols*sizeof(float32_t));
    arm_mat_init_f32(m, rows, cols, data);
}


The arm_mat_init just pass the data to the structure arm_matrix_instance_f32.

Probably I am missing how the management of the Heap works in ChibiOS, but... should matricesBuff not be populated with the values of A, B and C?

By the way, these are the pointers given to A, B, and C:
    (gdb) p/u A.pData
    $91 = 536939968
    (gdb) p/u B.pData
    $92 = 536939992
    (gdb) p/u C.pData
    $93 = 536940016


Why the distance between each other is 24 and not 16 (corresponding to 4 floats)?

Thanks in advance for your help!
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Why my Heap is empty?

Post by Giovanni »

Hi,

You are passing to chHeapInit() a MemoryHeap* pointing to nothing not a pointer to a statically allocated MemoryHeap, you would write chHeapInit(&matricesHeap, matricesBuff, 4*1024*sizeof(float)) in that case.

Giovanni
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Re: Why my Heap is empty?

Post by noether »

Giovanni wrote:Hi,

You are passing to chHeapInit() a MemoryHeap* pointing to nothing not a pointer to a statically allocated MemoryHeap, you would write chHeapInit(&matricesHeap, matricesBuff, 4*1024*sizeof(float)) in that case.

Giovanni


Thank you very much Giovanni, it was really a quick response!
In the first code I have defined: static MemoryHeap *matricesHeap;
this is why I have passed as argument to the chHeapInit without & .

I am not a experienced programmer, so many thanks for catching the mistake.

Now I can see in the Heap the values of the A, B and C matrices, but I still seeing "two holes" among them.

    (gdb) p matricesBuff
    $107 = {1.08459922e-19, 2.24207754e-44, 1.10000002, 2.0999999, 4.0999999,
    5.0999999, 1.08459922e-19, 2.24207754e-44, 3.0999999, 4.0999999, 5.0999999,
    7.0999999, 1.08459922e-19, 2.24207754e-44, 0, 0, 0, 0, 0, 2.28467702e-41,
    0 <repeats 16364 times>}

Why is it not well aligned?

Thanks again in advance.
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Why my Heap is empty?

Post by Giovanni »

The allocator maintains its own data into the 8 bytes before the allocated block, the real used memory is size+8.

Do not assume that allocated blocks are contiguous and do not assume that all the space reserved for the heap is entirely available.

Giovanni
noether
Posts: 91
Joined: Wed Nov 16, 2011 3:18 pm

Re: Why my Heap is empty?

Post by noether »

Giovanni wrote:The allocator maintains its own data into the 8 bytes before the allocated block, the real used memory is size+8.

Do not assume that allocated blocks are contiguous and do not assume that all the space reserved for the heap is entirely available.

Giovanni


Thank you very much for the explanation. I will be conservative for my estimations then.
Post Reply