]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/MemMang/heap_1.c
Added more files to the Rowley and IAR LM3S demos to test building the newer files...
[freertos] / FreeRTOS / Source / portable / MemMang / heap_1.c
index 65e2cd782e12e682cd9cee6f9cb2e82b50dbdeb6..f3190dac9a06b68fdeb607612dcd74c2c1959b91 100644 (file)
@@ -86,24 +86,19 @@ 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
+/* Allocate the memory for the heap. */\r
+static unsigned char ucHeap[ configTOTAL_HEAP_SIZE ];\r
 static size_t xNextFreeByte = ( size_t ) 0;\r
+\r
 /*-----------------------------------------------------------*/\r
 \r
 void *pvPortMalloc( size_t xWantedSize )\r
 {\r
 void *pvReturn = NULL;\r
+static unsigned char *pucAlignedHeap = NULL;\r
 \r
        /* Ensure that blocks are always aligned to the required number of bytes. */\r
        #if portBYTE_ALIGNMENT != 1\r
@@ -116,13 +111,19 @@ void *pvReturn = NULL;
 \r
        vTaskSuspendAll();\r
        {\r
+               if( pucAlignedHeap == NULL )\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
+\r
                /* Check there is enough room left for the allocation. */\r
-               if( ( ( xNextFreeByte + xWantedSize ) < configTOTAL_HEAP_SIZE ) &&\r
+               if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) &&\r
                        ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte )     )/* Check for overflow. */\r
                {\r
                        /* Return the next free byte then increment the index past this\r
                        block. */\r
-                       pvReturn = &( xHeap.ucHeap[ xNextFreeByte ] );\r
+                       pvReturn = pucAlignedHeap + xNextFreeByte;\r
                        xNextFreeByte += xWantedSize;\r
                }\r
        }\r
@@ -163,7 +164,7 @@ void vPortInitialiseBlocks( void )
 \r
 size_t xPortGetFreeHeapSize( void )\r
 {\r
-       return ( configTOTAL_HEAP_SIZE - xNextFreeByte );\r
+       return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );\r
 }\r
 \r
 \r