]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Demo/WIN32-MingW/main.c
Exercise the new vPortGetHeapStats() function from the Win32 demo projects.
[freertos] / FreeRTOS / Demo / WIN32-MingW / main.c
index 2f3b36bfdcb261afadad900001e680590ff5c31a..2ccf29ac715c111a7c5bead7c69b0e7675df8973 100644 (file)
@@ -103,6 +103,12 @@ void vFullDemoIdleFunction( void );
  */\r
 static void  prvInitialiseHeap( void );\r
 \r
+/*\r
+ * Performs a few sanity checks on the behaviour of the vPortGetHeapStats()\r
+ * function.\r
+ */\r
+static void prvExerciseHeapStats( void );\r
+\r
 /*\r
  * Prototypes for the standard FreeRTOS application hook (callback) functions\r
  * implemented within this file.  See http://www.freertos.org/a00016.html .\r
@@ -344,6 +350,8 @@ offsets into the array - with gaps in between and messy alignment just for test
 purposes. */\r
 static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];\r
 volatile uint32_t ulAdditionalOffset = 19; /* Just to prevent 'condition is always true' warnings in configASSERT(). */\r
+HeapStats_t xHeapStats;\r
+const HeapStats_t xZeroHeapStats = { 0 };\r
 const HeapRegion_t xHeapRegions[] =\r
 {\r
        /* Start address with dummy offsets                                             Size */\r
@@ -360,7 +368,80 @@ const HeapRegion_t xHeapRegions[] =
        /* Prevent compiler warnings when configASSERT() is not defined. */\r
        ( void ) ulAdditionalOffset;\r
 \r
+       /* The heap has not been initialised yet so expect stats to all be zero. */\r
+       vPortGetHeapStats( &xHeapStats );\r
+       configASSERT( memcmp( &xHeapStats, &xZeroHeapStats, sizeof( HeapStats_t ) ) == 0 );\r
+\r
        vPortDefineHeapRegions( xHeapRegions );\r
+\r
+       /* Sanity check vTaskGetHeapStats(). */\r
+       prvExerciseHeapStats();\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
+static void prvExerciseHeapStats( void )\r
+{\r
+HeapStats_t xHeapStats;\r
+size_t xInitialFreeSpace = xPortGetFreeHeapSize(), xMinimumFreeBytes;\r
+size_t xMetaDataOverhead, i;\r
+void *pvAllocatedBlock;\r
+const size_t xArraySize = 5, xBlockSize = 1000UL;\r
+void *pvAllocatedBlocks[ xArraySize ];\r
+\r
+       /* Check heap stats are as expected after initialisation but before any\r
+       allocations. */\r
+       vPortGetHeapStats( &xHeapStats );\r
+\r
+       /* Minimum ever free bytes remaining should be the same as the total number\r
+       of bytes as nothing has been allocated yet. */\r
+       configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xHeapStats.xAvailableHeapSpaceInBytes );\r
+       configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xInitialFreeSpace );\r
+\r
+       /* Nothing has been allocated or freed yet. */\r
+       configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == 0 );\r
+       configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 0 );\r
+\r
+       /* Allocate a 1000 byte block then measure what the overhead of the\r
+       allocation in regards to how many bytes more than 1000 were actually\r
+       removed from the heap in order to store metadata about the allocation. */\r
+       pvAllocatedBlock = pvPortMalloc( xBlockSize );\r
+       configASSERT( pvAllocatedBlock );\r
+       xMetaDataOverhead = ( xInitialFreeSpace - xPortGetFreeHeapSize() ) - xBlockSize;\r
+\r
+       /* Free the block again to get back to where we started. */\r
+       vPortFree( pvAllocatedBlock );\r
+       vPortGetHeapStats( &xHeapStats );\r
+       configASSERT( xHeapStats.xAvailableHeapSpaceInBytes == xInitialFreeSpace );\r
+       configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == 1 );\r
+       configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 1 );\r
+\r
+       /* Allocate blocks checking some stats value on each allocation. */\r
+       for( i = 0; i < xArraySize; i++ )\r
+       {\r
+               pvAllocatedBlocks[ i ] = pvPortMalloc( xBlockSize );\r
+               configASSERT( pvAllocatedBlocks[ i ] );\r
+               vPortGetHeapStats( &xHeapStats );\r
+               configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == ( xInitialFreeSpace - ( ( i + 1 ) * ( xBlockSize + xMetaDataOverhead ) ) ) );\r
+               configASSERT( xHeapStats.xMinimumEverFreeBytesRemaining == xHeapStats.xAvailableHeapSpaceInBytes );\r
+               configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == ( 2Ul + i ) );\r
+               configASSERT( xHeapStats.xNumberOfSuccessfulFrees == 1 ); /* Does not increase during allocations. */\r
+       }\r
+\r
+       configASSERT( xPortGetFreeHeapSize() == xPortGetMinimumEverFreeHeapSize() );\r
+       xMinimumFreeBytes = xPortGetFreeHeapSize();\r
+\r
+       /* Free the blocks again. */\r
+       for( i = 0; i < xArraySize; i++ )\r
+       {\r
+               vPortFree( pvAllocatedBlocks[ i ] );\r
+               vPortGetHeapStats( &xHeapStats );\r
+               configASSERT( xHeapStats.xAvailableHeapSpaceInBytes == ( xInitialFreeSpace - ( ( ( xArraySize - i - 1 ) * ( xBlockSize + xMetaDataOverhead ) ) ) ) );\r
+               configASSERT( xHeapStats.xNumberOfSuccessfulAllocations == ( xArraySize + 1 ) ); /* Does not increase during frees. */\r
+               configASSERT( xHeapStats.xNumberOfSuccessfulFrees == ( 2UL + i ) );\r
+       }\r
+\r
+       /* The minimum ever free heap size should not change as blocks are freed. */\r
+       configASSERT( xMinimumFreeBytes == xPortGetMinimumEverFreeHeapSize() );\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r