chHeapAlloc always returns NULL

Discussions and support about ChibiOS/RT, the free embedded RTOS.
Reja
Posts: 2
Joined: Thu May 07, 2020 1:38 pm

chHeapAlloc always returns NULL

Postby Reja » Thu May 07, 2020 1:49 pm

Hello,

I've been trying to use ChibiOS for a while but something doesn't seem to work.

When I create a thread on the heap, chHeapAlloc is used. I use the default_heap by passing NULL through the MemoryHeap parameter, and as size I use 256. As my code is running, it won't enter the

Code: Select all

 while (qp->h.u.next != NULL)
It will enter

Code: Select all

if (heapp->h_provider)
As

Code: Select all

hp = heapp->h_provider(size + sizeof(union heap_header));
is called, hp will be NULL and NULL will be returned.

The default_heap has been initialized but the memcore hasn't, could this cause the problem? And if not, what else could?

Regards,

Reja

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: chHeapAlloc always returns NULL

Postby Giovanni » Thu May 07, 2020 3:11 pm

Hi,

Not sure, this code path is explored in the test suite and it works in all demos. Are you using a custom linker file? is the heap allocator calling the core allocator in order to get memory blocks? does the test suite run successfully?

Also specify version, platform, compiler and any other relevant information.

Giovanni

Reja
Posts: 2
Joined: Thu May 07, 2020 1:38 pm

Re: chHeapAlloc always returns NULL

Postby Reja » Fri May 08, 2020 8:28 am

Hello,

I've used the code from this project
https://gitlab.soft.vub.ac.be/yvdriess/ ... a9516ac819
It has kernel version 2.7.0 unstable (sounds reliable). My teammember used this version because it was easier to implement in our SDK. Later I got the task to make it CMSIS complient, so some code has been added to use the cmsis_os.c file from the latest version of chibiOS. The heap initialization and the heap alloc function are the same in both the old and new version, but of course something else could have been changed.

We are using it for a cortex-M33 with the Cross ARM C compiler.

I'm sorry if this makes the question is not fit for this forum.

Reja

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: chHeapAlloc always returns NULL

Postby Giovanni » Fri May 08, 2020 12:38 pm

Hi,

That code is 6 years old.

Just use the latest version, it already has CMSIS compatibility. You can use the M33 like an M4 with some limitations (no MPU, no TZ). Official M33 support is already in the unstable version and will be in next release.

Giovanni

avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Re: chHeapAlloc always returns NULL

Postby avrhack » Mon Sep 13, 2021 10:23 pm

Did you ever solve this? I've got the same problem - LWIP is failing in tcpip_init when it tries to allocate a semaphore, because chHeapAlloc is returning NULL. I'm running a shell which shows there is Core Mem of >13k available but no heap. Thoughts?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: chHeapAlloc always returns NULL

Postby Giovanni » Mon Sep 13, 2021 10:35 pm

avrhack wrote:Did you ever solve this? I've got the same problem - LWIP is failing in tcpip_init when it tries to allocate a semaphore, because chHeapAlloc is returning NULL. I'm running a shell which shows there is Core Mem of >13k available but no heap. Thoughts?


You need to give me specifics about your setup if you want a meaningful answer. The heap allocator is working on all demos.

Giovanni

avrhack
Posts: 140
Joined: Tue Dec 24, 2013 1:42 pm
Has thanked: 17 times
Been thanked: 3 times

Re: chHeapAlloc always returns NULL

Postby avrhack » Tue Sep 14, 2021 10:48 am

Giovanni wrote:You need to give me specifics about your setup if you want a meaningful answer. The heap allocator is working on all demos.
Giovanni


Oh I'm sure it's not a ChibiOS defect but something in my setup. The test that triggers the NULL return is in chmemcore.c:

Code: Select all

/* Considering also the case where there is numeric overflow.*/
if ((prev < ch_memcore.basemem) || (prev > ch_memcore.topmem)) {
    return NULL;
 }
 


In my case prev is 0x20004f60 and both ch_memcore.basemem and ch_memcore.topmem are 0x20004f78 so clearly that will always fail.

In the linker map __heap_base__ is 0x20004f78 and __heap_end__ is 0x2000500 so the linker is allocating very little to the heap, although it should be enough for this semaphore.

Looking at the various allocated memory, bss is 18,204 and data is 88 and this is an STM32F103CB so should have 20K of RAM ie 20,480 which implies over 2k should be left but I'm assuming the bss number already includes the linker allocation for the heap?

But that's where I get stuck - the implication from the docs is that it should allocate all of the available space to the heap but there is only 0x8c allocated so I'm guessing the issue is that there isn't enough RAM but can't figure out where to start looking for where it has gone and whether it's always going to show 'full' when using the default heap as that will soak up any spare?

User avatar
Giovanni
Site Admin
Posts: 14444
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 921 times
Contact:

Re: chHeapAlloc always returns NULL

Postby Giovanni » Tue Sep 14, 2021 12:28 pm

The map file should tell you what it taking RAM and where boundaries are. Note that you can also make a static heap, declare an array and build an heap in there.

Giovanni


Return to “ChibiOS/RT”

Who is online

Users browsing this forum: No registered users and 6 guests