Leaked source code of windows server 2003
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.

218 lines
6.0 KiB

  1. /***
  2. *msize.c - calculate the size of a memory block in the heap
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines the following function:
  8. * _msize() - calculate the size of a block in the heap
  9. *
  10. *Revision History:
  11. * 07-18-89 GJF Module created
  12. * 11-13-89 GJF Added MTHREAD support. Also fixed copyright and got
  13. * rid of DEBUG286 stuff.
  14. * 12-18-89 GJF Changed name of header file to heap.h, also added
  15. * explicit _cdecl to function definitions.
  16. * 03-11-90 GJF Replaced _cdecl with _CALLTYPE1 and added #include
  17. * <cruntime.h>
  18. * 07-30-90 SBM Added return statement to MTHREAD _msize function
  19. * 09-28-90 GJF New-style function declarators.
  20. * 04-08-91 GJF Temporary hack for Win32/DOS folks - special version
  21. * of _msize that calls HeapSize. Change conditioned on
  22. * _WIN32DOS_.
  23. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  24. * 09-06-94 CFW Replace MTHREAD with _MT.
  25. * 11-03-94 CFW Debug heap support.
  26. * 12-01-94 CFW Simplify debug interface.
  27. * 02-01-95 GJF #ifdef out the *_base names for the Mac builds
  28. * (temporary).
  29. * 02-09-95 GJF Restored *_base names.
  30. * 05-01-95 GJF Spliced on winheap version.
  31. * 03-05-96 GJF Added support for small-block heap.
  32. * 04-10-96 GJF Return type of __sbh_find_block changed to __map_t *.
  33. * 05-30-96 GJF Minor changes for latest version of small-block heap.
  34. * 05-22-97 RDK New small-block heap scheme implemented.
  35. * 09-26-97 BWT Fix POSIX
  36. * 11-04-97 GJF Changed occurences of pBlock to pblock (in POSIX
  37. * support).
  38. * 12-17-97 GJF Exception-safe locking.
  39. * 09-30-98 GJF Bypass all small-block heap code when __sbh_initialized
  40. * is 0.
  41. * 11-16-98 GJF Merged in VC++ 5.0 version of small-block heap.
  42. * 05-01-99 PML Disable small-block heap for Win64.
  43. * 06-22-99 GJF Removed old small-block heap from static libs.
  44. *
  45. *******************************************************************************/
  46. #ifdef WINHEAP
  47. #include <cruntime.h>
  48. #include <malloc.h>
  49. #include <mtdll.h>
  50. #include <winheap.h>
  51. #include <windows.h>
  52. #include <dbgint.h>
  53. /***
  54. *size_t _msize(pblock) - calculate the size of specified block in the heap
  55. *
  56. *Purpose:
  57. * Calculates the size of memory block (in the heap) pointed to by
  58. * pblock.
  59. *
  60. *Entry:
  61. * void *pblock - pointer to a memory block in the heap
  62. *
  63. *Return:
  64. * size of the block
  65. *
  66. *******************************************************************************/
  67. size_t __cdecl _msize_base (void * pblock)
  68. {
  69. #ifdef _POSIX_
  70. return (size_t) HeapSize( _crtheap, 0, pblock );
  71. #else
  72. size_t retval;
  73. #ifdef HEAPHOOK
  74. if (_heaphook)
  75. {
  76. size_t size;
  77. if ((*_heaphook)(_HEAP_MSIZE, 0, pblock, (void *)&size))
  78. return size;
  79. }
  80. #endif /* HEAPHOOK */
  81. #ifndef _WIN64
  82. if ( __active_heap == __V6_HEAP )
  83. {
  84. PHEADER pHeader;
  85. #ifdef _MT
  86. _mlock( _HEAP_LOCK );
  87. __try {
  88. #endif
  89. if ((pHeader = __sbh_find_block(pblock)) != NULL)
  90. retval = (size_t)
  91. (((PENTRY)((char *)pblock - sizeof(int)))->sizeFront - 0x9);
  92. #ifdef _MT
  93. }
  94. __finally {
  95. _munlock( _HEAP_LOCK );
  96. }
  97. #endif
  98. if ( pHeader == NULL )
  99. retval = (size_t)HeapSize(_crtheap, 0, pblock);
  100. }
  101. #ifdef CRTDLL
  102. else if ( __active_heap == __V5_HEAP )
  103. {
  104. __old_sbh_region_t *preg;
  105. __old_sbh_page_t * ppage;
  106. __old_page_map_t * pmap;
  107. #ifdef _MT
  108. _mlock(_HEAP_LOCK);
  109. __try {
  110. #endif
  111. if ( (pmap = __old_sbh_find_block(pblock, &preg, &ppage)) != NULL )
  112. retval = ((size_t)(*pmap)) << _OLD_PARASHIFT;
  113. #ifdef _MT
  114. }
  115. __finally {
  116. _munlock( _HEAP_LOCK );
  117. }
  118. #endif
  119. if ( pmap == NULL )
  120. retval = (size_t) HeapSize( _crtheap, 0, pblock );
  121. }
  122. #endif
  123. else /* __active_heap == __SYSTEM_HEAP */
  124. #endif /* ndef _WIN64 */
  125. {
  126. retval = (size_t)HeapSize(_crtheap, 0, pblock);
  127. }
  128. return retval;
  129. #endif /* _POSIX_ */
  130. }
  131. #else /* ndef WINHEAP */
  132. #include <cruntime.h>
  133. #include <heap.h>
  134. #include <malloc.h>
  135. #include <mtdll.h>
  136. #include <stdlib.h>
  137. #include <dbgint.h>
  138. /***
  139. *size_t _msize(pblock) - calculate the size of specified block in the heap
  140. *
  141. *Purpose:
  142. * Calculates the size of memory block (in the heap) pointed to by
  143. * pblock.
  144. *
  145. *Entry:
  146. * void *pblock - pointer to a memory block in the heap
  147. *
  148. *Return:
  149. * size of the block
  150. *
  151. *******************************************************************************/
  152. #ifdef _MT
  153. size_t __cdecl _msize_base (
  154. void *pblock
  155. )
  156. {
  157. size_t retval;
  158. /* lock the heap
  159. */
  160. _mlock(_HEAP_LOCK);
  161. retval = _msize_lk(pblock);
  162. /* release the heap lock
  163. */
  164. _munlock(_HEAP_LOCK);
  165. return retval;
  166. }
  167. size_t __cdecl _msize_lk (
  168. #else /* ndef _MT */
  169. size_t __cdecl _msize_base (
  170. #endif /* _MT */
  171. void *pblock
  172. )
  173. {
  174. #ifdef HEAPHOOK
  175. if (_heaphook) {
  176. size_t size;
  177. if ((*_heaphook)(_HEAP_MSIZE, 0, pblock, (void *)&size))
  178. return size;
  179. }
  180. #endif /* HEAPHOOK */
  181. #ifdef DEBUG
  182. if (!_CHECK_BACKPTR(pblock))
  183. _heap_abort();
  184. #endif
  185. return( (size_t) ((char *)_ADDRESS(_BACKPTR(pblock)->pnextdesc) -
  186. (char *)pblock) );
  187. }
  188. #endif /* WINHEAP */