home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / biology / gsrc208a.zip / HEAPCHK.C < prev    next >
C/C++ Source or Header  |  1993-08-17  |  3KB  |  141 lines

  1. #include "copyleft.h"
  2.  
  3. /*
  4.     GEPASI - a simulator of metabolic pathways and other dynamical systems
  5.     Copyright (C) 1989, 1992, 1993  Pedro Mendes
  6. */
  7.  
  8. /*************************************/
  9. /*                                   */
  10. /*             heap check            */
  11. /*                                   */
  12. /*            specific for           */
  13. /*          Visual C/C++ 1.0         */
  14. /*           QuickC/WIN 1.0          */
  15. /*                                   */
  16. /*   (include here compilers that    */
  17. /*   compiled GEPASI successfully)   */
  18. /*                                   */
  19. /*************************************/
  20.  
  21. #include <stdio.h>
  22. #include <malloc.h>
  23. #include <stdlib.h>
  24.  
  25. /*
  26.  USE THE FOLLOWING ON THE CODE TO CHECK THE HEAP
  27.  
  28.  o  heapstat( _heapchk() );
  29.     Check heap status. Should be OK at start of heap.
  30.  
  31.  o  heapstat( _heapset( 254 ) );
  32.     Fill all free blocks with the test character.
  33.  
  34.  o  heapvalidate( 254 );
  35.     Do heapvalidate to make sure none of the operations
  36.     wrote to free blocks.
  37.  
  38. */
  39.  
  40.  
  41. /* Tests each block in the heap. If a modified free block is found,
  42.  * returns it's address. Otherwise returns NULL.
  43.  */
  44.  
  45. #ifdef _MSC_VER
  46.  
  47. long int heap_check_counter = 0;
  48.  
  49. int __far *heapvalidate( char fill )
  50. {
  51.     struct _heapinfo hi;
  52.     unsigned heapstatus, i;
  53.     char __far *p;
  54.  
  55.     /* Walk through entries, checking free blocks. */
  56.     hi._pentry = NULL;
  57.     while( (heapstatus = _heapwalk( &hi )) == _HEAPOK )
  58.     {
  59.         /* For free entries, check each byte to see that it still has
  60.          * only the fill character. Return address if changed.
  61.          */
  62.         if( hi._useflag != _USEDENTRY )
  63.         {
  64.             for( p = (char __far *)hi._pentry, i = 0; i < hi._size; p++,i++ )
  65.                 if( (char)*p != fill )
  66.                 {
  67.                     printf( "%4ld Free entry at %Fp modified.\n", heap_check_counter++, hi._pentry );
  68.                     return hi._pentry;
  69.                 }
  70.         }
  71.     }
  72.     printf( "%4ld Free entries are unchanged.\n", heap_check_counter++ );
  73.     return NULL;
  74. }
  75.  
  76. /* Reports on the status returned by _heapwalk, _heapset, or _heapchk */
  77. void heapstat( int status )
  78. {
  79.     printf( "\n%4ld Heap status: ", heap_check_counter++ );
  80.     switch( status )
  81.     {
  82.         case _HEAPOK:
  83.             printf( "OK - heap is fine" );
  84.             break;
  85.         case _HEAPEMPTY:
  86.             printf( "OK - empty heap" );
  87.             break;
  88.         case _HEAPEND:
  89.             printf( "OK - end of heap" );
  90.             break;
  91.         case _HEAPBADPTR:
  92.             printf( "ERROR - bad pointer to heap" );
  93.             break;
  94.         case _HEAPBADBEGIN:
  95.             printf( "ERROR - bad start of heap" );
  96.             break;
  97.         case _HEAPBADNODE:
  98.             printf( "ERROR - bad node in heap" );
  99.             break;
  100.     }
  101.     printf( "\n" );
  102.  
  103. }
  104.  
  105. void heap_check( void )
  106. {
  107.  heapstat( _heapchk() );
  108. }
  109.  
  110. void heap_free_set( void )
  111. {
  112.  heapstat( _heapset( 254 ) );
  113. }
  114.  
  115. void heap_free_check( void )
  116. {
  117.  heapvalidate( (char) 254 );
  118. }
  119.  
  120. #endif
  121.  
  122.  
  123. #ifndef _MSC_VER
  124.  
  125. void heap_check( void )
  126. {
  127.  return;
  128. }
  129.  
  130. void heap_free_set( void )
  131. {
  132.  return;
  133. }
  134.  
  135. void heap_free_check( void )
  136. {
  137.  return;
  138. }
  139.  
  140. #endif
  141.