MQX malloc

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

MQX malloc

2,576 Views
martindusek
Contributor V

Is it possible to use malloc inside MQX / MQX Lite task?

I tried it but malloc returns NULL pointer all the time.

0 Kudos
Reply
5 Replies

1,697 Views
danielnagy
Contributor II

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:

  • In PEx, in the CPU component, go to Build options > Generate linker file and set an appropriate Heap size (e.g. 0x0200).
  • In the MQX Lite PEx component, under Configuration parameters, turn on Lightweight Memory Allocation.
  • Inside the MQX Lite tasks, use these functions for memory management:
    • pinter _lwmem_alloc(_mem_size requested_size) - to allocate memory,
    • _mqx_uint _lwmem_free(pointer mem_ptr) - to free memory.
0 Kudos
Reply

1,697 Views
mr_max
Contributor IV

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()

Capture d’écran 2016-01-02 à 00.03.37.png

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 :

Capture d’écran 2016-01-02 à 00.09.21.png

Even worst, if allocate just 1 byte, the gap between two allocations will be 16 address ... Does the MCU will lost these memory space ?

0 Kudos
Reply

1,697 Views
danielchen
NXP TechSupport
NXP TechSupport

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

1,697 Views
martindusek
Contributor V

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?

0 Kudos
Reply

1,697 Views
mr_max
Contributor IV

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.

0 Kudos
Reply