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/FreeRTOSSupportForumArchive/November2013/freertosDirectusingpvPortMallocandvPortFree032bc6bfj.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 heap4.c dosent efficiently uses memory as pvPortMalloc() uses portBYTEALIGNMENT 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 portBYTEALIGNMENT why portBYTEALIGNMENT == 8 ? why it cant be 4 , 2 or 1?
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.
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.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.