home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / dtx9203 / borhot / heapsz.c next >
C/C++ Source or Header  |  1992-03-25  |  2KB  |  56 lines

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