]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/MemMang/heap_4.c
Update version number in readiness for V10.3.0 release. Sync SVN with reviewed releas...
[freertos] / FreeRTOS / Source / portable / MemMang / heap_4.c
index 7a2f98ed9dd3af84ccecc193d312e2d6f680d8ef..911d4f9a597e5a1a36a3eaee211f0d2a16ba637b 100644 (file)
@@ -1,67 +1,29 @@
 /*\r
-    FreeRTOS V7.6.0 - Copyright (C) 2013 Real Time Engineers Ltd.\r
-    All rights reserved\r
-\r
-    VISIT http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    FreeRTOS provides completely free yet professionally developed,    *\r
-     *    robust, strictly quality controlled, supported, and cross          *\r
-     *    platform software that has become a de facto standard.             *\r
-     *                                                                       *\r
-     *    Help yourself get started quickly and support the FreeRTOS         *\r
-     *    project by purchasing a FreeRTOS tutorial book, reference          *\r
-     *    manual, or both from: http://www.FreeRTOS.org/Documentation        *\r
-     *                                                                       *\r
-     *    Thank you!                                                         *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    This file is part of the FreeRTOS distribution.\r
-\r
-    FreeRTOS is free software; you can redistribute it and/or modify it under\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
-\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
-    FOR A PARTICULAR PURPOSE.  Full license text is available from the following\r
-    link: http://www.freertos.org/a00114.html\r
-\r
-    1 tab == 4 spaces!\r
-\r
-    ***************************************************************************\r
-     *                                                                       *\r
-     *    Having a problem?  Start by reading the FAQ "My application does   *\r
-     *    not run, what could be wrong?"                                     *\r
-     *                                                                       *\r
-     *    http://www.FreeRTOS.org/FAQHelp.html                               *\r
-     *                                                                       *\r
-    ***************************************************************************\r
-\r
-    http://www.FreeRTOS.org - Documentation, books, training, latest versions,\r
-    license and Real Time Engineers Ltd. contact details.\r
-\r
-    http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
-    including FreeRTOS+Trace - an indispensable productivity tool, a DOS\r
-    compatible FAT file system, and our tiny thread aware UDP/IP stack.\r
-\r
-    http://www.OpenRTOS.com - Real Time Engineers ltd license FreeRTOS to High\r
-    Integrity Systems to sell under the OpenRTOS brand.  Low cost OpenRTOS\r
-    licenses offer ticketed support, indemnification and middleware.\r
-\r
-    http://www.SafeRTOS.com - High Integrity Systems also provide a safety\r
-    engineered and independently SIL3 certified version for use in safety and\r
-    mission critical applications that require provable dependability.\r
-\r
-    1 tab == 4 spaces!\r
-*/\r
+ * FreeRTOS Kernel V10.3.0\r
+ * Copyright (C) 2020 Amazon.com, Inc. or its affiliates.  All Rights Reserved.\r
+ *\r
+ * Permission is hereby granted, free of charge, to any person obtaining a copy of\r
+ * this software and associated documentation files (the "Software"), to deal in\r
+ * the Software without restriction, including without limitation the rights to\r
+ * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\r
+ * the Software, and to permit persons to whom the Software is furnished to do so,\r
+ * subject to the following conditions:\r
+ *\r
+ * The above copyright notice and this permission notice shall be included in all\r
+ * copies or substantial portions of the Software.\r
+ *\r
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\r
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\r
+ * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\r
+ * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\r
+ * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
+ * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\r
+ *\r
+ * http://www.FreeRTOS.org\r
+ * http://aws.amazon.com/freertos\r
+ *\r
+ * 1 tab == 4 spaces!\r
+ */\r
 \r
 /*\r
  * A sample implementation of pvPortMalloc() and vPortFree() that combines\r
@@ -83,17 +45,24 @@ task.h is included from an application file. */
 \r
 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
 \r
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )\r
+       #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0\r
+#endif\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 << 1 ) )\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
+#if( configAPPLICATION_ALLOCATED_HEAP == 1 )\r
+       /* The application writer has already defined the array used for the RTOS\r
+       heap - probably so it can be placed in a special segment or address. */\r
+       extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
+#else\r
+       static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
+#endif /* configAPPLICATION_ALLOCATED_HEAP */\r
 \r
 /* Define the linked list structure.  This is used to link free blocks in order\r
 of their memory address. */\r
@@ -101,7 +70,7 @@ 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
@@ -111,7 +80,7 @@ typedef struct A_BLOCK_LINK
  * 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 +92,20 @@ 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 ) + ( ( size_t ) ( portBYTE_ALIGNMENT - 1 ) ) ) & ~( ( size_t ) 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
+/* Keeps track of the number of calls to allocate and free memory as well as the\r
+number of free bytes remaining, but says nothing about fragmentation. */\r
+static size_t xFreeBytesRemaining = 0U;\r
+static size_t xMinimumEverFreeBytesRemaining = 0U;\r
+static size_t xNumberOfSuccessfulAllocations = 0;\r
+static size_t xNumberOfSuccessfulFrees = 0;\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
+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 +114,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
@@ -163,16 +131,16 @@ void *pvReturn = NULL;
                }\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
                                of bytes. */\r
@@ -180,6 +148,7 @@ void *pvReturn = NULL;
                                {\r
                                        /* Byte alignment required. */\r
                                        xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );\r
+                                       configASSERT( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) == 0 );\r
                                }\r
                                else\r
                                {\r
@@ -208,8 +177,8 @@ void *pvReturn = NULL;
                                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
+                                       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
                                        of the list of free blocks. */\r
@@ -223,7 +192,8 @@ void *pvReturn = NULL;
                                                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( ( ( ( size_t ) pxNewBlockLink ) & portBYTE_ALIGNMENT_MASK ) == 0 );\r
 \r
                                                /* Calculate the sizes of two blocks split from the\r
                                                single block. */\r
@@ -231,7 +201,7 @@ void *pvReturn = NULL;
                                                pxBlock->xBlockSize = xWantedSize;\r
 \r
                                                /* Insert the new block into the list of free blocks. */\r
-                                               prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
+                                               prvInsertBlockIntoFreeList( pxNewBlockLink );\r
                                        }\r
                                        else\r
                                        {\r
@@ -253,6 +223,7 @@ void *pvReturn = NULL;
                                        by the application and has no "next" block. */\r
                                        pxBlock->xBlockSize |= xBlockAllocatedBit;\r
                                        pxBlock->pxNextFreeBlock = NULL;\r
+                                       xNumberOfSuccessfulAllocations++;\r
                                }\r
                                else\r
                                {\r
@@ -271,7 +242,7 @@ void *pvReturn = NULL;
 \r
                traceMALLOC( pvReturn, xWantedSize );\r
        }\r
-       xTaskResumeAll();\r
+       ( void ) xTaskResumeAll();\r
 \r
        #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
        {\r
@@ -287,20 +258,21 @@ void *pvReturn = NULL;
        }\r
        #endif\r
 \r
+       configASSERT( ( ( ( size_t ) pvReturn ) & ( size_t ) 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
@@ -321,10 +293,11 @@ 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
+                                       xNumberOfSuccessfulFrees++;\r
                                }\r
-                               xTaskResumeAll();\r
+                               ( void ) xTaskResumeAll();\r
                        }\r
                        else\r
                        {\r
@@ -359,11 +332,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
+size_t uxAddress;\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
+       uxAddress = ( size_t ) ucHeap;\r
+\r
+       if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
+       {\r
+               uxAddress += ( portBYTE_ALIGNMENT - 1 );\r
+               uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
+               xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;\r
+       }\r
+\r
+       pucAlignedHeap = ( uint8_t * ) uxAddress;\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
@@ -372,31 +356,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
+       uxAddress = ( ( size_t ) pucAlignedHeap ) + xTotalHeapSize;\r
+       uxAddress -= xHeapStructSize;\r
+       uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );\r
+       pxEnd = ( void * ) uxAddress;\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 = uxAddress - ( size_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
@@ -407,8 +392,8 @@ unsigned char *puc;
 \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
+       puc = ( uint8_t * ) pxIterator;\r
+       if( ( puc + pxIterator->xBlockSize ) == ( uint8_t * ) pxBlockToInsert )\r
        {\r
                pxIterator->xBlockSize += pxBlockToInsert->xBlockSize;\r
                pxBlockToInsert = pxIterator;\r
@@ -420,8 +405,8 @@ unsigned char *puc;
 \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
@@ -452,4 +437,56 @@ unsigned char *puc;
                mtCOVERAGE_TEST_MARKER();\r
        }\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats )\r
+{\r
+BlockLink_t *pxBlock;\r
+size_t xBlocks = 0, xMaxSize = 0, xMinSize = portMAX_DELAY; /* portMAX_DELAY used as a portable way of getting the maximum value. */\r
+\r
+       vTaskSuspendAll();\r
+       {\r
+               pxBlock = xStart.pxNextFreeBlock;\r
+\r
+               /* pxBlock will be NULL if the heap has not been initialised.  The heap\r
+               is initialised automatically when the first allocation is made. */\r
+               if( pxBlock != NULL )\r
+               {\r
+                       do\r
+                       {\r
+                               /* Increment the number of blocks and record the largest block seen\r
+                               so far. */\r
+                               xBlocks++;\r
+\r
+                               if( pxBlock->xBlockSize > xMaxSize )\r
+                               {\r
+                                       xMaxSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               if( pxBlock->xBlockSize < xMinSize )\r
+                               {\r
+                                       xMinSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               /* Move to the next block in the chain until the last block is\r
+                               reached. */\r
+                               pxBlock = pxBlock->pxNextFreeBlock;\r
+                       } while( pxBlock != pxEnd );\r
+               }\r
+       }\r
+       xTaskResumeAll();\r
+\r
+       pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;\r
+       pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;\r
+       pxHeapStats->xNumberOfFreeBlocks = xBlocks;\r
+\r
+       taskENTER_CRITICAL();\r
+       {\r
+               pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;\r
+               pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;\r
+               pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;\r
+               pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;\r
+       }\r
+       taskEXIT_CRITICAL();\r
+}\r
 \r