}\r
}\r
\r
-\r
-#define INCLUDE_TEST_CODE 0\r
-#if INCLUDE_TEST_CODE == 1\r
-\r
-#define heapMAX_TEST_BLOCKS 6\r
-\r
-void vTestHeap4( void )\r
-{\r
-void *pvReturned;\r
-static void *pvUsedBlocks[ heapMAX_TEST_BLOCKS ];\r
-unsigned long ulIndex = 0, ulSize, ulRandSample;\r
-size_t xSize1, xSize2, xSize3;\r
-static const unsigned long ulCombinations[ 6 ][ 3 ] =\r
-{\r
- { 0, 1, 2 },\r
- { 0, 2, 1 },\r
- { 1, 0, 2 },\r
- { 1, 2, 0 },\r
- { 2, 0, 1 },\r
- { 2, 1, 0 }\r
-};\r
-\r
- /* Attempt to obtain a block of memory that equals the enture heap size.\r
- This should fail as the size of a block link structure will be added to the\r
- block in pvPortMalloc(). */\r
- pvReturned = pvPortMalloc( xTotalHeapSize );\r
- configASSERT( pvReturned == NULL );\r
-\r
- /* Attempt to obtain a block of memory that equals the entire heap size \r
- minus the size of the block link structure that will get added to the \r
- wanted size inside pvPortMalloc(). This should also fail as the heap \r
- already contains a start and end block link structure. */\r
- pvReturned = pvPortMalloc( xTotalHeapSize - heapSTRUCT_SIZE );\r
- configASSERT( pvReturned == NULL );\r
-\r
- /* Attempt to obtain a block of memory that equals the entire heap size \r
- minus the size of the block link structure that will get added to the \r
- wanted size inside pvPortMalloc(), minus the size of the block link \r
- structure that marks the end of the heap. */\r
- pvReturned = pvPortMalloc( xTotalHeapSize - ( 2 * heapSTRUCT_SIZE ) );\r
-\r
- /* The returned value should point just past the first block link. */\r
- configASSERT( pvReturned == ( xHeap.ucHeap + heapSTRUCT_SIZE ) );\r
-\r
- /* There should be no heap remaining. */\r
- configASSERT( xFreeBytesRemaining == 0 );\r
-\r
- /* The start should point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock == pxEnd );\r
-\r
- /* Free the memory again. */\r
- vPortFree( pvReturned );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
-\r
- /* The next test plugs a gap that create a continuous block up to the pxEnd\r
- marker. */\r
-\r
- /* Remove a small block. */\r
- pvUsedBlocks[ ulIndex ] = pvPortMalloc( 8 );\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE - 8 - heapSTRUCT_SIZE ) );\r
- ulIndex++;\r
-\r
- /* Remove another block. */\r
- pvUsedBlocks[ ulIndex ] = pvPortMalloc( 32 );\r
-\r
- /* Return the frist removed block, which should join with the start block\r
- and leave a gap. */\r
- vPortFree( pvUsedBlocks[ 0 ] );\r
- \r
- /* Return the second free block, which should fill the gap. */\r
- vPortFree( pvUsedBlocks[ 1 ] );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
-\r
- /* The next test plugs a gap that create a continuous block but not up to\r
- the end marker - it then fills the last gap too. */\r
-\r
- ulIndex = 0;\r
-\r
- /* Remove a small block. */\r
- pvUsedBlocks[ ulIndex ] = pvPortMalloc( 8 );\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE - 8 - heapSTRUCT_SIZE ) );\r
- ulIndex++;\r
-\r
- /* Remove another block. */\r
- pvUsedBlocks[ ulIndex ] = pvPortMalloc( 32 );\r
- ulIndex++;\r
-\r
- /* And one final block. */\r
- pvUsedBlocks[ ulIndex ] = pvPortMalloc( 128 );\r
-\r
- /* Return the frist removed block, which should join with the start block\r
- and leave a gap. */\r
- vPortFree( pvUsedBlocks[ 0 ] );\r
-\r
- /* Return the last block, which should join with the end. */\r
- vPortFree( pvUsedBlocks[ 2 ] );\r
- \r
- /* Return the middle block block, which should fill the gap. */\r
- vPortFree( pvUsedBlocks[ 1 ] );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
-\r
- for( ulIndex = 0; ulIndex < 6; ulIndex++ )\r
- {\r
- pvUsedBlocks[ 0 ] = pvPortMalloc( 10 );\r
- pvUsedBlocks[ 1 ] = pvPortMalloc( 1 );\r
- pvUsedBlocks[ 2 ] = pvPortMalloc( 10000 );\r
-\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 0 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 1 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 2 ] ] );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
- }\r
-\r
- /* Do the same, but using the entire block of memory. */\r
- for( ulIndex = 0; ulIndex < 6; ulIndex++ )\r
- {\r
- /* Total heap size. */\r
- ulSize = xTotalHeapSize - heapSTRUCT_SIZE;\r
-\r
- /* Minus 4 heap structs (three allocated blocks plus pxEnd. */\r
- ulSize -= 4 * heapSTRUCT_SIZE;\r
-\r
- pvUsedBlocks[ 0 ] = pvPortMalloc( ulSize / 3 );\r
- pvUsedBlocks[ 1 ] = pvPortMalloc( ulSize / 3 );\r
- /* The last block includes any remainder. */\r
- pvUsedBlocks[ 2 ] = pvPortMalloc( ( ulSize / 3 ) + ( ulSize % 3 ) );\r
- configASSERT( pvUsedBlocks[ 2 ] ); \r
- configASSERT( xFreeBytesRemaining == 0 );\r
-\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 0 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 1 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 2 ] ] );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
- }\r
-\r
- /* Do the same, but using random block sizes. */\r
- for( ulRandSample = 0; ulRandSample < 0xffffffUL; ulRandSample++ )\r
- {\r
- xSize1 = rand();\r
- xSize2 = rand();\r
- xSize3 = rand();\r
-\r
- for( ulIndex = 0; ulIndex < 6; ulIndex++ )\r
- {\r
- pvUsedBlocks[ 0 ] = pvPortMalloc( xSize1 );\r
- pvUsedBlocks[ 1 ] = pvPortMalloc( xSize2 );\r
- pvUsedBlocks[ 2 ] = pvPortMalloc( xSize3 );\r
-\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 0 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 1 ] ] );\r
- vPortFree( pvUsedBlocks[ ulCombinations[ ulIndex ][ 2 ] ] );\r
-\r
- /* The heap should be back to its full size, which is the total bytes\r
- in the array minus the space taken up by the pxEnd structure. */\r
- configASSERT( xFreeBytesRemaining == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
-\r
- /* The start block should now point to a block that holds the entire heap\r
- space, which should itself point to the end. */\r
- configASSERT( xStart.pxNextFreeBlock->xBlockSize == ( xTotalHeapSize - heapSTRUCT_SIZE ) );\r
- configASSERT( xStart.pxNextFreeBlock->pxNextFreeBlock == pxEnd );\r
- }\r
- }\r
-\r
-/* Particularly test the case where the block being inserted fills a gap \r
-requiring both the block in front and the block behind to be merged into one. */\r
-}\r
-\r
-#endif INCLUDE_TEST_CODE\r