home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / memory / heapwalk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-11-01  |  3.6 KB  |  126 lines

  1. /* HEAPWALK.C illustrates heap testing functions including:
  2.  *      _heapchk        _fheapchk       _nheapchk
  3.  *      _heapset        _fheapset       _nheapset
  4.  *      _heapwalk       _fheapwalk      _nheapwalk
  5.  *      _msize          _fmsize         _nmsize
  6.  *
  7.  * Only the model independent versions are shown. They map to the model
  8.  * dependent versions, depending on the memory model.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <conio.h>
  13. #include <malloc.h>
  14. #include <stdlib.h>
  15. #include <time.h>
  16.  
  17. /* Macro to get a random integer within a specified range */
  18. #define getrandom( min, max ) ((rand() % (int)((max) - (min))) + (min) + 1)
  19.  
  20. void heapdump( char fill );
  21. void heapstat( int status );
  22.  
  23. main()
  24. {
  25.     int *p[10], i;
  26.  
  27.     srand( (unsigned)time( 0L ) );  /* Seed with current time. */
  28.  
  29.     /* Check heap status. Should be OK at start of heap. */
  30.     heapstat( _heapchk() );
  31.  
  32.     /* Now do some operations that affect the heap. In this example,
  33.      * allocate random-size blocks.
  34.      */
  35.     for( i = 0; i < 10; i++ )
  36.     {
  37.         if( (p[i] = (int *)calloc( getrandom( 1, 10000 ),
  38.                                    sizeof( int ) )) == NULL )
  39.         {
  40.             --i;
  41.             break;
  42.         }
  43.         printf( "Allocated %u at %p\n", _msize( p[i] ), (void far *)p[i] );
  44.     }
  45.  
  46.     /* Fill all free blocks with the test character. */
  47.     heapstat( _heapset( 254 ) );
  48.  
  49.     /* In a real program, you might do operations here on the allocated
  50.      * buffers. Then do heapdump to make sure none of the operations wrote
  51.      * to free blocks.
  52.      */
  53.     heapdump( 254 );
  54.  
  55.     /* Do some more heap operations. */
  56.     for( ; i >= 0; i-- )
  57.     {
  58.         free( p[i] );
  59.         printf( "Deallocating %u at %p\n", _msize( p[i] ), (void far *)p[i] );
  60.     }
  61.  
  62.     /* Check heap again. */
  63.     heapdump( 254 );
  64. }
  65.  
  66. /* Test routine to check each block in the heap. */
  67. void heapdump( char fill )
  68. {
  69.     struct _heapinfo hi;
  70.     int heapstatus, i;
  71.     char far *p;
  72.  
  73.     /* Walk through entries, displaying results and checking free blocks. */
  74.     printf( "\nHeap dump:\n" );
  75.     hi._pentry = NULL;
  76.     while( (heapstatus = _heapwalk( &hi )) == _HEAPOK )
  77.     {
  78.         printf( "\n\t%s block at %p of size %u\t",
  79.                 hi._useflag == _USEDENTRY ? "USED" : "FREE",
  80.                 hi._pentry, hi._size );
  81.  
  82.         /* For free entries, check each byte to see that it still has
  83.          * only the fill character.
  84.          */
  85.         if( hi._useflag != _USEDENTRY )
  86.         {
  87.             for( p = (char far *)hi._pentry, i = 0; i < hi._size; p++, i++ )
  88.                 if( (char)*p != fill )
  89.                     break;
  90.             if( i == hi._size )
  91.                 printf( "Not changed" );
  92.             else
  93.                 printf( "Changed" );
  94.         }
  95.     }
  96.     heapstat( heapstatus );
  97. }
  98.  
  99. /* Report on the status returned by _heapwalk, _heapset, or _heapchk. */
  100. void heapstat( int status )
  101. {
  102.     printf( "\nHeap status: " );
  103.     switch( status )
  104.     {
  105.         case _HEAPOK:
  106.             printf( "OK - heap is fine" );
  107.             break;
  108.         case _HEAPEMPTY:
  109.             printf( "OK - empty heap" );
  110.             break;
  111.         case _HEAPEND:
  112.             printf( "OK - end of heap" );
  113.             break;
  114.         case _HEAPBADPTR:
  115.             printf( "ERROR - bad pointer to heap" );
  116.             break;
  117.         case _HEAPBADBEGIN:
  118.             printf( "ERROR - bad start of heap" );
  119.             break;
  120.         case _HEAPBADNODE:
  121.             printf( "ERROR - bad node in heap" );
  122.             break;
  123.     }
  124.     printf( "\n\n" );
  125. }
  126.