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.

476 lines
16 KiB

  1. /***
  2. *heap.h - Heap code include file
  3. *
  4. * Copyright (c) 1988-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Contains information needed by the C library heap code.
  8. *
  9. * [Internal]
  10. *
  11. *Revision History:
  12. * 05-16-89 JCR Module created
  13. * 06-02-89 GJF Removed naming conflict
  14. * 06-29-89 JCR Completely new for "New heap - rev 2"
  15. * 06-29-89 GJF Added _HDRSIZE, fixed some minor glitches.
  16. * 06-29-89 GJF Added _BLKSIZE(), fixed more minor bugs.
  17. * 06-30-89 GJF Changed several macros to operate on a pointer to a
  18. * descriptor, rather than a descriptor itself.
  19. * 06-30-89 JCR Corrected/updated several macros
  20. * 07-06-89 JCR Added region support, misc improvements, etc.
  21. * 07-07-89 GJF Minor bug in _ROUND() macro
  22. * 07-07-89 JCR Added _DUMMY status
  23. * 07-19-89 GJF Removed _PBACKPTR macro
  24. * 07-20-89 JCR Region routine prototypes, _HEAPFIND values
  25. * 07-21-89 JCR #define _heap_growsize to _amblksiz for compatibility
  26. * 07-25-89 GJF Added prototypes for calloc, free and malloc
  27. * 07-28-89 GJF Added prototype for _msize
  28. * 08-28-89 JCR Added _HEAP_COALESCE value
  29. * 10-30-89 GJF Fixed copyright
  30. * 11-03-89 GJF Added _DISTTOBNDRY(), _NEXTSEGBNDRY() macros and
  31. * prototypes for _flat_malloc(), _heap_advance_rover(),
  32. * _heap_split_block() functions
  33. * 11-07-89 GJF Added _SEGSIZE_, added prototype for _heap_search()
  34. * restored function prototype for_heap_grow_region()
  35. * 11-08-89 JCR Added non-pow2 rounding macro
  36. * 11-10-89 JCR Added _heap_free_region prototype
  37. * 11-10-89 GJF Added prototypes and macros for multi-thread support
  38. * 11-16-89 JCR If DEBUG defined include <assert.h>, added sanity check
  39. * 12-13-89 GJF Removed prototypes duplicated in malloc.h
  40. * 12-20-89 GJF Removed plastdesc from _heap_desc_ struct, removed
  41. * _DELHEAP and _ADDHEAP macros (unused and wrong), added
  42. * explicit _cdecl to function prototypes
  43. * 01-08-89 GJF Use assert macro from assertm.h instead of assert.h
  44. * 03-01-90 GJF Added #ifndef _INC_HEAP and #include <cruntime.h>
  45. * stuff. Also, removed some unused DEBUG286 stuff.
  46. * 03-22-90 GJF Replaced _cdecl with _CALLTYPE1 in prototypes.
  47. * 07-25-90 SBM Replaced <assertm.h> by <assert.h>
  48. * 08-13-90 SBM Added casts to macros for clean compiles at -W3
  49. * 12-28-90 SRW Fixed _heap_split_block prototype to match code
  50. * 12-28-90 SRW Changed _HEAP_GROWSIZE to be 0x10000 [_WIN32_]
  51. * 03-05-91 GJF Added decl for _heap_resetsize, removed proto for
  52. * _heap_advance_rover (both conditioned on _OLDROVER_
  53. * not being #define-d).
  54. * 03-13-91 GJF Made _HEAP_GROWSIZE 32K for [_CRUISER_].
  55. * 04-09-91 PNT Added _MAC_ definitions
  56. * 08-20-91 JCR C++ and ANSI naming
  57. * 03-30-92 DJM POSIX support.
  58. * 08-06-92 GJF Function calling type and variable type macros.
  59. * 01-21-93 GJF Removed support for C6-386's _cdecl.
  60. * 04-06-93 SKS Replace _CRTAPI1/2 with __cdecl, _CRTVAR1 with nothing
  61. * 04-16-93 SKS Change _HEAP_REGIONSIZE from 4 MB to 16 KB.
  62. * Low memory environments such as Win32S cannot commit
  63. * memory without actually allocating it.
  64. * 04-20-93 SKS Add new constant _HEAP_MAXREGIONSIZE.
  65. * 04-26-93 SKS Constants _HEAP_REGIONSIZE and _HEAP_MAXREGIONSIZE
  66. * split to handle both small and large memory modes
  67. * _HEAP_MAXREGIONSIZE becomes variable "_heap_maxregsize"
  68. * 09-01-93 GJF Merged NT SDK and Cuda version (this include file is
  69. * not used for the winheap\*.* heap manager).
  70. * 12-09-93 GJF Added _GRANULARITY and defined _HDRSIZE in terms of it.
  71. * 12-13-93 SKS Add _heapused_lk(). Move here declarations of
  72. * _heap_descpages and _HEAP_EMPTYLIST_SIZE.
  73. * 03-02-94 GJF Deleted _GETEMPTY() macro and prototype for
  74. * _heap_grow_emptylist() (now static). Added prototype
  75. * for __getempty(). Changed _heap_split_block() return
  76. * type.
  77. * 03-31-94 GJF Made declarations of:
  78. * _heap_descpages
  79. * _heap_maxregsize
  80. * _heap_regions
  81. * _heap_regionsize
  82. * _heap_resetsize
  83. * conditional on ndef DLL_FOR_WIN32S. Also, made the
  84. * definition of _heap_growsize conditional in the same
  85. * manner and conditionally include win32s.h.
  86. * 07-29-94 GJF Page are twice as big on the DEC Alpha.
  87. * 08-04-94 GJF DEC Alpha needs 8 byte alignment too.
  88. * 09-06-94 CFW Remove _CRTHEAP_ switch.
  89. * 09-21-94 SKS Fix typo: no leading _ on "DLL_FOR_WIN32S"
  90. * 10-09-94 BWT PPC changes from John Morgan.
  91. * 11-02-94 SKS Change _HEAP_MAXREGIONSIZE_S from 256 KB to 16 MB
  92. * (same as _HEAP_MAXREGIONSIZE_L) for users who want a
  93. * LOT of memory under Win32s. This is reasonable now
  94. * that Win32s supports reserved but uncommitted memory.
  95. * 12-05-94 CFW Fix debug new handler support.
  96. * 02-06-95 GJF Removed some of the Mac-merge changes.
  97. * 02-06-95 CFW Remove assert.h, DEBUG -> _DEBUG
  98. * 02-14-95 CFW Clean up Mac merge.
  99. * 03-29-95 CFW Add error message to internal headers.
  100. * 04-24-95 CFW Add heap hook.
  101. * 12-14-95 JWM Add "#pragma once".
  102. * 04-23-96 SKS Return type of _heap_init changed from void to int.
  103. * 02-21-97 GJF Cleaned out obsolete support for DLL_FOR_WIN32S.
  104. * Replaced defined(_M_MPPC) || defined(_M_M68K) with
  105. * defined(_MAC). Also, detab-ed.
  106. * 10-07-97 RDL Added IA64.
  107. * 05-17-99 PML Remove all Macintosh support.
  108. * 07-07-99 AWB Changes for 64-bit size_t. (v-aborni)
  109. * 10-06-99 PML Add _W64 modifier to types which are 32 bits in Win32,
  110. * 64 bits in Win64.
  111. * 07-15-01 PML Remove all ALPHA, MIPS, and PPC code
  112. *
  113. ****/
  114. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  115. #pragma once
  116. #endif
  117. #ifndef _INC_HEAP
  118. #define _INC_HEAP
  119. #ifndef _CRTBLD
  120. /*
  121. * This is an internal C runtime header file. It is used when building
  122. * the C runtimes only. It is not to be used as a public header file.
  123. */
  124. #error ERROR: Use of C runtime library internal header file.
  125. #endif /* _CRTBLD */
  126. #ifdef __cplusplus
  127. extern "C" {
  128. #endif
  129. #include <cruntime.h>
  130. /* Define __cdecl for non-Microsoft compilers */
  131. #if ( !defined(_MSC_VER) && !defined(__cdecl) )
  132. #define __cdecl
  133. #endif
  134. /*
  135. * Heap block descriptor
  136. */
  137. struct _block_descriptor {
  138. struct _block_descriptor *pnextdesc; /* ptr to next descriptor */
  139. void *pblock; /* ptr to memory block */
  140. };
  141. #define _BLKDESC struct _block_descriptor
  142. #define _PBLKDESC struct _block_descriptor *
  143. /*
  144. * Useful constants
  145. */
  146. /*
  147. * Unit of allocation. All allocations are of size n * _GRANULARITY. Note
  148. * that _GRANULARITY must be a power of 2, or it cannot be used with the
  149. * _ROUND2 macro.
  150. */
  151. #if defined(_M_IA64)
  152. #define _GRANULARITY 8
  153. #else
  154. #define _GRANULARITY 4
  155. #endif
  156. /*
  157. * Size of the header in a memory block. Note that we must have
  158. * sizeof(void *) <= _HDRSIZE so the header is big enough to hold a pointer
  159. * to the descriptor.
  160. */
  161. #define _HDRSIZE 1 * _GRANULARITY
  162. /* _heapchk/_heapset parameter */
  163. #define _HEAP_NOFILL 0x7FFFFFF
  164. /*
  165. * Descriptor status values
  166. */
  167. #define _INUSE 0
  168. #define _FREE 1
  169. #define _DUMMY 2
  170. #if _INUSE != 0 /*IFSTRIP=IGN*/
  171. #error *** Heap code assumes _INUSE value is 0! ***
  172. #endif
  173. /*
  174. * Macros for manipulating heap memory block descriptors
  175. * stat = one of the status values
  176. * addr = user-visible address of a heap block
  177. */
  178. #define _STATUS_MASK 0x3 /* last 2 bits are status */
  179. #define _ADDRESS(pdesc) ( (void *) ((unsigned)((pdesc)->pblock) & \
  180. (~_STATUS_MASK)) )
  181. #define _STATUS(pdesc) ( (unsigned) ((unsigned)((pdesc)->pblock) & \
  182. _STATUS_MASK) )
  183. #define _SET_INUSE(pdesc) ( pdesc->pblock = (void *) \
  184. ((unsigned)_ADDRESS(pdesc) | _INUSE) )
  185. #define _SET_FREE(pdesc) ( pdesc->pblock = (void *) \
  186. ((unsigned)_ADDRESS(pdesc) | _FREE) )
  187. #define _SET_DUMMY(pdesc) ( pdesc->pblock = (void *) \
  188. ((unsigned)_ADDRESS(pdesc) | _DUMMY) )
  189. #define _IS_INUSE(pdesc) ( _STATUS(pdesc) == _INUSE )
  190. #define _IS_FREE(pdesc) ( _STATUS(pdesc) == _FREE )
  191. #define _IS_DUMMY(pdesc) ( _STATUS(pdesc) == _DUMMY )
  192. #define _BLKSIZE(pdesc) ( (unsigned) ( \
  193. (char *)_ADDRESS(pdesc->pnextdesc) - \
  194. (char *)_ADDRESS(pdesc) - _HDRSIZE ) )
  195. #define _MEMSIZE(pdesc) ( (char *)_ADDRESS(pdesc->pnextdesc) - \
  196. (char *)_ADDRESS(pdesc) )
  197. #define _BACKPTR(addr) ( *(_PBLKDESC*)((char *)(addr) - _HDRSIZE) )
  198. #define _CHECK_PDESC(pdesc) ( (*(_PBLKDESC*) (_ADDRESS(pdesc))) == pdesc )
  199. #define _CHECK_BACKPTR(addr) ( ((char *)(_BACKPTR(addr)->pblock) + _HDRSIZE) \
  200. == addr)
  201. /*
  202. * Heap descriptor
  203. */
  204. struct _heap_desc_ {
  205. _PBLKDESC pfirstdesc; /* pointer to first descriptor */
  206. _PBLKDESC proverdesc; /* rover pointer */
  207. _PBLKDESC emptylist; /* pointer to empty list */
  208. _BLKDESC sentinel; /* Sentinel block for end of heap list */
  209. };
  210. extern struct _heap_desc_ _heap_desc;
  211. /*
  212. * Region descriptor and heap grow data
  213. */
  214. struct _heap_region_ {
  215. void * _regbase; /* base address of region */
  216. unsigned _currsize; /* current size of region */
  217. unsigned _totalsize; /* total size of region */
  218. };
  219. #define _heap_growsize _amblksiz
  220. #ifndef _OLDROVER_
  221. extern unsigned int _heap_resetsize;
  222. #endif /* _OLDROVER_ */
  223. extern unsigned int _heap_regionsize;
  224. extern unsigned int _heap_maxregsize;
  225. extern struct _heap_region_ _heap_regions[];
  226. extern void ** _heap_descpages;
  227. #define _PAGESIZE_ 0x1000 /* one page */
  228. #define _SEGSIZE_ 0x10000 /* one segment (i.e., 64 Kb) */
  229. #define _HEAP_REGIONMAX 0x40 /* Max number of regions: 64 */
  230. /* For small memory systems: */
  231. #define _HEAP_REGIONSIZE_S 0x4000 /* Initial region size (16K) */
  232. #define _HEAP_MAXREGSIZE_S 0x1000000 /* Maximum region size (16M) */
  233. /* For large memory systems: */
  234. #define _HEAP_REGIONSIZE_L 0x100000 /* Initial region size (1M) */
  235. #define _HEAP_MAXREGSIZE_L 0x1000000 /* Maximum region size (16M) */
  236. #define _HEAP_GROWSIZE 0x10000 /* Default grow increment (64K) */
  237. #define _HEAP_GROWMIN _PAGESIZE_ /* Minimum grow inc (1 page) */
  238. #define _HEAP_GROWSTART _PAGESIZE_ /* Startup grow increment */
  239. #define _HEAP_COALESCE -1 /* Coalesce heap value */
  240. #define _HEAP_EMPTYLIST_SIZE (1 * _PAGESIZE_)
  241. /*
  242. * Values returned by _heap_findaddr() routine
  243. */
  244. #define _HEAPFIND_EXACT 0 /* found address exactly */
  245. #define _HEAPFIND_WITHIN 1 /* address is within a block */
  246. #define _HEAPFIND_BEFORE -1 /* address before beginning of heap */
  247. #define _HEAPFIND_AFTER -2 /* address after end of heap */
  248. #define _HEAPFIND_EMPTY -3 /* address not found: empty heap */
  249. /*
  250. * Arguments to _heap_param
  251. */
  252. #define _HP_GETPARAM 0 /* get heap parameter value */
  253. #define _HP_SETPARAM 1 /* set heap parameter value */
  254. #define _HP_AMBLKSIZ 1 /* get/set _amblksiz value (aka */
  255. #define _HP_GROWSIZE _HP_AMBLKSIZ /* _heap_growsize */
  256. #define _HP_RESETSIZE 2 /* get/set _heap_resetsize value */
  257. /*
  258. * Macros to round numbers
  259. *
  260. * _ROUND2 = rounds a number up to a power of 2
  261. * _ROUND = rounds a number up to any other numer
  262. *
  263. * n = number to be rounded
  264. * pow2 = must be a power of two value
  265. * r = any number
  266. */
  267. #define _ROUND2(n,pow2) \
  268. ( ( n + pow2 - 1) & ~(pow2 - 1) )
  269. #define _ROUND(n,r) \
  270. ( ( (n/r) + ((n%r)?1:0) ) * r)
  271. /*
  272. Macros for accessing heap descriptor lists:
  273. _PUTEMPTY(x) = Puts an empty heap desc on the empty list
  274. (x = _PBLKDESC = pointer to heap block descriptor)
  275. */
  276. #ifdef _DEBUG
  277. #define _PUTEMPTY(x) \
  278. { \
  279. (x)->pnextdesc = _heap_desc.emptylist; \
  280. \
  281. (x)->pblock = NULL; \
  282. \
  283. _heap_desc.emptylist = (x); \
  284. }
  285. #else
  286. #define _PUTEMPTY(x) \
  287. { \
  288. (x)->pnextdesc = _heap_desc.emptylist; \
  289. \
  290. _heap_desc.emptylist = (x); \
  291. }
  292. #endif
  293. /*
  294. * Macros for finding the next 64 Kb boundary from a pointer
  295. */
  296. #define _NXTSEGBNDRY(p) ((void *)((unsigned)(p) & 0xffff0000 + 0x10000))
  297. #define _DISTTOBNDRY(p) ((unsigned)(0x10000 - (0x0000ffff & (unsigned)(p))))
  298. /*
  299. * Define size_t type (if necessary)
  300. */
  301. #if !defined(_W64)
  302. #if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 /*IFSTRIP=IGN*/
  303. #define _W64 __w64
  304. #else
  305. #define _W64
  306. #endif
  307. #endif
  308. #ifndef _SIZE_T_DEFINED
  309. #ifdef _WIN64
  310. typedef unsigned __int64 size_t;
  311. #else
  312. typedef _W64 unsigned int size_t;
  313. #endif
  314. #define _SIZE_T_DEFINED
  315. #endif
  316. /*
  317. * Prototypes
  318. */
  319. void * __cdecl _nh_malloc(size_t, int);
  320. void * __cdecl _heap_alloc(size_t);
  321. void * __cdecl _flat_malloc(size_t);
  322. _PBLKDESC __getempty(void);
  323. void __cdecl _heap_abort(void);
  324. int __cdecl _heap_addblock(void *, unsigned int);
  325. #ifdef _OLDROVER_
  326. void __cdecl _heap_advance_rover(void);
  327. #endif /* _OLDROVER_ */
  328. void __cdecl _heap_free_region(int);
  329. int __cdecl _heap_findaddr(void *, _PBLKDESC *);
  330. int __cdecl _heap_grow(unsigned int);
  331. int __cdecl _heap_grow_region(unsigned, size_t);
  332. int __cdecl _heap_init(void);
  333. #ifndef _OLDROVER_
  334. int __cdecl _heap_param(int, int, void *);
  335. #endif /* _OLDROVER_ */
  336. _PBLKDESC __cdecl _heap_search(unsigned size);
  337. _PBLKDESC __cdecl _heap_split_block(_PBLKDESC, size_t);
  338. #ifdef _DEBUG
  339. void __cdecl _heap_print_all(void);
  340. void __cdecl _heap_print_regions(void);
  341. void __cdecl _heap_print_desc(void);
  342. void __cdecl _heap_print_emptylist(void);
  343. void __cdecl _heap_print_heaplist(void);
  344. #endif
  345. /*
  346. * Prototypes and macros for multi-thread support
  347. */
  348. #ifdef _MT
  349. void __cdecl _free_lk(void *);
  350. size_t __cdecl _msize_lk(void *);
  351. size_t __cdecl _heapused_lk(size_t *pUsed, size_t *pFree);
  352. #ifdef _DEBUG
  353. void __cdecl _heap_print_regions_lk(void);
  354. void __cdecl _heap_print_desc_lk(void);
  355. void __cdecl _heap_print_emptylist_lk(void);
  356. void __cdecl _heap_print_heaplist_lk(void);
  357. #endif
  358. #else /* ndef _MT */
  359. #define _free_lk(p) free(p)
  360. #define _msize_lk(p) _msize(p)
  361. #ifdef _DEBUG
  362. #define _heap_print_regions_lk() _heap_print_regions()
  363. #define _heap_print_desc_lk() _heap_print_desc()
  364. #define _heap_print_emptylist_lk() _heap_print_emptylist()
  365. #define _heap_print_heaplist_lk() _heap_print_heaplist()
  366. #endif
  367. #endif /* _MT */
  368. #ifdef HEAPHOOK
  369. #ifndef _HEAPHOOK_DEFINED
  370. /* hook function type */
  371. typedef int (__cdecl * _HEAPHOOK)(int, size_t, void *, void *);
  372. #define _HEAPHOOK_DEFINED
  373. #endif /* _HEAPHOOK_DEFINED */
  374. extern _HEAPHOOK _heaphook;
  375. #endif /* HEAPHOOK */
  376. #ifdef __cplusplus
  377. }
  378. #endif
  379. #endif /* _INC_HEAP */