Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

156 lines
3.1 KiB

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