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.

278 lines
9.3 KiB

  1. /***
  2. *malloc.c - Get a block of memory from the heap
  3. *
  4. * Copyright (c) 1989-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Defines the malloc() function.
  8. *
  9. *Revision History:
  10. * 06-29-89 GJF Module created (no rest for the wicked).
  11. * 07-07-89 GJF Several bug fixes
  12. * 07-21-89 GJF Added code to maintain proverdesc such that proverdesc
  13. * either points to the descriptor for the first free
  14. * block in the heap or, if there are no free blocks, is
  15. * the same as plastdesc.
  16. * 11-07-89 GJF Substantially revised to cope with 'tiling'.
  17. * 11-09-89 GJF Embarrassing bug (didn't bother to assign pdesc)
  18. * 11-10-89 GJF Added MTHREAD support.
  19. * 11-17-89 GJF Oops, must call _free_lk() instead of free()!
  20. * 12-18-89 GJF Changed name of header file to heap.h, also added
  21. * explicit _cdecl to function definitions.
  22. * 12-19-89 GJF Got rid of plastdesc from _heap_split_block() and
  23. * _ heap_advance_rover().
  24. * 03-11-90 GJF Replaced _cdecl with _CALLTYPE1, added #include
  25. * <cruntime.h> and removed #include <register.h>.
  26. * 07-25-90 SBM Replaced <stdio.h> by <stddef.h>, replaced
  27. * <assertm.h> by <assert.h>
  28. * 09-28-90 GJF New-style function declarators.
  29. * 02-26-91 SRW Optimize heap rover for _WIN32_.
  30. * 03-07-91 FAR Fix bug in heap rover
  31. * 03-11-91 FAR REALLY Fix bug in heap rover
  32. * 03-14-91 GJF Changed strategy for rover - old version available
  33. * by #define-ing _OLDROVER_.
  34. * 04-05-91 GJF Temporary hack for Win32/DOS folks - special version
  35. * of malloc that just calls HeapAlloc. Change conditioned
  36. * on _WIN32DOS_.
  37. * 05-28-91 GJF Removed M_I386 conditionals and put in _CRUISER_
  38. * conditionals so the 'tiling' version is built only for
  39. * Cruiser.
  40. * 03-03-93 SKS Add new handler support (_pnhHeap and related code)
  41. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  42. * 09-22-93 GJF Turned on range check. Also, removed old Cruiser
  43. * support.
  44. * 12-09-93 GJF Replace 4 (bytes) with _GRANULARITY.
  45. * 02-16-94 SKS Move set_new_handler support to new() in new.cxx
  46. * 03-01-94 SKS Declare _pnhHeap, pointer to the new handler, here
  47. * 03-02-94 GJF Changed logic of, and interface to, _heap_split_block
  48. * so that it may fail for lack of a free descriptor.
  49. * Also, changed malloc() so that the too-large block is
  50. * returned to the caller when _heap_split_block fails.
  51. * 03-03-94 SKS Reimplement new() handling within malloc - new scheme
  52. * allows the new() handler to be called optionally on
  53. * malloc failures. _set_new_mode(1) enables the option.
  54. * 03-04-94 SKS Rename _nhMode to _newmode, move it to its own module.
  55. * 03-02-94 GJF Changed logic of, and interface to, _heap_split_block
  56. * so that it may fail for lack of a free descriptor.
  57. * Also, changed malloc() so that the too-large block is
  58. * returned to the caller when _heap_split_block fails.
  59. * 04-01-94 GJF Made definition of _pnhHeap and declaration of
  60. * _newmode conditional on DLL_FOR_WIN32S.
  61. * 11-03-94 CFW Debug heap support.
  62. * 12-01-94 CFW Simplify debug interface.
  63. * 02-06-95 CFW assert -> _ASSERTE.
  64. * 02-07-95 GJF Deleted _OLDROVER_ code (obsolete). Also made some
  65. * temporary, conditional changes so this file can be
  66. * used in the MAC builds.
  67. * 02-09-95 GJF Restored *_base names.
  68. * 05-01-95 GJF Spliced on winheap version.
  69. * 05-24-95 CFW Official ANSI C++ new handler added.
  70. * 06-23-95 CFW ANSI new handler removed, fix block size check.
  71. * 03-01-96 GJF Added support for small-block heap. Also, the case
  72. * where size is too big now gets passed to the
  73. * new-handler.
  74. * 05-22-97 RDK New small-block heap scheme implemented.
  75. * 09-26-97 BWT Fix POSIX
  76. * 10-03-97 JWM Removed superfluous "#endif * _POSIX *"
  77. * 11-04-97 GJF Small POSIX fixes from Roger Lanser.
  78. * 12-17-97 GJF Exception-safe locking.
  79. * 05-22-98 JWM Support for KFrei's RTC work.
  80. * 07-28-98 JWM RTC update.
  81. * 09-30-98 GJF Bypass all small-block heap code when __sbh_initialized
  82. * is 0.
  83. * 11-16-98 GJF Merged in VC++ 5.0 version of small-block heap.
  84. * 12-18-98 GJF Changes for 64-bit size_t.
  85. * 05-01-99 PML Disable small-block heap for Win64.
  86. * 05-13-99 PML Remove Win32s
  87. * 05-26-99 KBF Updated RTC hook func params
  88. * 06-22-99 GJF Removed old small-block heap from static libs.
  89. * 04-28-00 BWT Fix Posix
  90. * 08-04-00 PML Don't round allocation sizes when using system
  91. * heap (VS7#131005).
  92. * 02-19-02 BWT Remove posix and !winheap code. Collapse code
  93. * to a single routine to assist with watson dumps.
  94. *
  95. *******************************************************************************/
  96. #include <cruntime.h>
  97. #include <malloc.h>
  98. #include <internal.h>
  99. #include <mtdll.h>
  100. #include <dbgint.h>
  101. #include <windows.h>
  102. #include <winheap.h>
  103. #include <rtcsup.h>
  104. extern int _newmode; /* malloc new() handler mode */
  105. #ifndef _WIN64
  106. void *V6_HeapAlloc(size_t size)
  107. {
  108. void *pvReturn = NULL;
  109. if ( size <= __sbh_threshold )
  110. {
  111. #ifdef _MT
  112. _mlock( _HEAP_LOCK );
  113. __try {
  114. #endif
  115. pvReturn = __sbh_alloc_block((int)size);
  116. #ifdef _MT
  117. }
  118. __finally {
  119. _munlock( _HEAP_LOCK );
  120. }
  121. #endif
  122. }
  123. return pvReturn;
  124. }
  125. #ifdef CRTDLL
  126. void *V5_HeapAlloc(size_t size)
  127. {
  128. void * pvReturn = NULL;
  129. /* round up to the nearest paragraph */
  130. if ( size )
  131. size = (size + _OLD_PARASIZE - 1) & ~(_OLD_PARASIZE - 1);
  132. else
  133. size = _OLD_PARASIZE;
  134. if ( size <= __old_sbh_threshold ) {
  135. #ifdef _MT
  136. _mlock(_HEAP_LOCK);
  137. __try {
  138. #endif
  139. pvReturn = __old_sbh_alloc_block(size >> _OLD_PARASHIFT);
  140. #ifdef _MT
  141. }
  142. __finally {
  143. _munlock(_HEAP_LOCK);
  144. }
  145. #endif
  146. }
  147. return pvReturn;
  148. }
  149. #endif /* CRTDLL */
  150. #endif /* WIN64 */
  151. #ifdef _DEBUG
  152. #define _heap_alloc _heap_alloc_base
  153. #endif
  154. /***
  155. *void *_heap_alloc_base(size_t size) - does actual allocation
  156. *
  157. *Purpose:
  158. * Same as malloc() except the new handler is not called.
  159. *
  160. *Entry:
  161. * See malloc
  162. *
  163. *Exit:
  164. * See malloc
  165. *
  166. *Exceptions:
  167. *
  168. *******************************************************************************/
  169. __forceinline void * __cdecl _heap_alloc (size_t size)
  170. {
  171. #ifdef HEAPHOOK
  172. // call heap hook if installed
  173. if (_heaphook)
  174. {
  175. void * pvReturn;
  176. if ((*_heaphook)(_HEAP_MALLOC, size, NULL, (void *)&pvReturn))
  177. return pvReturn;
  178. }
  179. #endif /* HEAPHOOK */
  180. #ifdef _WIN64
  181. return HeapAlloc(_crtheap, 0, size ? size : 1);
  182. #else
  183. void *pvReturn;
  184. if (__active_heap == __SYSTEM_HEAP) {
  185. return HeapAlloc(_crtheap, 0, size ? size : 1);
  186. } else
  187. if ( __active_heap == __V6_HEAP ) {
  188. if (pvReturn = V6_HeapAlloc(size)) {
  189. return pvReturn;
  190. }
  191. }
  192. #ifdef CRTDLL
  193. else if ( __active_heap == __V5_HEAP )
  194. {
  195. if (pvReturn = V5_HeapAlloc(size)) {
  196. return pvReturn;
  197. }
  198. }
  199. #endif /* CRTDLL */
  200. if (size == 0)
  201. size = 1;
  202. size = (size + BYTES_PER_PARA - 1) & ~(BYTES_PER_PARA - 1);
  203. return HeapAlloc(_crtheap, 0, size);
  204. #endif /* ndef _WIN64 */
  205. }
  206. /***
  207. *void *malloc(size_t size) - Get a block of memory from the heap
  208. *
  209. *Purpose:
  210. * Allocate of block of memory of at least size bytes from the heap and
  211. * return a pointer to it.
  212. *
  213. * Calls the new appropriate new handler (if installed).
  214. *
  215. *Entry:
  216. * size_t size - size of block requested
  217. *
  218. *Exit:
  219. * Success: Pointer to memory block
  220. * Failure: NULL (or some error value)
  221. *
  222. *Uses:
  223. *
  224. *Exceptions:
  225. *
  226. *******************************************************************************/
  227. void * __cdecl _malloc_base (size_t size)
  228. {
  229. void *res = NULL;
  230. // validate size
  231. if (size > _HEAP_MAXREQ) {
  232. goto Cleanup;
  233. }
  234. for (;;) {
  235. // allocate memory block
  236. res = _heap_alloc(size);
  237. // if successful allocation, return pointer to memory
  238. // if new handling turned off altogether, return NULL
  239. if (res || _newmode == 0)
  240. break;
  241. // call installed new handler
  242. if (!_callnewh(size))
  243. break;
  244. // new handler was successful -- try to allocate again
  245. }
  246. Cleanup:
  247. RTCCALLBACK(_RTC_Allocate_hook, (res, size, 0));
  248. return res;
  249. }