FreeRTOS Support Archive
The FreeRTOS support forum is used to obtain active support directly from Real
Time Engineers Ltd. In return for using our top quality software and services for
free, we request you play fair and do your bit to help others too! Sign up
to receive notifications of new support topics then help where you can.
This is a read only archive of threads posted to the FreeRTOS support forum.
The archive is updated every week, so will not always contain the very latest posts.
Use these archive pages to search previous posts. Use the Live FreeRTOS Forum
link to reply to a post, or start a new support thread.
[FreeRTOS Home] [Live FreeRTOS Forum] [FAQ] [Archive Top] [March 2013 Threads]
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 );
vTaskSuspendAll();
{
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.
Regards.
Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.