Malloc not working in a task

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 
已解决

Malloc not working in a task

跳至解决方案
5,827 次查看
joebirch
Contributor III

I'm new to KDS, KSDK and PEx and am trying to get some basics working.

 

The problem is that malloc always returns 0 when called from a task.

malloc returns a valid address when called from main.

 

I am using KDS 3.0, KSDK 1.2 on a FRDM_K22, gcc

all defaults except:

  • OS is freeRTOS (with default memory sceme -   configFRTOS_MEMORY_SCHEME==3)
  • 1 task
  • fsl_debug_console
  • set heap to 0x0400

 

I appreciate the help

 

Joe

标签 (1)
标记 (2)
1 解答
2,976 次查看
BlackNight
NXP Employee
NXP Employee

Hi Joe,

I recommend that you have a look at this article: FreeRTOS, malloc() and SP check with GNU Tools | MCU on Eclipse

The problem you are facing is that the GNU malloc() routine has a safety check against the stack pointer.

It checks that the current SP is not inside the heap. However, for FreeRTOS tasks using malloc/memory scheme 3, this is the case :-(

What I do is to use my own _sbrk() routine (see above post). Just keep in mind that with the GNU ARM Embedded in KDS v3.0.0 the name has to be _sbrk() (and not sbrk() as in other versions of the library).

The other comment I have: a heap of 0x400 (1 KByte) sounds very small to me. Keep in mind that all the task stacks/semaphores/etc have to fit in there. That's enough for the IDLE task (assuming a default stack size of 200), but will likely fail for additional tasks. Or in other words: the heap size you allocate in FreeRTOS_Config.h will not taken into account for the heap size used by malloc.

I hope this helps,

Erich

在原帖中查看解决方案

4 回复数
2,977 次查看
BlackNight
NXP Employee
NXP Employee

Hi Joe,

I recommend that you have a look at this article: FreeRTOS, malloc() and SP check with GNU Tools | MCU on Eclipse

The problem you are facing is that the GNU malloc() routine has a safety check against the stack pointer.

It checks that the current SP is not inside the heap. However, for FreeRTOS tasks using malloc/memory scheme 3, this is the case :-(

What I do is to use my own _sbrk() routine (see above post). Just keep in mind that with the GNU ARM Embedded in KDS v3.0.0 the name has to be _sbrk() (and not sbrk() as in other versions of the library).

The other comment I have: a heap of 0x400 (1 KByte) sounds very small to me. Keep in mind that all the task stacks/semaphores/etc have to fit in there. That's enough for the IDLE task (assuming a default stack size of 200), but will likely fail for additional tasks. Or in other words: the heap size you allocate in FreeRTOS_Config.h will not taken into account for the heap size used by malloc.

I hope this helps,

Erich

2,976 次查看
joebirch
Contributor III

Sergei and Erich

Thanks for the quick reply.I switched to freeRtos memory scheme 1 (alloc but no free) and am up and running.

I realize a heap of 1k is too small but I was trying to keep the example as simple as possible before posting.

Joe

0 项奖励
2,976 次查看
sergeisharonov
Contributor III

Don't know about KDS 3.0, KSDK 1.2 but in previous versions malloc was a mess.

How is your malloc implemented? How does it interact with FreeRTOS memory allocation?

How is your malloc protected from re-entrancy in multitasking environment?

From my old notes to myself:

"Newlib has its own malloc/free and FreeRTOS has its own equivalent routines. To use newlib malloc in multitasking environment we are supposed to provide locking functions __malloc_lock/unclock. Unfortunately these are never called by newlib-nano. We will junk newlib malloc and friends and override them with wrapped FreeRTOS routines. Note: newlib will go and do malloc/free behind your back, so we cannot just use FreeRTOS routines in our code. Implementation: use heap_4.c from portable dir to get FreeRTOS memory allocator, update it to stick its heap array into second RAM page of K22 Kinetis via attribute, update linker script not to waste any RAM for newlib heap."

Hope this helps.

Regards,

Sergei

P.S. I do not use PE or included KSDK port of FreeRTOS.

0 项奖励
2,976 次查看
BlackNight
NXP Employee
NXP Employee

Hi Sergei,

all what FreeRTOS does is to provide a wrapper around malloc (see heap_3.c, pvPortMalloc()).

It does not affect any malloc() done by something else (e.g. by your own library, other libraries, etc). Only if calls go through the RTOS (or pvPortMalloc()), things are protected.

I prefer to use the normal FreeRTOS heap mechanism, and not using malloc() at all. But you are correct: if newlib does its own thing, there is no way to intercept that, except you move in your own malloc() and free().

I hope this helps,

Erich

0 项奖励