home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / c / library / dos / diverses / tctnt / heapsz.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-27  |  1.4 KB  |  53 lines

  1. /* HEAPSZ.C: Function to Find Total Far Heap Free Memory
  2.  
  3.         This function will determine how much memory is available on
  4.         the far heap.
  5.  
  6.         Usage: To use this function you must put the statement:
  7.  
  8.                                                 extern long heapsize(void);
  9.  
  10.                                         within the scope of the calling function.
  11.  
  12.         Method: The memory available on the far heap is computed by
  13.                         determining the sum of the freed blocks of memory and
  14.                         adding that amount to the value returned by coreleft().
  15.  
  16.         Input:  None.
  17. */
  18.  
  19. #include <alloc.h>          // for heapinfo, heapcheck(), heapwalk(),
  20.                                                 // coreleft() and HEAPOK
  21. #include <stdio.h>            // for printf()
  22.  
  23. unsigned long heapsize(void) {
  24.     unsigned long i = 0;       // Return variable
  25.     struct heapinfo hi;        // Value returned from farheapwalk
  26.     hi.ptr = NULL;             // Set the beginning of farheapwalk
  27.  
  28.     if( heapcheck() <= 0 )     // Check for corrupted heap
  29.         return (-1L);
  30.  
  31.     // Walk the heap and sum size of free blocks
  32.     while( heapwalk(&hi) == _HEAPOK )
  33.         if( hi.in_use == 0 )
  34.             i += hi.size;
  35.  
  36.  
  37.     // Check for amount of free memory from the highest allocated
  38.   // to the end of DOS
  39.     i += coreleft();
  40.  
  41.   return i;
  42. } // end of heapsize()
  43.  
  44. //*******************************************************************
  45. int main()
  46. {
  47.     unsigned long theap;
  48.     theap = heapsize();
  49.     printf("Heap available = %lu.\n", theap);
  50.   return 0;
  51. } // end of main()
  52.  
  53.