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.

479 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. *
  112. ****/
  113. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  114. #pragma once
  115. #endif
  116. #ifndef _INC_HEAP
  117. #define _INC_HEAP
  118. #ifndef _CRTBLD
  119. /*
  120. * This is an internal C runtime header file. It is used when building
  121. * the C runtimes only. It is not to be used as a public header file.
  122. */
  123. #error ERROR: Use of C runtime library internal header file.
  124. #endif /* _CRTBLD */
  125. #ifdef __cplusplus
  126. extern "C" {
  127. #endif
  128. #include <cruntime.h>
  129. /* Define __cdecl for non-Microsoft compilers */
  130. #if ( !defined(_MSC_VER) && !defined(__cdecl) )
  131. #define __cdecl
  132. #endif
  133. /*
  134. * Heap block descriptor
  135. */
  136. struct _block_descriptor {
  137. struct _block_descriptor *pnextdesc; /* ptr to next descriptor */
  138. void *pblock; /* ptr to memory block */
  139. };
  140. #define _BLKDESC struct _block_descriptor
  141. #define _PBLKDESC struct _block_descriptor *
  142. /*
  143. * Useful constants
  144. */
  145. /*
  146. * Unit of allocation. All allocations are of size n * _GRANULARITY. Note
  147. * that _GRANULARITY must be a power of 2, or it cannot be used with the
  148. * _ROUND2 macro.
  149. */
  150. #if defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC) || defined(_M_IA64)
  151. #define _GRANULARITY 8
  152. #else
  153. #define _GRANULARITY 4
  154. #endif
  155. /*
  156. * Size of the header in a memory block. Note that we must have
  157. * sizeof(void *) <= _HDRSIZE so the header is big enough to hold a pointer
  158. * to the descriptor.
  159. */
  160. #define _HDRSIZE 1 * _GRANULARITY
  161. /* _heapchk/_heapset parameter */
  162. #define _HEAP_NOFILL 0x7FFFFFF
  163. /*
  164. * Descriptor status values
  165. */
  166. #define _INUSE 0
  167. #define _FREE 1
  168. #define _DUMMY 2
  169. #if _INUSE != 0 /*IFSTRIP=IGN*/
  170. #error *** Heap code assumes _INUSE value is 0! ***
  171. #endif
  172. /*
  173. * Macros for manipulating heap memory block descriptors
  174. * stat = one of the status values
  175. * addr = user-visible address of a heap block
  176. */
  177. #define _STATUS_MASK 0x3 /* last 2 bits are status */
  178. #define _ADDRESS(pdesc) ( (void *) ((unsigned)((pdesc)->pblock) & \
  179. (~_STATUS_MASK)) )
  180. #define _STATUS(pdesc) ( (unsigned) ((unsigned)((pdesc)->pblock) & \
  181. _STATUS_MASK) )
  182. #define _SET_INUSE(pdesc) ( pdesc->pblock = (void *) \
  183. ((unsigned)_ADDRESS(pdesc) | _INUSE) )
  184. #define _SET_FREE(pdesc) ( pdesc->pblock = (void *) \
  185. ((unsigned)_ADDRESS(pdesc) | _FREE) )
  186. #define _SET_DUMMY(pdesc) ( pdesc->pblock = (void *) \
  187. ((unsigned)_ADDRESS(pdesc) | _DUMMY) )
  188. #define _IS_INUSE(pdesc) ( _STATUS(pdesc) == _INUSE )
  189. #define _IS_FREE(pdesc) ( _STATUS(pdesc) == _FREE )
  190. #define _IS_DUMMY(pdesc) ( _STATUS(pdesc) == _DUMMY )
  191. #define _BLKSIZE(pdesc) ( (unsigned) ( \
  192. (char *)_ADDRESS(pdesc->pnextdesc) - \
  193. (char *)_ADDRESS(pdesc) - _HDRSIZE ) )
  194. #define _MEMSIZE(pdesc) ( (char *)_ADDRESS(pdesc->pnextdesc) - \
  195. (char *)_ADDRESS(pdesc) )
  196. #define _BACKPTR(addr) ( *(_PBLKDESC*)((char *)(addr) - _HDRSIZE) )
  197. #define _CHECK_PDESC(pdesc) ( (*(_PBLKDESC*) (_ADDRESS(pdesc))) == pdesc )
  198. #define _CHECK_BACKPTR(addr) ( ((char *)(_BACKPTR(addr)->pblock) + _HDRSIZE) \
  199. == addr)
  200. /*
  201. * Heap descriptor
  202. */
  203. struct _heap_desc_ {
  204. _PBLKDESC pfirstdesc; /* pointer to first descriptor */
  205. _PBLKDESC proverdesc; /* rover pointer */
  206. _PBLKDESC emptylist; /* pointer to empty list */
  207. _BLKDESC sentinel; /* Sentinel block for end of heap list */
  208. };
  209. extern struct _heap_desc_ _heap_desc;
  210. /*
  211. * Region descriptor and heap grow data
  212. */
  213. struct _heap_region_ {
  214. void * _regbase; /* base address of region */
  215. unsigned _currsize; /* current size of region */
  216. unsigned _totalsize; /* total size of region */
  217. };
  218. #define _heap_growsize _amblksiz
  219. #ifndef _OLDROVER_
  220. extern unsigned int _heap_resetsize;
  221. #endif /* _OLDROVER_ */
  222. extern unsigned int _heap_regionsize;
  223. extern unsigned int _heap_maxregsize;
  224. extern struct _heap_region_ _heap_regions[];
  225. extern void ** _heap_descpages;
  226. #ifdef _M_ALPHA
  227. #define _PAGESIZE_ 0x2000 /* one page */
  228. #else
  229. #define _PAGESIZE_ 0x1000 /* one page */
  230. #endif
  231. #define _SEGSIZE_ 0x10000 /* one segment (i.e., 64 Kb) */
  232. #define _HEAP_REGIONMAX 0x40 /* Max number of regions: 64 */
  233. /* For small memory systems: */
  234. #define _HEAP_REGIONSIZE_S 0x4000 /* Initial region size (16K) */
  235. #define _HEAP_MAXREGSIZE_S 0x1000000 /* Maximum region size (16M) */
  236. /* For large memory systems: */
  237. #define _HEAP_REGIONSIZE_L 0x100000 /* Initial region size (1M) */
  238. #define _HEAP_MAXREGSIZE_L 0x1000000 /* Maximum region size (16M) */
  239. #define _HEAP_GROWSIZE 0x10000 /* Default grow increment (64K) */
  240. #define _HEAP_GROWMIN _PAGESIZE_ /* Minimum grow inc (1 page) */
  241. #define _HEAP_GROWSTART _PAGESIZE_ /* Startup grow increment */
  242. #define _HEAP_COALESCE -1 /* Coalesce heap value */
  243. #define _HEAP_EMPTYLIST_SIZE (1 * _PAGESIZE_)
  244. /*
  245. * Values returned by _heap_findaddr() routine
  246. */
  247. #define _HEAPFIND_EXACT 0 /* found address exactly */
  248. #define _HEAPFIND_WITHIN 1 /* address is within a block */
  249. #define _HEAPFIND_BEFORE -1 /* address before beginning of heap */
  250. #define _HEAPFIND_AFTER -2 /* address after end of heap */
  251. #define _HEAPFIND_EMPTY -3 /* address not found: empty heap */
  252. /*
  253. * Arguments to _heap_param
  254. */
  255. #define _HP_GETPARAM 0 /* get heap parameter value */
  256. #define _HP_SETPARAM 1 /* set heap parameter value */
  257. #define _HP_AMBLKSIZ 1 /* get/set _amblksiz value (aka */
  258. #define _HP_GROWSIZE _HP_AMBLKSIZ /* _heap_growsize */
  259. #define _HP_RESETSIZE 2 /* get/set _heap_resetsize value */
  260. /*
  261. * Macros to round numbers
  262. *
  263. * _ROUND2 = rounds a number up to a power of 2
  264. * _ROUND = rounds a number up to any other numer
  265. *
  266. * n = number to be rounded
  267. * pow2 = must be a power of two value
  268. * r = any number
  269. */
  270. #define _ROUND2(n,pow2) \
  271. ( ( n + pow2 - 1) & ~(pow2 - 1) )
  272. #define _ROUND(n,r) \
  273. ( ( (n/r) + ((n%r)?1:0) ) * r)
  274. /*
  275. Macros for accessing heap descriptor lists:
  276. _PUTEMPTY(x) = Puts an empty heap desc on the empty list
  277. (x = _PBLKDESC = pointer to heap block descriptor)
  278. */
  279. #ifdef _DEBUG
  280. #define _PUTEMPTY(x) \
  281. { \
  282. (x)->pnextdesc = _heap_desc.emptylist; \
  283. \
  284. (x)->pblock = NULL; \
  285. \
  286. _heap_desc.emptylist = (x); \
  287. }
  288. #else
  289. #define _PUTEMPTY(x) \
  290. { \
  291. (x)->pnextdesc = _heap_desc.emptylist; \
  292. \
  293. _heap_desc.emptylist = (x); \
  294. }
  295. #endif
  296. /*
  297. * Macros for finding the next 64 Kb boundary from a pointer
  298. */
  299. #define _NXTSEGBNDRY(p) ((void *)((unsigned)(p) & 0xffff0000 + 0x10000))
  300. #define _DISTTOBNDRY(p) ((unsigned)(0x10000 - (0x0000ffff & (unsigned)(p))))
  301. /*
  302. * Define size_t type (if necessary)
  303. */
  304. #if !defined(_W64)
  305. #if !defined(__midl) && (defined(_X86_) || defined(_M_IX86)) && _MSC_VER >= 1300 /*IFSTRIP=IGN*/
  306. #define _W64 __w64
  307. #else
  308. #define _W64
  309. #endif
  310. #endif
  311. #ifndef _SIZE_T_DEFINED
  312. #ifdef _WIN64
  313. typedef unsigned __int64 size_t;
  314. #else
  315. typedef _W64 unsigned int size_t;
  316. #endif
  317. #define _SIZE_T_DEFINED
  318. #endif
  319. /*
  320. * Prototypes
  321. */
  322. void * __cdecl _nh_malloc(size_t, int);
  323. void * __cdecl _heap_alloc(size_t);
  324. void * __cdecl _flat_malloc(size_t);
  325. _PBLKDESC __getempty(void);
  326. void __cdecl _heap_abort(void);
  327. int __cdecl _heap_addblock(void *, unsigned int);
  328. #ifdef _OLDROVER_
  329. void __cdecl _heap_advance_rover(void);
  330. #endif /* _OLDROVER_ */
  331. void __cdecl _heap_free_region(int);
  332. int __cdecl _heap_findaddr(void *, _PBLKDESC *);
  333. int __cdecl _heap_grow(unsigned int);
  334. int __cdecl _heap_grow_region(unsigned, size_t);
  335. int __cdecl _heap_init(void);
  336. #ifndef _OLDROVER_
  337. int __cdecl _heap_param(int, int, void *);
  338. #endif /* _OLDROVER_ */
  339. _PBLKDESC __cdecl _heap_search(unsigned size);
  340. _PBLKDESC __cdecl _heap_split_block(_PBLKDESC, size_t);
  341. #ifdef _DEBUG
  342. void __cdecl _heap_print_all(void);
  343. void __cdecl _heap_print_regions(void);
  344. void __cdecl _heap_print_desc(void);
  345. void __cdecl _heap_print_emptylist(void);
  346. void __cdecl _heap_print_heaplist(void);
  347. #endif
  348. /*
  349. * Prototypes and macros for multi-thread support
  350. */
  351. #ifdef _MT
  352. void __cdecl _free_lk(void *);
  353. size_t __cdecl _msize_lk(void *);
  354. size_t __cdecl _heapused_lk(size_t *pUsed, size_t *pFree);
  355. #ifdef _DEBUG
  356. void __cdecl _heap_print_regions_lk(void);
  357. void __cdecl _heap_print_desc_lk(void);
  358. void __cdecl _heap_print_emptylist_lk(void);
  359. void __cdecl _heap_print_heaplist_lk(void);
  360. #endif
  361. #else /* ndef _MT */
  362. #define _free_lk(p) free(p)
  363. #define _msize_lk(p) _msize(p)
  364. #ifdef _DEBUG
  365. #define _heap_print_regions_lk() _heap_print_regions()
  366. #define _heap_print_desc_lk() _heap_print_desc()
  367. #define _heap_print_emptylist_lk() _heap_print_emptylist()
  368. #define _heap_print_heaplist_lk() _heap_print_heaplist()
  369. #endif
  370. #endif /* _MT */
  371. #ifdef HEAPHOOK
  372. #ifndef _HEAPHOOK_DEFINED
  373. /* hook function type */
  374. typedef int (__cdecl * _HEAPHOOK)(int, size_t, void *, void *);
  375. #define _HEAPHOOK_DEFINED
  376. #endif /* _HEAPHOOK_DEFINED */
  377. extern _HEAPHOOK _heaphook;
  378. #endif /* HEAPHOOK */
  379. #ifdef __cplusplus
  380. }
  381. #endif
  382. #endif /* _INC_HEAP */