]> git.sur5r.net Git - freertos/commitdiff
Check in the new memory allocator that allows the heap to span multiple blocks.
authorrtel <rtel@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Wed, 2 Jul 2014 10:19:49 +0000 (10:19 +0000)
committerrtel <rtel@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Wed, 2 Jul 2014 10:19:49 +0000 (10:19 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2267 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS/Source/portable/MemMang/heap_5.c [new file with mode: 0644]

diff --git a/FreeRTOS/Source/portable/MemMang/heap_5.c b/FreeRTOS/Source/portable/MemMang/heap_5.c
new file mode 100644 (file)
index 0000000..c01205e
--- /dev/null
@@ -0,0 +1,519 @@
+/*\r
+    FreeRTOS V8.0.1 - 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
+\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     !<<\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
+    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
+\r
+/*\r
+ * A sample implementation of pvPortMalloc() that allows the heap to be defined\r
+ * across multiple non-contigous blocks and combines (coalescences) adjacent\r
+ * memory blocks as they are freed.\r
+ *\r
+ * See heap_1.c, heap_2.c, heap_3.c and heap_4.c for alternative\r
+ * implementations, and the memory management pages of http://www.FreeRTOS.org\r
+ * for more information.\r
+ *\r
+ * Usage notes:\r
+ *\r
+ * vPortDefineHeapRegions() ***must*** be called before pvPortMalloc().\r
+ * pvPortMalloc() will be called if any task objects (tasks, queues, event\r
+ * groups, etc.) are created, therefore vPortDefineHeapRegions() ***must*** be\r
+ * called before any other objects are defined.\r
+ *\r
+ * vPortDefineHeapRegions() takes a single parameter.  The parameter is an array\r
+ * of HeapRegion_t structures.  HeapRegion_t is defined in portable.h as\r
+ *\r
+ * typedef struct HeapRegion\r
+ * {\r
+ *     uint8_t *pucStartAddress; << Start address of a block of memory that will be part of the heap.\r
+ *     size_t xSizeInBytes;      << Size of the block of memory.\r
+ * } HeapRegion_t;\r
+ *\r
+ * The array is terminated using a NULL zero sized region definition, and the\r
+ * memory regions defined in the array ***must*** appear in address order from\r
+ * low address to high address.  So the following is a valid example of how\r
+ * to use the function.\r
+ *\r
+ * HeapRegion_t xHeapRegions[] =\r
+ * {\r
+ *     { 0x80000000UL, 0x10000 }, << Defines a block of 0x10000 bytes starting at address 0x80000000\r
+ *     { 0x90000000UL, 0xa0000 }, << Defines a block of 0xa0000 bytes starting at address of 0x90000000\r
+ *     { NULL, 0 }                << Terminates the array.\r
+ * };\r
+ *\r
+ * vPortDefineHeapRegions( xHeapRegions ); << Pass the array into vPortDefineHeapRegions().\r
+ *\r
+ * Note 0x80000000 is the lower address so appears in the array first.\r
+ *\r
+ */\r
+#include <stdlib.h>\r
+\r
+/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining\r
+all the API functions to use the MPU wrappers.  That should only be done when\r
+task.h is included from an application file. */\r
+#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
+\r
+#include "FreeRTOS.h"\r
+#include "task.h"\r
+\r
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
+\r
+/* Block sizes must not get too small. */\r
+#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( uxHeapStructSize << 1 ) )\r
+\r
+/* Assumes 8bit bytes! */\r
+#define heapBITS_PER_BYTE              ( ( size_t ) 8 )\r
+\r
+/* Define the linked list structure.  This is used to link free blocks in order\r
+of their memory address. */\r
+typedef struct A_BLOCK_LINK\r
+{\r
+       struct A_BLOCK_LINK *pxNextFreeBlock;   /*<< The next free block in the list. */\r
+       size_t xBlockSize;                                              /*<< The size of the free block. */\r
+} BlockLink_t;\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/*\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( BlockLink_t *pxBlockToInsert );\r
+\r
+/*-----------------------------------------------------------*/\r
+\r
+/* The size of the structure placed at the beginning of each allocated memory\r
+block must by correctly byte aligned. */\r
+static const uint32_t uxHeapStructSize = ( ( 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 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 = 0;\r
+static size_t xMinimumEverFreeBytesRemaining = 0;\r
+\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
+\r
+/*-----------------------------------------------------------*/\r
+\r
+void *pvPortMalloc( size_t xWantedSize )\r
+{\r
+BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
+void *pvReturn = NULL;\r
+\r
+       /* The heap must be initialised before the first call to\r
+       prvPortMalloc(). */\r
+       configASSERT( pxEnd );\r
+\r
+       vTaskSuspendAll();\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 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 BlockLink_t\r
+                       structure in addition to the requested amount of bytes. */\r
+                       if( xWantedSize > 0 )\r
+                       {\r
+                               xWantedSize += uxHeapStructSize;\r
+\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
+                               }\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
+                               one     of adequate size is found. */\r
+                               pxPreviousBlock = &xStart;\r
+                               pxBlock = xStart.pxNextFreeBlock;\r
+                               while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )\r
+                               {\r
+                                       pxPreviousBlock = pxBlock;\r
+                                       pxBlock = pxBlock->pxNextFreeBlock;\r
+                               }\r
+\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
+                                       BlockLink_t structure at its start. */\r
+                                       pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + uxHeapStructSize );\r
+\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
+                                       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
+                                               compiler. */\r
+                                               pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );\r
+\r
+                                               /* Calculate the sizes of two blocks split from the\r
+                                               single block. */\r
+                                               pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;\r
+                                               pxBlock->xBlockSize = xWantedSize;\r
+\r
+                                               /* 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
+                                       if( xFreeBytesRemaining < xMinimumEverFreeBytesRemaining )\r
+                                       {\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
+       ( void ) xTaskResumeAll();\r
+\r
+       #if( configUSE_MALLOC_FAILED_HOOK == 1 )\r
+       {\r
+               if( pvReturn == NULL )\r
+               {\r
+                       extern void vApplicationMallocFailedHook( void );\r
+                       vApplicationMallocFailedHook();\r
+               }\r
+               else\r
+               {\r
+                       mtCOVERAGE_TEST_MARKER();\r
+               }\r
+       }\r
+       #endif\r
+\r
+       return pvReturn;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortFree( void *pv )\r
+{\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 BlockLink_t structure immediately\r
+               before it. */\r
+               puc -= uxHeapStructSize;\r
+\r
+               /* This casting is to keep the compiler from issuing warnings. */\r
+               pxLink = ( void * ) puc;\r
+\r
+               /* Check the block is actually allocated. */\r
+               configASSERT( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 );\r
+               configASSERT( pxLink->pxNextFreeBlock == NULL );\r
+\r
+               if( ( pxLink->xBlockSize & xBlockAllocatedBit ) != 0 )\r
+               {\r
+                       if( pxLink->pxNextFreeBlock == NULL )\r
+                       {\r
+                               /* The block is being returned to the heap - it is no longer\r
+                               allocated. */\r
+                               pxLink->xBlockSize &= ~xBlockAllocatedBit;\r
+\r
+                               vTaskSuspendAll();\r
+                               {\r
+                                       /* Add this block to the list of free blocks. */\r
+                                       xFreeBytesRemaining += pxLink->xBlockSize;\r
+                                       traceFREE( pv, pxLink->xBlockSize );\r
+                                       prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+                               }\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
+\r
+size_t xPortGetFreeHeapSize( void )\r
+{\r
+       return xFreeBytesRemaining;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+size_t xPortGetMinimumEverFreeHeapSize( void )\r
+{\r
+       return xMinimumEverFreeBytesRemaining;\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvInsertBlockIntoFreeList( BlockLink_t *pxBlockToInsert )\r
+{\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
+       for( pxIterator = &xStart; pxIterator->pxNextFreeBlock < pxBlockToInsert; pxIterator = pxIterator->pxNextFreeBlock )\r
+       {\r
+               /* Nothing to do here, just iterate to the right position. */\r
+       }\r
+\r
+       /* Do the block being inserted, and the block it is being inserted after\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 = ( uint8_t * ) pxBlockToInsert;\r
+       if( ( puc + pxBlockToInsert->xBlockSize ) == ( uint8_t * ) pxIterator->pxNextFreeBlock )\r
+       {\r
+               if( pxIterator->pxNextFreeBlock != pxEnd )\r
+               {\r
+                       /* Form one big block from the two blocks. */\r
+                       pxBlockToInsert->xBlockSize += pxIterator->pxNextFreeBlock->xBlockSize;\r
+                       pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock->pxNextFreeBlock;\r
+               }\r
+               else\r
+               {\r
+                       pxBlockToInsert->pxNextFreeBlock = pxEnd;\r
+               }\r
+       }\r
+       else\r
+       {\r
+               pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock;\r
+       }\r
+\r
+       /* If the block being inserted plugged a gab, so was merged with the block\r
+       before and the block after, then it's pxNextFreeBlock pointer will have\r
+       already been set, and should not be set here as that would make it point\r
+       to itself. */\r
+       if( pxIterator != pxBlockToInsert )\r
+       {\r
+               pxIterator->pxNextFreeBlock = pxBlockToInsert;\r
+       }\r
+       else\r
+       {\r
+               mtCOVERAGE_TEST_MARKER();\r
+       }\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortDefineHeapRegions( HeapRegion_t *pxHeapRegions )\r
+{\r
+BlockLink_t *pxFirstFreeBlockInRegion = NULL, *pxPreviousFreeBlock;\r
+uint8_t *pucAlignedHeap;\r
+size_t xTotalRegionSize, xTotalHeapSize = 0;\r
+BaseType_t xDefinedRegions = 0;\r
+uint32_t ulAddress;\r
+HeapRegion_t *pxHeapRegion;\r
+\r
+       /* Can only call once! */\r
+       configASSERT( pxEnd == NULL );\r
+\r
+       pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
+\r
+       while( pxHeapRegion->xSizeInBytes > 0 )\r
+       {\r
+               xTotalRegionSize = pxHeapRegion->xSizeInBytes;\r
+\r
+               /* Ensure the heap region starts on a correctly aligned boundary. */\r
+               ulAddress = ( uint32_t ) pxHeapRegion->pucStartAddress;\r
+               if( ( ulAddress & portBYTE_ALIGNMENT_MASK ) != 0 )\r
+               {\r
+                       ulAddress += ( portBYTE_ALIGNMENT - 1 );\r
+                       ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+\r
+                       /* Adjust the size for the bytes lost to alignment. */\r
+                       xTotalRegionSize -= ulAddress - ( uint32_t ) pxHeapRegion->pucStartAddress;\r
+               }\r
+\r
+               pucAlignedHeap = ( uint8_t * ) ulAddress;\r
+\r
+               /* Set xStart if it has not already been set. */\r
+               if( xDefinedRegions == 0 )\r
+               {\r
+                       /* xStart is used to hold a pointer to the first item in the list of\r
+                       free blocks.  The void cast is used to prevent compiler warnings. */\r
+                       xStart.pxNextFreeBlock = ( BlockLink_t * ) pucAlignedHeap;\r
+                       xStart.xBlockSize = ( size_t ) 0;\r
+               }\r
+               else\r
+               {\r
+                       /* Should only get here if one region has already been added to the\r
+                       heap. */\r
+                       configASSERT( pxEnd != NULL );\r
+\r
+                       /* Check blocks are passed in with increasing start addresses. */\r
+                       configASSERT( ulAddress > ( uint32_t ) pxEnd );\r
+               }\r
+\r
+               /* Remember the location of the end marker in the previous region, if\r
+               any. */\r
+               pxPreviousFreeBlock = pxEnd;\r
+\r
+               /* pxEnd is used to mark the end of the list of free blocks and is\r
+               inserted at the end of the region space. */\r
+               ulAddress = ( ( uint32_t ) pucAlignedHeap ) + xTotalRegionSize;\r
+               ulAddress -= uxHeapStructSize;\r
+               ulAddress &= ~portBYTE_ALIGNMENT_MASK;\r
+               pxEnd = ( BlockLink_t * ) ulAddress;\r
+               pxEnd->xBlockSize = 0;\r
+               pxEnd->pxNextFreeBlock = NULL;\r
+\r
+               /* To start with there is a single free block in this region that is\r
+               sized to take up the entire heap region minus the space taken by the\r
+               free block structure. */\r
+               pxFirstFreeBlockInRegion = ( BlockLink_t * ) pucAlignedHeap;\r
+               pxFirstFreeBlockInRegion->xBlockSize = ulAddress - ( uint32_t ) pxFirstFreeBlockInRegion;\r
+               pxFirstFreeBlockInRegion->pxNextFreeBlock = pxEnd;\r
+\r
+               /* If this is not the first region that makes up the entire heap space\r
+               then link the previous region to this region. */\r
+               if( pxPreviousFreeBlock != NULL )\r
+               {\r
+                       pxPreviousFreeBlock->pxNextFreeBlock = pxFirstFreeBlockInRegion;\r
+               }\r
+\r
+               xTotalHeapSize += pxFirstFreeBlockInRegion->xBlockSize;\r
+\r
+               /* Move onto the next HeapRegion_t structure. */\r
+               xDefinedRegions++;\r
+               pxHeapRegion = &( pxHeapRegions[ xDefinedRegions ] );\r
+       }\r
+\r
+       xMinimumEverFreeBytesRemaining = xTotalHeapSize;\r
+       xFreeBytesRemaining = xTotalHeapSize;\r
+\r
+       /* Check something was actually defined before it is accessed. */\r
+       configASSERT( xTotalHeapSize );\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