home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / heapused.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  149 lines

  1. /***
  2. *heapused.c -
  3. *
  4. *   Copyright (c) 1993-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. *
  8. *******************************************************************************/
  9.  
  10.  
  11. #ifdef WINHEAP
  12.  
  13.  
  14. #include <cruntime.h>
  15. #include <malloc.h>
  16. #include <errno.h>
  17.  
  18. size_t __cdecl _heapused(
  19.         size_t *pUsed,
  20.         size_t *pCommit
  21.         )
  22. {
  23.         errno = ENOSYS;
  24.         return( 0 );
  25. }
  26.  
  27.  
  28. #else  /* WINHEAP */
  29.  
  30.  
  31. #include <cruntime.h>
  32. #include <mtdll.h>  /* needed for _heapused() */
  33. #include <oscalls.h>
  34. #include <dos.h>
  35. #include <heap.h>
  36. #include <stddef.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39.  
  40. #ifdef _MT
  41.  
  42. size_t __cdecl _heapused(size_t *pUsed, size_t *pCommit)
  43. {
  44.     size_t retval;
  45.  
  46.     /* lock the heap */
  47.     _mlock(_HEAP_LOCK);
  48.  
  49.     retval = _heapused_lk(pUsed, pCommit);
  50.  
  51.     /* release the heap lock */
  52.     _munlock(_HEAP_LOCK);
  53.  
  54.     return retval;
  55. }
  56.  
  57. size_t __cdecl _heapused_lk(size_t *pUsed, size_t *pCommit)
  58.  
  59. #else  /* _MT */
  60.  
  61. _CRTIMP size_t __cdecl _heapused(size_t *pUsed, size_t *pCommit)
  62.  
  63. #endif  /* _MT */
  64. {
  65.     _PBLKDESC p;
  66.     _PBLKDESC next;
  67.     int index ;
  68.     size_t usedbytes;   /* bytes devoted to in-use blocks */
  69.     size_t freebytes;   /* bytes devoted to free blocks */
  70.     size_t rsrvbytes;   /* total bytes of reserved address space */
  71.     void * * pageptr ;
  72.  
  73.     if ( (p = _heap_desc.pfirstdesc) == NULL
  74.       || _heap_desc.pfirstdesc == &_heap_desc.sentinel )
  75.     {
  76.         return 0 ;  /* invalid heap */
  77.     }
  78.  
  79.     /*
  80.      * Scan through the heap, counting free and used blocks.
  81.      * Include the overhead of each block and its heap descriptor.
  82.      */
  83.  
  84.     freebytes = 0 ;
  85.     usedbytes = 0 ;
  86.  
  87.     while (p != NULL)
  88.     {
  89.  
  90.         next = p->pnextdesc;
  91.  
  92.         if (p == &_heap_desc.sentinel)
  93.         {
  94.             if (next != NULL)
  95.             {
  96.                 return 0 ;
  97.             }
  98.         }
  99.         else if (_IS_FREE(p))
  100.         {
  101.             freebytes += _BLKSIZE(p) + _HDRSIZE;
  102.         }
  103.         else if (_IS_INUSE(p))
  104.         {
  105.             usedbytes += _BLKSIZE(p) + _HDRSIZE;
  106.         }
  107.  
  108.         p = next;
  109.     }
  110.  
  111.     /*
  112.      * Now we need to count the pages used for descriptors as reserved memory.
  113.      * _heap_descpages points to the head of a singly-linked list of the pages.
  114.      * The descriptors for in-use blocks are considered in-use memory.
  115.      */
  116.  
  117.     pageptr = _heap_descpages;
  118.  
  119.     rsrvbytes = 0 ;
  120.  
  121.     while ( pageptr )
  122.     {
  123.         rsrvbytes += _HEAP_EMPTYLIST_SIZE ;
  124.         pageptr = * pageptr ;
  125.     }
  126.  
  127.     usedbytes += rsrvbytes ;
  128.  
  129.     /*
  130.      * Loop through the region descriptor table
  131.      */
  132.  
  133.     for ( index=0 ; index < _HEAP_REGIONMAX ; index++ )
  134.     {
  135.         rsrvbytes += _heap_regions[index]._totalsize ;
  136.     }
  137.  
  138.     if ( pUsed )
  139.         * pUsed = usedbytes ;
  140.  
  141.     if ( pCommit )
  142.         * pCommit = freebytes + usedbytes ;
  143.  
  144.     return rsrvbytes ;
  145. }
  146.  
  147.  
  148. #endif  /* WINHEAP */
  149.