Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Real time embedded FreeRTOS mailing list 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem TCP & FAT Training


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 ]    [ Sitemap ]    [ ]

Copyright (C) 2004-2010 Richard Barry. Copyright (C) 2010-2016 Real Time Engineers Ltd.
Any and all data, files, source code, html content and documentation included in the FreeRTOSTM distribution or available on this site are the exclusive property of Real Time Engineers Ltd.. See the files license.txt (included in the distribution) and this copyright notice for more information. FreeRTOSTM and FreeRTOS.orgTM are trade marks of Real Time Engineers Ltd.

Latest News:

FreeRTOS V9.0.0 is now available for download.

Free TCP/IP and file system demos for the RTOS

Sponsored Links

⇓ Now With No Code Size Limit! ⇓
⇑ Free Download Without Registering ⇑

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Renesas Electronics Gold Alliance RTOS Partner.jpg

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers

Atmel RTOS partner supporting ARM Cortex-M3 and AVR32 microcontrollers

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

Xilinx Microblaze and Zynq partner

Silicon Labs low power RTOS partner

Altera RTOS partner for Nios II and Cortex-A9 SoC

Freescale Alliance RTOS Member supporting ARM and ColdFire microcontrollers

Infineon ARM Cortex-M microcontrollers

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

Cypress RTOS partner supporting ARM Cortex-M3

Fujitsu RTOS partner supporting ARM Cortex-M3 and FM3

Microsemi (previously Actel) RTOS partner supporting ARM Cortex-M3

Atollic Partner

IAR Partner

Keil ARM Partner

Embedded Artists