#endif\r
#endif\r
\r
-/* Used by heap_5.c. */\r
+/* Used by heap_5.c to define the start address and size of each memory region\r
+that together comprise the total FreeRTOS heap space. */\r
typedef struct HeapRegion\r
{\r
uint8_t *pucStartAddress;\r
size_t xSizeInBytes;\r
} HeapRegion_t;\r
\r
+/* Used to pass information about the heap out of vPortGetHeapStats(). */\r
+typedef struct xHeapStats\r
+{\r
+ size_t xAvailableHeapSpaceInBytes; /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */\r
+ size_t xSizeOfLargestFreeBlockInBytes; /* The maximum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */\r
+ size_t xSizeOfSmallestFreeBlockInBytes; /* The minimum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */\r
+ size_t xNumberOfFreeBlocks; /* The number of free memory blocks within the heap at the time vPortGetHeapStats() is called. */\r
+ size_t xMinimumEverFreeBytesRemaining; /* The minimum amount of total free memory (sum of all free blocks) there has been in the heap since the system booted. */\r
+ size_t xNumberOfSuccessfulAllocations; /* The number of calls to pvPortMalloc() that have returned a valid memory block. */\r
+ size_t xNumberOfSuccessfulFrees; /* The number of calls to vPortFree() that has successfully freed a block of memory. */\r
+} HeapStats_t;\r
+\r
/*\r
* Used to define multiple heap regions for use by heap_5.c. This function\r
* must be called before any calls to pvPortMalloc() - not creating a task,\r
*/\r
void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION;\r
\r
+/*\r
+ * Returns a HeapStats_t structure filled with information about the current\r
+ * heap state.\r
+ */\r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats );\r
\r
/*\r
* Map to the memory management routines required for the port.\r
/* 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
by the application and has no "next" block. */\r
pxBlock->xBlockSize |= xBlockAllocatedBit;\r
pxBlock->pxNextFreeBlock = NULL;\r
+ xNumberOfSuccessfulAllocations++;\r
}\r
else\r
{\r
xFreeBytesRemaining += pxLink->xBlockSize;\r
traceFREE( pv, pxLink->xBlockSize );\r
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+ xNumberOfSuccessfulFrees++;\r
}\r
( void ) xTaskResumeAll();\r
}\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
+\r
static void prvHeapInit( void )\r
{\r
BlockLink_t *pxFirstFreeBlock;\r
/* 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
by the application and has no "next" block. */\r
pxBlock->xBlockSize |= xBlockAllocatedBit;\r
pxBlock->pxNextFreeBlock = NULL;\r
+ xNumberOfSuccessfulAllocations++;\r
}\r
else\r
{\r
xFreeBytesRemaining += pxLink->xBlockSize;\r
traceFREE( pv, pxLink->xBlockSize );\r
prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+ xNumberOfSuccessfulFrees++;\r
}\r
( void ) xTaskResumeAll();\r
}\r
/* 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