Quality RTOS & Embedded Software

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


What do you think about this patch to heap?

Posted by Armandas on March 31, 2013
I've been reading through the memory management code, trying to understand how it works and made a couple of changes as I went along. These changes can be applied to heap_2.c and heap_4.c files.

The first part changes the way pvReturn is assigned a value. The current code is not broken, but it was not clear to me why you would use pxPreviousBlock->pxNextFreeBlock when pxBlock points to the same location.

The changes in the second part may actually be usefull, as they get rid of a couple unused variables.

Here's the diff. Let me know what you think.

--- heap_2.c
+++ heap_2v2.c
@@ -214,7 +214,7 @@
/* Return the memory space - jumping over the xBlockLink structure
at its start. */
-pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
+pvReturn = ( void * ) ( ( ( unsigned char * ) pxBlock ) + heapSTRUCT_SIZE );

/* This block is being returned for use so must be taken out of the
list of free blocks. */
@@ -237,7 +237,7 @@
prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );

-xFreeBytesRemaining -= pxBlock->xBlockSize;
+xFreeBytesRemaining -= xWantedSize;
@@ -259,17 +259,11 @@

void vPortFree( void *pv )
-unsigned char *puc = ( unsigned char * ) pv;
xBlockLink *pxLink;

if( pv != NULL )
-/* The memory being freed will have an xBlockLink structure immediately
-before it. */
-puc -= heapSTRUCT_SIZE;
-/* This casting is to keep the compiler from issuing warnings. */
-pxLink = ( void * ) puc;
+pxLink = ( void * ) ( ( ( unsigned char * ) pv ) - heapSTRUCT_SIZE );


RE: What do you think about this patch to heap?

Posted by Richard on April 1, 2013
Thank you for your feedback.

“-xFreeBytesRemaining -= pxBlock->xBlockSize;
+xFreeBytesRemaining -= xWantedSize;”

This is how the code was originally, but resulted in xFreeBytesRemaining being set correctly under certain circumstances - you would have to search the support forum archive to see exactly when, but according to the change history the change was made in FreeRTOS V6.1.0 (you can see how the code was before here.

“-unsigned char *puc = ( unsigned char * ) pv;”

The code uses that variable just for clarity. I think the variable would most likely be optimised away with compiler optimisation on anyway.


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

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

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

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