]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/MemMang/heap_2.c
Added more files to the Rowley and IAR LM3S demos to test building the newer files...
[freertos] / FreeRTOS / Source / portable / MemMang / heap_2.c
index bd827a94127427bf95e2f82b3afb1d69bd694ade..bd07401ab8fd4a03a60af369ed0793cc2dec0ab4 100644 (file)
@@ -1,7 +1,7 @@
 /*\r
     FreeRTOS V7.3.0 - Copyright (C) 2012 Real Time Engineers Ltd.\r
 \r
-    FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT \r
+    FEATURES AND PORTS ARE ADDED TO FREERTOS ALL THE TIME.  PLEASE VISIT\r
     http://www.FreeRTOS.org TO ENSURE YOU ARE USING THE LATEST VERSION.\r
 \r
     ***************************************************************************\r
@@ -42,7 +42,7 @@
     FreeRTOS WEB site.\r
 \r
     1 tab == 4 spaces!\r
-    \r
+\r
     ***************************************************************************\r
      *                                                                       *\r
      *    Having a problem?  Start by reading the FAQ "My application does   *\r
      *                                                                       *\r
     ***************************************************************************\r
 \r
-    \r
-    http://www.FreeRTOS.org - Documentation, training, latest versions, license \r
-    and contact details.  \r
-    \r
+\r
+    http://www.FreeRTOS.org - Documentation, training, latest versions, license\r
+    and contact details.\r
+\r
     http://www.FreeRTOS.org/plus - A selection of FreeRTOS ecosystem products,\r
     including FreeRTOS+Trace - an indispensable productivity tool.\r
 \r
-    Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell \r
-    the code with commercial support, indemnification, and middleware, under \r
+    Real Time Engineers ltd license FreeRTOS to High Integrity Systems, who sell\r
+    the code with commercial support, indemnification, and middleware, under\r
     the OpenRTOS brand: http://www.OpenRTOS.com.  High Integrity Systems also\r
-    provide a safety engineered and independently SIL3 certified version under \r
+    provide a safety engineered and independently SIL3 certified version under\r
     the SafeRTOS brand: http://www.SafeRTOS.com.\r
 */\r
 \r
 /*\r
  * A sample implementation of pvPortMalloc() and vPortFree() that permits\r
  * allocated blocks to be freed, but does not combine adjacent free blocks\r
- * into a single larger block (and so will fragment memory).  See heap_4.c for \r
+ * into a single larger block (and so will fragment memory).  See heap_4.c for\r
  * an aquivalent that does combine adjacent blocks into single larger blocks.\r
  *\r
- * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the \r
+ * See heap_1.c, heap_3.c and heap_4.c for alternative implementations, and the\r
  * memory management pages of http://www.FreeRTOS.org for more information.\r
  */\r
 #include <stdlib.h>\r
@@ -87,17 +87,16 @@ task.h is included from an application file. */
 \r
 #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE\r
 \r
-/* Allocate the memory for the heap.  The struct is used to force byte\r
-alignment without using any non-portable code. */\r
-static union xRTOS_HEAP\r
-{\r
-       #if portBYTE_ALIGNMENT == 8\r
-               volatile portDOUBLE dDummy;\r
-       #else\r
-               volatile unsigned long ulDummy;\r
-       #endif\r
-       unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
-} xHeap;\r
+/* A few bytes might be lost to byte aligning the heap start address. */\r
+#define configADJUSTED_HEAP_SIZE       ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )\r
+\r
+/* \r
+ * Initialises the heap structures into their start condition. \r
+ */\r
+static void prvHeapInit( void );\r
+\r
+/* Allocate the memory for the heap. */\r
+static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
 \r
 /* Define the linked list structure.  This is used to link free blocks in order\r
 of their size. */\r
@@ -116,7 +115,7 @@ static xBlockLink xStart, xEnd;
 \r
 /* Keeps track of the number of free bytes remaining, but says nothing about\r
 fragmentation. */\r
-static size_t xFreeBytesRemaining = configTOTAL_HEAP_SIZE;\r
+static size_t xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;\r
 \r
 /* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */\r
 \r
@@ -146,27 +145,6 @@ size_t xBlockSize;                                                                                                                                 \
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#define prvHeapInit()                                                                                                                          \\r
-{                                                                                                                                                                      \\r
-xBlockLink *pxFirstFreeBlock;                                                                                                          \\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
-       xStart.pxNextFreeBlock = ( void * ) xHeap.ucHeap;                                                               \\r
-       xStart.xBlockSize = ( size_t ) 0;                                                                                               \\r
-                                                                                                                                                                       \\r
-       /* xEnd is used to mark the end of the list of free blocks. */                                  \\r
-       xEnd.xBlockSize = configTOTAL_HEAP_SIZE;                                                                                \\r
-       xEnd.pxNextFreeBlock = NULL;                                                                                                    \\r
-                                                                                                                                                                       \\r
-       /* To start with there is a single free block that is sized to take up the              \\r
-       entire heap space. */                                                                                                                   \\r
-       pxFirstFreeBlock = ( void * ) xHeap.ucHeap;                                                                             \\r
-       pxFirstFreeBlock->xBlockSize = configTOTAL_HEAP_SIZE;                                                   \\r
-       pxFirstFreeBlock->pxNextFreeBlock = &xEnd;                                                                              \\r
-}\r
-/*-----------------------------------------------------------*/\r
-\r
 void *pvPortMalloc( size_t xWantedSize )\r
 {\r
 xBlockLink *pxBlock, *pxPreviousBlock, *pxNewBlockLink;\r
@@ -197,7 +175,7 @@ void *pvReturn = NULL;
                        }\r
                }\r
 \r
-               if( ( xWantedSize > 0 ) && ( xWantedSize < configTOTAL_HEAP_SIZE ) )\r
+               if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )\r
                {\r
                        /* Blocks are stored in byte order - traverse the list from the start\r
                        (smallest) block until one of adequate size is found. */\r
@@ -236,7 +214,7 @@ void *pvReturn = NULL;
                                        /* Insert the new block into the list of free blocks. */\r
                                        prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );\r
                                }\r
-                               \r
+\r
                                xFreeBytesRemaining -= pxBlock->xBlockSize;\r
                        }\r
                }\r
@@ -268,7 +246,8 @@ xBlockLink *pxLink;
                before it. */\r
                puc -= heapSTRUCT_SIZE;\r
 \r
-               /* This casting is to keep the compiler from issuing warnings. */\r
+               /* This unexpected casting is to keep some compilers from issuing \r
+               byte alignment warnings. */\r
                pxLink = ( void * ) puc;\r
 \r
                vTaskSuspendAll();\r
@@ -292,3 +271,29 @@ void vPortInitialiseBlocks( void )
 {\r
        /* This just exists to keep the linker quiet. */\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvHeapInit( void )\r
+{\r
+xBlockLink *pxFirstFreeBlock;\r
+unsigned char *pucAlignedHeap;\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
+\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
+       xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;\r
+       xStart.xBlockSize = ( size_t ) 0;\r
+\r
+       /* xEnd is used to mark the end of the list of free blocks. */\r
+       xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;\r
+       xEnd.pxNextFreeBlock = NULL;\r
+\r
+       /* To start with there is a single free block that is sized to take up the\r
+       entire heap space. */\r
+       pxFirstFreeBlock = ( void * ) pucAlignedHeap;\r
+       pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;\r
+       pxFirstFreeBlock->pxNextFreeBlock = &xEnd;\r
+}\r
+/*-----------------------------------------------------------*/\r