Using pvPortMalloc() in application

gauravpatni wrote on Monday, January 01, 2018:

Hi,

I want to use pvPortMalloc() in application code ( STM32L4 , cortex M4) to create temp large size ( 1k to 30K , MCU has 128K RAM ) buffer .
I reffered similar requirement post https://www.freertos.org/FreeRTOS_Support_Forum_Archive/November_2013/freertos_Direct_using_pvPortMalloc_and_vPortFree_032bc6bfj.html

from that i came to know that application can use pvPortMalloc() as it is thread safe & suggest to use heap_4.c.

  1. But my doubt is thet heap_4.c dosent efficiently uses memory as pvPortMalloc() uses portBYTE_ALIGNMENT for BlockLink_t struct & start of meory buffer . due to that every time few bytes are wated ( worst case is 7 per alignment ) .
    This may lead to unnecessary wating of RAM if no of pvPortMalloc() calls are more .

  2. also while portBYTE_ALIGNMENT why portBYTE_ALIGNMENT == 8 ? why it cant be 4 , 2 or 1?

Regards,
Gaurav

richard_damon wrote on Monday, January 01, 2018:

All dynamic memory allocations will have a similar possible loss of memory due to alignment issues. The value of 8 for the alignment is based on the rule that ARM processors have some data that needs to be aligned on 8 byte boundries, so dynamic memory allocations make sure that all allocations are on such a boundry because they can’t know if it is needed for this block or not.

Note, even if you statially allocate you memory, you will still run in to some memory loss from alignement, it can be less because the compiler know the needed alignment for each variable, but it still occurs.

rtel wrote on Monday, January 01, 2018:

To add to Richard D’s post - it is actually a requirement of the C
standard to return an aligned address and the alignment requirement
comes from the processor not the RTOS.