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.

335 lines
10 KiB

  1. /***
  2. *winheap.h - Private include file for winheap directory.
  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. * 10-01-92 SRW Created.
  13. * 10-28-92 SRW Change winheap code to call Heap????Ex calls
  14. * 11-05-92 SKS Change name of variable "CrtHeap" to "_crtheap"
  15. * 11-07-92 SRW _NTIDW340 replaced by linkopts\betacmp.c
  16. * 02-23-93 SKS Update copyright to 1993
  17. * 10-01-94 BWT Add _nh_malloc prototype and update copyright
  18. * 10-31-94 GJF Added _PAGESIZE_ definition.
  19. * 11-07-94 GJF Changed _INC_HEAP to _INC_WINHEAP.
  20. * 02-14-95 CFW Clean up Mac merge.
  21. * 03-29-95 CFW Add error message to internal headers.
  22. * 04-06-95 GJF Updated (primarily Win32s DLL support) to re-
  23. * incorporate into retail Crt build.
  24. * 05-24-95 CFW Add heap hook.
  25. * 12-14-95 JWM Add "#pragma once".
  26. * 03-07-96 GJF Added support for the small-block heap.
  27. * 04-05-96 GJF Changes to __sbh_page_t type to improve performance
  28. * (see sbheap.c for details).
  29. * 05-08-96 GJF Several changes to small-block heap types.
  30. * 02-21-97 GJF Cleaned out obsolete support for Win32s.
  31. * 05-22-97 RDK Replaced definitions for new small-block support.
  32. * 07-23-97 GJF _heap_init changed slightly.
  33. * 10-01-98 GJF Added decl for __sbh_initialized. Also, changed
  34. * __sbh_heap_init() slightly.
  35. * 11-17-98 GJF Resurrected support for old (VC++ 5.0) small-block and
  36. * added support for multiple heap scheme (VC++ 6.1)
  37. * 06-22-99 GJF Removed old small-block heap from static libs.
  38. * 11-30-99 PML Compile /Wp64 clean.
  39. * 08-07-00 PML __active_heap not available on Win64
  40. *
  41. ****/
  42. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  43. #pragma once
  44. #endif
  45. #ifndef _INC_WINHEAP
  46. #define _INC_WINHEAP
  47. #ifndef _CRTBLD
  48. /*
  49. * This is an internal C runtime header file. It is used when building
  50. * the C runtimes only. It is not to be used as a public header file.
  51. */
  52. #error ERROR: Use of C runtime library internal header file.
  53. #endif /* _CRTBLD */
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #include <windows.h>
  58. // Declarations and definitions for the multiple heap scheme (VC++ 6.1)
  59. // Heap-selection constants
  60. #define __SYSTEM_HEAP 1
  61. #define __V5_HEAP 2
  62. #define __V6_HEAP 3
  63. #define __HEAP_ENV_STRING "__MSVCRT_HEAP_SELECT"
  64. #define __GLOBAL_HEAP_SELECTOR "__GLOBAL_HEAP_SELECTED"
  65. #ifndef _WIN64
  66. // Heap-selection global variable
  67. extern int __active_heap;
  68. #endif /* _WIN64 */
  69. #ifdef CRTDLL
  70. // Linker info for heap selection
  71. typedef struct {
  72. union {
  73. DWORD dw;
  74. struct {
  75. BYTE bverMajor;
  76. BYTE bverMinor;
  77. };
  78. };
  79. } LinkerVersion;
  80. extern void __cdecl _GetLinkerVersion(LinkerVersion * plv);
  81. #endif /* CRTDLL */
  82. // Definitions, declarations and prototypes for the small-block heap (VC++ 6.0)
  83. #define BYTES_PER_PARA 16
  84. #define DWORDS_PER_PARA 4
  85. #define PARAS_PER_PAGE 256 // tunable value
  86. #define PAGES_PER_GROUP 8 // tunable value
  87. #define GROUPS_PER_REGION 32 // tunable value (max 32)
  88. #define BYTES_PER_PAGE (BYTES_PER_PARA * PARAS_PER_PAGE)
  89. #define BYTES_PER_GROUP (BYTES_PER_PAGE * PAGES_PER_GROUP)
  90. #define BYTES_PER_REGION (BYTES_PER_GROUP * GROUPS_PER_REGION)
  91. #define ENTRY_OFFSET 0x0000000cL // offset of entry in para
  92. #define OVERHEAD_PER_PAGE 0x00000010L // sixteen bytes of overhead
  93. #define MAX_FREE_ENTRY_SIZE (BYTES_PER_PAGE - OVERHEAD_PER_PAGE)
  94. #define BITV_COMMIT_INIT (((1 << GROUPS_PER_REGION) - 1) << \
  95. (32 - GROUPS_PER_REGION))
  96. #define MAX_ALLOC_DATA_SIZE 0x3f8
  97. #define MAX_ALLOC_ENTRY_SIZE (MAX_ALLOC_DATA_SIZE + 0x8)
  98. typedef unsigned int BITVEC;
  99. typedef struct tagListHead
  100. {
  101. struct tagEntry * pEntryNext;
  102. struct tagEntry * pEntryPrev;
  103. }
  104. LISTHEAD, *PLISTHEAD;
  105. typedef struct tagEntry
  106. {
  107. int sizeFront;
  108. struct tagEntry * pEntryNext;
  109. struct tagEntry * pEntryPrev;
  110. }
  111. ENTRY, *PENTRY;
  112. typedef struct tagEntryEnd
  113. {
  114. int sizeBack;
  115. }
  116. ENTRYEND, *PENTRYEND;
  117. typedef struct tagGroup
  118. {
  119. int cntEntries;
  120. struct tagListHead listHead[64];
  121. }
  122. GROUP, *PGROUP;
  123. typedef struct tagRegion
  124. {
  125. int indGroupUse;
  126. char cntRegionSize[64];
  127. BITVEC bitvGroupHi[GROUPS_PER_REGION];
  128. BITVEC bitvGroupLo[GROUPS_PER_REGION];
  129. struct tagGroup grpHeadList[GROUPS_PER_REGION];
  130. }
  131. REGION, *PREGION;
  132. typedef struct tagHeader
  133. {
  134. BITVEC bitvEntryHi;
  135. BITVEC bitvEntryLo;
  136. BITVEC bitvCommit;
  137. void * pHeapData;
  138. struct tagRegion * pRegion;
  139. }
  140. HEADER, *PHEADER;
  141. extern HANDLE _crtheap;
  142. /*
  143. * Global variable declarations for the small-block heap.
  144. */
  145. extern size_t __sbh_threshold;
  146. void * __cdecl _nh_malloc(size_t, int);
  147. void * __cdecl _heap_alloc(size_t);
  148. extern PHEADER __sbh_pHeaderList; // pointer to list start
  149. extern PHEADER __sbh_pHeaderScan; // pointer to list rover
  150. extern int __sbh_sizeHeaderList; // allocated size of list
  151. extern int __sbh_cntHeaderList; // count of entries defined
  152. extern PHEADER __sbh_pHeaderDefer;
  153. extern int __sbh_indGroupDefer;
  154. extern size_t __cdecl _get_sb_threshold(void);
  155. extern int __cdecl _set_sb_threshold(size_t);
  156. extern int __cdecl _heap_init(int);
  157. extern void __cdecl _heap_term(void);
  158. extern void * __cdecl _malloc_base(size_t);
  159. extern void * __cdecl _nh_malloc_base(size_t, int);
  160. extern void * __cdecl _heap_alloc_base(size_t);
  161. extern void __cdecl _free_base(void *);
  162. extern void * __cdecl _realloc_base(void *, size_t);
  163. extern void * __cdecl _expand_base(void *, size_t);
  164. extern void * __cdecl _calloc_base(size_t, size_t);
  165. extern size_t __cdecl _msize_base(void *);
  166. extern int __cdecl __sbh_heap_init(size_t);
  167. extern void * __cdecl __sbh_alloc_block(int);
  168. extern PHEADER __cdecl __sbh_alloc_new_region(void);
  169. extern int __cdecl __sbh_alloc_new_group(PHEADER);
  170. extern PHEADER __cdecl __sbh_find_block(void *);
  171. #ifdef _DEBUG
  172. extern int __cdecl __sbh_verify_block(PHEADER, void *);
  173. #endif
  174. extern void __cdecl __sbh_free_block(PHEADER, void *);
  175. extern int __cdecl __sbh_resize_block(PHEADER, void *, int);
  176. extern void __cdecl __sbh_heapmin(void);
  177. extern int __cdecl __sbh_heap_check(void);
  178. #ifdef CRTDLL
  179. // Definitions, declarations and prototypes for the old small-block heap
  180. // (shipped with VC++ 5.0)
  181. #ifdef _M_ALPHA
  182. #define _OLD_PAGESIZE 0x2000 // one page
  183. #else
  184. #define _OLD_PAGESIZE 0x1000 // one page
  185. #endif
  186. // Constants and types used by the old small-block heap
  187. #define _OLD_PARASIZE 0x10
  188. #define _OLD_PARASHIFT 0x4
  189. #ifdef _M_ALPHA
  190. #define _OLD_PARAS_PER_PAGE 454
  191. #define _OLD_PADDING_PER_PAGE 5
  192. #define _OLD_PAGES_PER_REGION 512
  193. #define _OLD_PAGES_PER_COMMITMENT 8
  194. #else
  195. #define _OLD_PARAS_PER_PAGE 240
  196. #define _OLD_PADDING_PER_PAGE 7
  197. #define _OLD_PAGES_PER_REGION 1024
  198. #define _OLD_PAGES_PER_COMMITMENT 16
  199. #endif
  200. typedef char __old_para_t[16];
  201. #ifdef _M_ALPHA
  202. typedef unsigned short __old_page_map_t;
  203. #else
  204. typedef unsigned char __old_page_map_t;
  205. #endif
  206. #define _OLD_FREE_PARA (__old_page_map_t)(0)
  207. #define _OLD_UNCOMMITTED_PAGE (-1)
  208. #define _OLD_NO_FAILED_ALLOC (size_t)(_OLD_PARAS_PER_PAGE + 1)
  209. // Small-block heap page. The first four fields of the structure below are
  210. // descriptor for the page. That is, they hold information about allocations
  211. // in the page. The last field (typed as an array of paragraphs) is the
  212. // allocation area.
  213. typedef struct __old_sbh_page_struct {
  214. __old_page_map_t * p_starting_alloc_map;
  215. size_t free_paras_at_start;
  216. __old_page_map_t alloc_map[_OLD_PARAS_PER_PAGE + 1];
  217. __old_page_map_t reserved[_OLD_PADDING_PER_PAGE];
  218. __old_para_t alloc_blocks[_OLD_PARAS_PER_PAGE];
  219. } __old_sbh_page_t;
  220. #define _OLD_NO_PAGES (__old_sbh_page_t *)-1
  221. // Type used in small block region desciptor type (see below).
  222. typedef struct {
  223. int free_paras_in_page;
  224. size_t last_failed_alloc;
  225. } __old_region_map_t;
  226. // Small-block heap region descriptor. Most often, the small-block heap
  227. // consists of a single region, described by the statically allocated
  228. // decriptor __small_block_heap (declared below).
  229. struct __old_sbh_region_struct {
  230. struct __old_sbh_region_struct *p_next_region;
  231. struct __old_sbh_region_struct *p_prev_region;
  232. __old_region_map_t * p_starting_region_map;
  233. __old_region_map_t * p_first_uncommitted;
  234. __old_sbh_page_t * p_pages_begin;
  235. __old_sbh_page_t * p_pages_end;
  236. __old_region_map_t region_map[_OLD_PAGES_PER_REGION + 1];
  237. };
  238. typedef struct __old_sbh_region_struct __old_sbh_region_t;
  239. // Global variable declarations for the old small-block heap.
  240. extern __old_sbh_region_t __old_small_block_heap;
  241. extern size_t __old_sbh_threshold;
  242. // Prototypes for internal functions of the old small-block heap.
  243. void * __cdecl __old_sbh_alloc_block(size_t);
  244. void * __cdecl __old_sbh_alloc_block_from_page(__old_sbh_page_t *, size_t,
  245. size_t);
  246. void __cdecl __old_sbh_decommit_pages(int);
  247. __old_page_map_t * __cdecl __old_sbh_find_block(void *, __old_sbh_region_t **,
  248. __old_sbh_page_t **);
  249. void __cdecl __old_sbh_free_block(__old_sbh_region_t *, __old_sbh_page_t *,
  250. __old_page_map_t *);
  251. int __cdecl __old_sbh_heap_check(void);
  252. __old_sbh_region_t * __cdecl __old_sbh_new_region(void);
  253. void __cdecl __old_sbh_release_region(__old_sbh_region_t *);
  254. int __cdecl __old_sbh_resize_block(__old_sbh_region_t *,
  255. __old_sbh_page_t *, __old_page_map_t *, size_t);
  256. #endif /* CRTDLL */
  257. #ifdef HEAPHOOK
  258. #ifndef _HEAPHOOK_DEFINED
  259. /* hook function type */
  260. typedef int (__cdecl * _HEAPHOOK)(int, size_t, void *, void *);
  261. #define _HEAPHOOK_DEFINED
  262. #endif /* _HEAPHOOK_DEFINED */
  263. extern _HEAPHOOK _heaphook;
  264. #endif /* HEAPHOOK */
  265. #ifdef __cplusplus
  266. }
  267. #endif
  268. #endif /* _INC_WINHEAP */