]> git.sur5r.net Git - freertos/blobdiff - FreeRTOS/Source/portable/MemMang/heap_5.c
Add vPortGetHeapStats() function to query heap statistics.
[freertos] / FreeRTOS / Source / portable / MemMang / heap_5.c
index 8e50762360bd1b920bd6a390cb551834d4b7733a..614210edf3e5f0c01efedc17325aec740f0e9b2b 100644 (file)
@@ -116,10 +116,12 @@ static const size_t xHeapStructSize       = ( sizeof( BlockLink_t ) + ( ( size_t ) ( p
 /* 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
+/* Keeps track of the number of calls to allocate and free memory as well as the\r
+number of free bytes remaining, but says nothing about fragmentation. */\r
 static size_t xFreeBytesRemaining = 0U;\r
 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
+static size_t xNumberOfSuccessfulAllocations = 0;\r
+static size_t xNumberOfSuccessfulFrees = 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
@@ -231,6 +233,7 @@ void *pvReturn = NULL;
                                        by the application and has no "next" block. */\r
                                        pxBlock->xBlockSize |= xBlockAllocatedBit;\r
                                        pxBlock->pxNextFreeBlock = NULL;\r
+                                       xNumberOfSuccessfulAllocations++;\r
                                }\r
                                else\r
                                {\r
@@ -301,6 +304,7 @@ BlockLink_t *pxLink;
                                        xFreeBytesRemaining += pxLink->xBlockSize;\r
                                        traceFREE( pv, pxLink->xBlockSize );\r
                                        prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+                                       xNumberOfSuccessfulFrees++;\r
                                }\r
                                ( void ) xTaskResumeAll();\r
                        }\r
@@ -482,4 +486,56 @@ const HeapRegion_t *pxHeapRegion;
        /* 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
+\r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats )\r
+{\r
+       BlockLink_t *pxBlock;\r
+       size_t xBlocks = 0, xMaxSize = 0, xMinSize = 0;\r
+\r
+       vTaskSuspendAll();\r
+       {\r
+               pxBlock = xStart.pxNextFreeBlock;\r
+\r
+               /* pxBlock will be NULL if the heap has not been initialised.  The heap\r
+               is initialised automatically when the first allocation is made. */\r
+               if( pxBlock != NULL )\r
+               {\r
+                       do\r
+                       {\r
+                               /* Increment the number of blocks and record the largest block seen\r
+                               so far. */\r
+                               xBlocks++;\r
+\r
+                               if( pxBlock->xBlockSize > xMaxSize )\r
+                               {\r
+                                       xMaxSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               if( pxBlock->xBlockSize < xMinSize )\r
+                               {\r
+                                       xMinSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               /* Move to the next block in the chain until the last block is\r
+                               reached. */\r
+                               pxBlock = pxBlock->pxNextFreeBlock;\r
+                       } while( pxBlock != pxEnd );\r
+               }\r
+       }\r
+       xTaskResumeAll();\r
+\r
+       pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;\r
+       pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;\r
+       pxHeapStats->xNumberOfFreeBlocks = xBlocks;\r
+\r
+       taskENTER_CRITICAL();\r
+       {\r
+               pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;\r
+               pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;\r
+               pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;\r
+               pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;\r
+       }\r
+       taskEXIT_CRITICAL();\r
+}\r
 \r