\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
\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
\r
size_t xPortGetFreeHeapSize( void )\r
{\r
- return ( configTOTAL_HEAP_SIZE - xNextFreeByte );\r
+ return ( configADJUSTED_HEAP_SIZE - xNextFreeByte );\r
}\r
\r
\r