]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/MemMang/heap_4.c
Update version number to 8.1.2 after moving the defaulting of configUSE_PORT_OPTIMISE...
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
index 4ed42b8de2d2872ef180b7eff77cd823a808a956..ee282c8c0ea2a6abc40a3f74caada4aa013bf0d6 100644 (file)
@@ -1,5 +1,5 @@
 /*\r
-    FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd. \r
+    FreeRTOS V8.1.2 - Copyright (C) 2014 Real Time Engineers Ltd.\r
     All rights reserved\r
 \r
     VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
     the terms of the GNU General Public License (version 2) as published by the\r
     Free Software Foundation >>!AND MODIFIED BY!<< the FreeRTOS exception.\r
 \r
-    >>! NOTE: The modification to the GPL is included to allow you to distribute\r
-    >>! a combined work that includes FreeRTOS without being obliged to provide\r
-    >>! the source code for proprietary components outside of the FreeRTOS\r
-    >>! kernel.\r
+    >>!   NOTE: The modification to the GPL is included to allow you to     !<<\r
+    >>!   distribute a combined work that includes FreeRTOS without being   !<<\r
+    >>!   obliged to provide the source code for proprietary components     !<<\r
+    >>!   outside of the FreeRTOS kernel.                                   !<<\r
 \r
     FreeRTOS is distributed in the hope that it will be useful, but WITHOUT ANY\r
     WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS\r
 */\r
 \r
 /*\r
- * A sample implementation of pvPortMalloc() and vPortFree() that combines \r
- * (coalescences) adjacent memory blocks as they are freed, and in so doing \r
+ * A sample implementation of pvPortMalloc() and vPortFree() that combines\r
+ * (coalescences) adjacent memory blocks as they are freed, and in so doing\r
  * limits memory fragmentation.\r
  *\r
- * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the \r
+ * See heap_1.c, heap_2.c and heap_3.c for alternative implementations, and the\r
  * memory management pages of http://www.FreeRTOS.org for more information.\r
  */\r
 #include <stdlib.h>\r
@@ -84,16 +84,13 @@ task.h is included from an application file. */
 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
 \r
 /* Block sizes must not get too small. */\r
-#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )\r
+#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( xHeapStructSize * 2 ) )\r
 \r
 /* Assumes 8bit bytes! */\r
 #define heapBITS_PER_BYTE              ( ( size_t ) 8 )\r
 \r
-/* A few bytes might be lost to byte aligning the heap start address. */\r
-#define heapADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
-\r
 /* Allocate the memory for the heap. */\r
-static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
+static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
 \r
 /* Define the linked list structure.  This is used to link free blocks in order\r
 of their memory address. */\r
@@ -101,17 +98,17 @@ typedef struct A_BLOCK_LINK
 {\r
        struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
        size_t xBlockSize;                                              /*<< The size of the free block. */\r
-} xBlockLink;\r
+} BlockLink_t;\r
 \r
 /*-----------------------------------------------------------*/\r
 \r
 /*\r
- * Inserts a block of memory that is being freed into the correct position in \r
+ * Inserts a block of memory that is being freed into the correct position in\r
  * the list of free memory blocks.  The block being freed will be merged with\r
  * the block in front it and/or the block behind it if the memory blocks are\r
  * adjacent to each other.\r
  */\r
-static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert );\r
+static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert );\r
 \r
 /*\r
  * Called automatically to setup the required heap structures the first time\r
@@ -123,21 +120,18 @@ static void prvHeapInit( void );
 \r
 /* The size of the structure placed at the beginning of each allocated memory\r
 block must by correctly byte aligned. */\r
-static const unsigned short heapSTRUCT_SIZE    = ( ( sizeof ( xBlockLink ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
-\r
-/* Ensure the pxEnd pointer will end up on the correct byte alignment. */\r
-static const size_t xTotalHeapSize = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
+static const size_t xHeapStructSize    = ( ( sizeof( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );\r
 \r
 /* Create a couple of list links to mark the start and end of the list. */\r
-static xBlockLink xStart, *pxEnd = NULL;\r
+static BlockLink_t xStart, *pxEnd = NULL;\r
 \r
 /* Keeps track of the number of free bytes remaining, but says nothing about\r
 fragmentation. */\r
-static size_t xFreeBytesRemaining = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
-static size_t xMinimumEverFreeBytesRemaining = ( ( size_t ) heapADJUSTED_HEAP_SIZE ) & ( ( size_t ) ~portBYTE_ALIGNMENT_MASK );\r
+static size_t xFreeBytesRemaining = 0U;\r
+static size_t xMinimumEverFreeBytesRemaining = 0U;\r
 \r
-/* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize \r
-member of an xBlockLink structure is set then the block belongs to the \r
+/* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
+member of an BlockLink_t structure is set then the block belongs to the\r
 application.  When the bit is free the block is still part of the free heap\r
 space. */\r
 static size_t xBlockAllocatedBit = 0;\r
@@ -146,7 +140,7 @@ static size_t xBlockAllocatedBit = 0;
 \r
 void *pvPortMalloc( size_t xWantedSize )\r
 {\r
-xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
+BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
 void *pvReturn = NULL;\r
 \r
        vTaskSuspendAll();\r
@@ -157,31 +151,44 @@ void *pvReturn = NULL;
                {\r
                        prvHeapInit();\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
 \r
                /* Check the requested block size is not so large that the top bit is\r
-               set.  The top bit of the block size member of the xBlockLink structure \r
+               set.  The top bit of the block size member of the BlockLink_t structure\r
                is used to determine who owns the block - the application or the\r
                kernel, so it must be free. */\r
                if( ( xWantedSize & xBlockAllocatedBit ) == 0 )\r
                {\r
-                       /* The wanted size is increased so it can contain a xBlockLink\r
+                       /* The wanted size is increased so it can contain a BlockLink_t\r
                        structure in addition to the requested amount of bytes. */\r
                        if( xWantedSize > 0 )\r
                        {\r
-                               xWantedSize += heapSTRUCT_SIZE;\r
+                               xWantedSize += xHeapStructSize;\r
 \r
-                               /* Ensure that blocks are always aligned to the required number \r
+                               /* Ensure that blocks are always aligned to the required number\r
                                of bytes. */\r
                                if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0x00 )\r
                                {\r
                                        /* Byte alignment required. */\r
                                        xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
+                                       configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );\r
+                               }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
                                }\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
 \r
                        if( ( xWantedSize > 0 ) && ( xWantedSize <= xFreeBytesRemaining ) )\r
                        {\r
-                               /* Traverse the list from the start     (lowest address) block until \r
+                               /* Traverse the list from the start     (lowest address) block until\r
                                one     of adequate size is found. */\r
                                pxPreviousBlock = &xStart;\r
                                pxBlock = xStart.pxNextFreeBlock;\r
@@ -191,29 +198,30 @@ void *pvReturn = NULL;
                                        pxBlock = pxBlock->pxNextFreeBlock;\r
                                }\r
 \r
-                               /* If the end marker was reached then a block of adequate size \r
+                               /* If the end marker was reached then a block of adequate size\r
                                was     not found. */\r
                                if( pxBlock != pxEnd )\r
                                {\r
-                                       /* Return the memory space pointed to - jumping over the \r
-                                       xBlockLink structure at its start. */\r
-                                       pvReturn = ( void * ) ( ( ( unsigned char * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );\r
+                                       /* Return the memory space pointed to - jumping over the\r
+                                       BlockLink_t structure at its start. */\r
+                                       pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + xHeapStructSize );\r
 \r
-                                       /* This block is being returned for use so must be taken out \r
+                                       /* This block is being returned for use so must be taken out\r
                                        of the list of free blocks. */\r
                                        pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;\r
 \r
-                                       /* If the block is larger than required it can be split into \r
+                                       /* If the block is larger than required it can be split into\r
                                        two. */\r
                                        if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )\r
                                        {\r
-                                               /* This block is to be split into two.  Create a new \r
-                                               block following the number of bytes requested. The void \r
-                                               cast is used to prevent byte alignment warnings from the \r
+                                               /* This block is to be split into two.  Create a new\r
+                                               block following the number of bytes requested. The void\r
+                                               cast is used to prevent byte alignment warnings from the\r
                                                compiler. */\r
-                                               pxNewBlockLink = ( void * ) ( ( ( unsigned char * ) pxBlock ) + xWantedSize );\r
+                                               pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
+                                               configASSERT( ( ( ( uint32_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
 \r
-                                               /* Calculate the sizes of two blocks split from the \r
+                                               /* Calculate the sizes of two blocks split from the\r
                                                single block. */\r
                                                pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
                                                pxBlock->xBlockSize = xWantedSize;\r
@@ -221,6 +229,10 @@ void *pvReturn = NULL;
                                                /* Insert the new block into the list of free blocks. */\r
                                                prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
 \r
                                        xFreeBytesRemaining -= pxBlock->xBlockSize;\r
 \r
@@ -228,18 +240,34 @@ void *pvReturn = NULL;
                                        {\r
                                                xMinimumEverFreeBytesRemaining = xFreeBytesRemaining;\r
                                        }\r
+                                       else\r
+                                       {\r
+                                               mtCOVERAGE_TEST_MARKER();\r
+                                       }\r
 \r
                                        /* The block is being returned - it is allocated and owned\r
                                        by the application and has no "next" block. */\r
                                        pxBlock->xBlockSize |= xBlockAllocatedBit;\r
                                        pxBlock->pxNextFreeBlock = NULL;\r
                                }\r
+                               else\r
+                               {\r
+                                       mtCOVERAGE_TEST_MARKER();\r
+                               }\r
                        }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
+                       }\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
                }\r
 \r
                traceMALLOC( pvReturn, xWantedSize );\r
        }\r
-       xTaskResumeAll();\r
+       ( void ) xTaskResumeAll();\r
 \r
        #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
        {\r
@@ -248,23 +276,28 @@ void *pvReturn = NULL;
                        extern void vApplicationMallocFailedHook( void );\r
                        vApplicationMallocFailedHook();\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
        }\r
        #endif\r
 \r
+       configASSERT( ( ( ( uint32_t ) pvReturn ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
        return pvReturn;\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
 void vPortFree( void *pv )\r
 {\r
-unsigned char *puc = ( unsigned char * ) pv;\r
-xBlockLink *pxLink;\r
+uint8_t *puc = ( uint8_t * ) pv;\r
+BlockLink_t *pxLink;\r
 \r
        if( pv != NULL )\r
        {\r
-               /* The memory being freed will have an xBlockLink structure immediately\r
+               /* The memory being freed will have an BlockLink_t structure immediately\r
                before it. */\r
-               puc -= heapSTRUCT_SIZE;\r
+               puc -= xHeapStructSize;\r
 \r
                /* This casting is to keep the compiler from issuing warnings. */\r
                pxLink = ( void * ) puc;\r
@@ -272,7 +305,7 @@ xBlockLink *pxLink;
                /* Check the block is actually allocated. */\r
                configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
                configASSERT( pxLink->pxNextFreeBlock == NULL );\r
-               \r
+\r
                if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
                {\r
                        if( pxLink->pxNextFreeBlock == NULL )\r
@@ -285,12 +318,20 @@ xBlockLink *pxLink;
                                {\r
                                        /* Add this block to the list of free blocks. */\r
                                        xFreeBytesRemaining += pxLink->xBlockSize;\r
-                                       prvInsertBlockIntoFreeList( ( ( xBlockLink * ) pxLink ) );\r
                                        traceFREE( pv, pxLink->xBlockSize );\r
+                                       prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
                                }\r
-                               xTaskResumeAll();\r
+                               ( void ) xTaskResumeAll();\r
+                       }\r
+                       else\r
+                       {\r
+                               mtCOVERAGE_TEST_MARKER();\r
                        }\r
                }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
        }\r
 }\r
 /*-----------------------------------------------------------*/\r
@@ -315,11 +356,22 @@ void vPortInitialiseBlocks( void )
 \r
 static void prvHeapInit( void )\r
 {\r
-xBlockLink *pxFirstFreeBlock;\r
-unsigned char *pucHeapEnd, *pucAlignedHeap;\r
+BlockLink_t *pxFirstFreeBlock;\r
+uint8_t *pucAlignedHeap;\r
+uint32_t ulAddress;\r
+size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;\r
 \r
        /* Ensure the heap starts on a correctly aligned boundary. */\r
-       pucAlignedHeap = ( unsigned char * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ( portPOINTER_SIZE_TYPE ) ~portBYTE_ALIGNMENT_MASK ) );\r
+       ulAddress = ( uint32_t ) ucHeap;\r
+\r
+       if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
+       {\r
+               ulAddress += ( portBYTE_ALIGNMENT - 1 );\r
+               ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+               xTotalHeapSize -= ulAddress - ( uint32_t ) ucHeap;\r
+       }\r
+\r
+       pucAlignedHeap = ( uint8_t * ) ulAddress;\r
 \r
        /* xStart is used to hold a pointer to the first item in the list of free\r
        blocks.  The void cast is used to prevent compiler warnings. */\r
@@ -328,31 +380,32 @@ unsigned char *pucHeapEnd, *pucAlignedHeap;
 \r
        /* pxEnd is used to mark the end of the list of free blocks and is inserted\r
        at the end of the heap space. */\r
-       pucHeapEnd = pucAlignedHeap + xTotalHeapSize;\r
-       pucHeapEnd -= heapSTRUCT_SIZE;\r
-       pxEnd = ( void * ) pucHeapEnd;\r
-       configASSERT( ( ( ( unsigned long ) pxEnd ) & ( ( unsigned long ) portBYTE_ALIGNMENT_MASK ) ) == 0UL );\r
+       ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalHeapSize;\r
+       ulAddress -= xHeapStructSize;\r
+       ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+       pxEnd = ( void * ) ulAddress;\r
        pxEnd->xBlockSize = 0;\r
        pxEnd->pxNextFreeBlock = NULL;\r
 \r
        /* To start with there is a single free block that is sized to take up the\r
        entire heap space, minus the space taken by pxEnd. */\r
        pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
-       pxFirstFreeBlock->xBlockSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
+       pxFirstFreeBlock->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlock;\r
        pxFirstFreeBlock->pxNextFreeBlock = pxEnd;\r
 \r
-       /* The heap now contains pxEnd. */\r
-       xFreeBytesRemaining -= heapSTRUCT_SIZE;\r
+       /* Only one block exists - and it covers the entire usable heap space. */\r
+       xMinimumEverFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
+       xFreeBytesRemaining = pxFirstFreeBlock->xBlockSize;\r
 \r
        /* Work out the position of the top bit in a size_t variable. */\r
        xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-static void prvInsertBlockIntoFreeList( xBlockLink *pxBlockToInsert )\r
+static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
 {\r
-xBlockLink *pxIterator;\r
-unsigned char *puc;\r
+BlockLink_t *pxIterator;\r
+uint8_t *puc;\r
 \r
        /* Iterate through the list until a block is found that has a higher address\r
        than the block being inserted. */\r
@@ -362,18 +415,22 @@ unsigned char *puc;
        }\r
 \r
        /* Do the block being inserted, and the block it is being inserted after\r
-       make a contiguous block of memory? */   \r
-       puc = ( unsigned char * ) pxIterator;\r
-       if( ( puc + pxIterator->xBlockSize ) == ( unsigned char * ) pxBlockToInsert )\r
+       make a contiguous block of memory? */\r
+       puc = ( uint8_t * ) pxIterator;\r
+       if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
        {\r
                pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
                pxBlockToInsert = pxIterator;\r
        }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
 \r
        /* Do the block being inserted, and the block it is being inserted before\r
        make a contiguous block of memory? */\r
-       puc = ( unsigned char * ) pxBlockToInsert;\r
-       if( ( puc + pxBlockToInsert->xBlockSize ) == ( unsigned char * ) pxIterator->pxNextFreeBlock )\r
+       puc = ( uint8_t * ) pxBlockToInsert;\r
+       if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
        {\r
                if( pxIterator->pxNextFreeBlock != pxEnd )\r
                {\r
@@ -388,7 +445,7 @@ unsigned char *puc;
        }\r
        else\r
        {\r
-               pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;         \r
+               pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
        }\r
 \r
        /* If the block being inserted plugged a gab, so was merged with the block\r
@@ -399,5 +456,9 @@ unsigned char *puc;
        {\r
                pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
        }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
 }\r
 \r