Is it possible to use malloc inside MQX / MQX Lite task?
I tried it but malloc returns NULL pointer all the time.
Hello,
I have faced the same problem using MQX Lite with Processor Expert (without SDK). Malloc() worked outside MQX Lite tasks, but not in them. A possible solution is to use MQX Lite's own memory allocation features (lwmem), which are used very similarly to standard malloc() and free(). To use lwmem, do the following:
Martin,
I got the same problems of you with MQX Lite + KSDK + PEx. But I finally found how to do it. To make it work, you need to edit lite_config.h header file locate at "/SDK/rtos/mqx/config/common/lite_config.h" in your KDS project explorer. Open it.
Near line 63 you should have this :
#ifndef MQXCFG_ALLOCATOR
#define MQXCFG_ALLOCATOR MQX_ALLOCATOR_NONE
#endif
Now change define by :
#ifndef MQXCFG_ALLOCATOR
#define MQXCFG_ALLOCATOR MQX_ALLOCATOR_LWMEM
#endif
Now when you allocate memory by using OSA_MemAlloc()method from fsl_os_abstraction, your pointer should return a 32 bits address.
I did some test with 2x8bytes memory allocation. The address show below correspond to my two address pointers after OSA_MemAlloc()
Even OSA_FreeMem()work also very well.
The only thing that I don't understand yet is why an allocation of 8 bytes have the same address gap than 16 bytes ? See below :
Even worst, if allocate just 1 byte, the gap between two allocations will be 16 address ... Does the MCU will lost these memory space ?
Hi Martin:
Freescale MQX uses its own memory allocation system, which doesn't rely on heap.
The default MQX memory pool, where MQX kernel allocates system memory, is between
__KERNEL_DATA_START and __KERNEL_DATA_END (symbols generated during bsp linkage).
It allocates memory using function _mem_alloc_system().
On top of this, in bsp we define malloc() to call _mem_alloc_system().
If you want the MQX kernel to call Thumb library malloc, then you have to define
_mem_alloc_system* in preprocessor to be malloc #define _mem_alloc_system malloc and so on.
and then disable bsp "duplicate" definition of malloc (either by link order or be removing the duplcate definition from the sources).
If you want to use Thumb library allocation system, you can, but it is your responsibility
to give the CodeWarrior necesarry linker generated files and so on, whatever the Thumb library allocation system relies on.
You loose TAD, kernel logging and other MQX features related to memory allocation.
Have a nice day!
Regards
Daniel
I don't mind what kind of malloc I use. My problem is that calling malloc in mqx lite task returns NULL pointer all the time. I use Processor Expert and Kinetis Design Studio.
In CPU component / Build options / Generate linker file I set heap size. Then I use malloc in main function of my project. It works. But when I use malloc in my mqx lite task (there is only one in the project) the malloc always returns NULL pointer. Why and how to fix it?
Hi Martin,
I never used malloc() into a MQX (lite) task but maybe you should use mem_alloc() instead of the standard malloc() function.
Best regard.
Max.