Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Using pvPortMalloc() in application

Posted by gauravpatni on January 1, 2018


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?

Regards, Gaurav

Using pvPortMalloc() in application

Posted by richard_damon on January 1, 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.

Using pvPortMalloc() in application

Posted by rtel on January 1, 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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.


"OTA Update Security and Reliability" webinar presented by TI and AWS. Register here.


FreeRTOS and other embedded software careers at AWS.

Latest News

FreeRTOS kernel V10.0.1 is available for immediate download. Now MIT licensed.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner