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.

319 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. * 07-15-01 PML Remove all ALPHA, MIPS, and PPC code
  41. *
  42. ****/
  43. #if _MSC_VER > 1000 /*IFSTRIP=IGN*/
  44. #pragma once
  45. #endif
  46. #ifndef _INC_WINHEAP
  47. #define _INC_WINHEAP
  48. #ifndef _CRTBLD
  49. /*
  50. * This is an internal C runtime header file. It is used when building
  51. * the C runtimes only. It is not to be used as a public header file.
  52. */
  53. #error ERROR: Use of C runtime library internal header file.
  54. #endif /* _CRTBLD */
  55. #ifdef __cplusplus
  56. extern "C" {
  57. #endif
  58. #include <windows.h>
  59. // Declarations and definitions for the multiple heap scheme (VC++ 6.1)
  60. // Heap-selection constants
  61. #define __SYSTEM_HEAP 1
  62. #define __V5_HEAP 2
  63. #define __V6_HEAP 3
  64. #define __HEAP_ENV_STRING "__MSVCRT_HEAP_SELECT"
  65. #define __GLOBAL_HEAP_SELECTOR "__GLOBAL_HEAP_SELECTED"
  66. #ifndef _WIN64
  67. // Heap-selection global variable
  68. extern int __active_heap;
  69. #endif /* _WIN64 */
  70. #ifdef CRTDLL
  71. // Linker info for heap selection
  72. typedef struct {
  73. union {
  74. DWORD dw;
  75. struct {
  76. BYTE bverMajor;
  77. BYTE bverMinor;
  78. };
  79. };
  80. } LinkerVersion;
  81. extern void __cdecl _GetLinkerVersion(LinkerVersion * plv);
  82. #endif /* CRTDLL */
  83. // Definitions, declarations and prototypes for the small-block heap (VC++ 6.0)
  84. #define BYTES_PER_PARA 16
  85. #define DWORDS_PER_PARA 4
  86. #define PARAS_PER_PAGE 256 // tunable value
  87. #define PAGES_PER_GROUP 8 // tunable value
  88. #define GROUPS_PER_REGION 32 // tunable value (max 32)
  89. #define BYTES_PER_PAGE (BYTES_PER_PARA * PARAS_PER_PAGE)
  90. #define BYTES_PER_GROUP (BYTES_PER_PAGE * PAGES_PER_GROUP)
  91. #define BYTES_PER_REGION (BYTES_PER_GROUP * GROUPS_PER_REGION)
  92. #define ENTRY_OFFSET 0x0000000cL // offset of entry in para
  93. #define OVERHEAD_PER_PAGE 0x00000010L // sixteen bytes of overhead
  94. #define MAX_FREE_ENTRY_SIZE (BYTES_PER_PAGE - OVERHEAD_PER_PAGE)
  95. #define BITV_COMMIT_INIT (((1 << GROUPS_PER_REGION) - 1) << \
  96. (32 - GROUPS_PER_REGION))
  97. #define MAX_ALLOC_DATA_SIZE 0x3f8
  98. #define MAX_ALLOC_ENTRY_SIZE (MAX_ALLOC_DATA_SIZE + 0x8)
  99. typedef unsigned int BITVEC;
  100. typedef struct tagListHead
  101. {
  102. struct tagEntry * pEntryNext;
  103. struct tagEntry * pEntryPrev;
  104. }
  105. LISTHEAD, *PLISTHEAD;
  106. typedef struct tagEntry
  107. {
  108. int sizeFront;
  109. struct tagEntry * pEntryNext;
  110. struct tagEntry * pEntryPrev;
  111. }
  112. ENTRY, *PENTRY;
  113. typedef struct tagEntryEnd
  114. {
  115. int sizeBack;
  116. }
  117. ENTRYEND, *PENTRYEND;
  118. typedef struct tagGroup
  119. {
  120. int cntEntries;
  121. struct tagListHead listHead[64];
  122. }
  123. GROUP, *PGROUP;
  124. typedef struct tagRegion
  125. {
  126. int indGroupUse;
  127. char cntRegionSize[64];
  128. BITVEC bitvGroupHi[GROUPS_PER_REGION];
  129. BITVEC bitvGroupLo[GROUPS_PER_REGION];
  130. struct tagGroup grpHeadList[GROUPS_PER_REGION];
  131. }
  132. REGION, *PREGION;
  133. typedef struct tagHeader
  134. {
  135. BITVEC bitvEntryHi;
  136. BITVEC bitvEntryLo;
  137. BITVEC bitvCommit;
  138. void * pHeapData;
  139. struct tagRegion * pRegion;
  140. }
  141. HEADER, *PHEADER;
  142. extern HANDLE _crtheap;
  143. /*
  144. * Global variable declarations for the small-block heap.
  145. */
  146. extern size_t __sbh_threshold;
  147. void * __cdecl _nh_malloc(size_t, int);
  148. void * __cdecl _heap_alloc(size_t);
  149. extern PHEADER __sbh_pHeaderList; // pointer to list start
  150. extern PHEADER __sbh_pHeaderScan; // pointer to list rover
  151. extern int __sbh_sizeHeaderList; // allocated size of list
  152. extern int __sbh_cntHeaderList; // count of entries defined
  153. extern PHEADER __sbh_pHeaderDefer;
  154. extern int __sbh_indGroupDefer;
  155. extern size_t __cdecl _get_sb_threshold(void);
  156. extern int __cdecl _set_sb_threshold(size_t);
  157. extern int __cdecl _heap_init(int);
  158. extern void __cdecl _heap_term(void);
  159. extern void * __cdecl _malloc_base(size_t);
  160. extern void __cdecl _free_base(void *);
  161. extern void * __cdecl _realloc_base(void *, size_t);
  162. extern void * __cdecl _expand_base(void *, size_t);
  163. extern void * __cdecl _calloc_base(size_t, size_t);
  164. extern size_t __cdecl _msize_base(void *);
  165. extern int __cdecl __sbh_heap_init(size_t);
  166. extern void * __cdecl __sbh_alloc_block(int);
  167. extern PHEADER __cdecl __sbh_alloc_new_region(void);
  168. extern int __cdecl __sbh_alloc_new_group(PHEADER);
  169. extern PHEADER __cdecl __sbh_find_block(void *);
  170. #ifdef _DEBUG
  171. extern int __cdecl __sbh_verify_block(PHEADER, void *);
  172. #endif
  173. extern void __cdecl __sbh_free_block(PHEADER, void *);
  174. extern int __cdecl __sbh_resize_block(PHEADER, void *, int);
  175. extern void __cdecl __sbh_heapmin(void);
  176. extern int __cdecl __sbh_heap_check(void);
  177. #ifdef CRTDLL
  178. // Definitions, declarations and prototypes for the old small-block heap
  179. // (shipped with VC++ 5.0)
  180. #define _OLD_PAGESIZE 0x1000 // one page
  181. // Constants and types used by the old small-block heap
  182. #define _OLD_PARASIZE 0x10
  183. #define _OLD_PARASHIFT 0x4
  184. #define _OLD_PARAS_PER_PAGE 240
  185. #define _OLD_PADDING_PER_PAGE 7
  186. #define _OLD_PAGES_PER_REGION 1024
  187. #define _OLD_PAGES_PER_COMMITMENT 16
  188. typedef char __old_para_t[16];
  189. typedef unsigned char __old_page_map_t;
  190. #define _OLD_FREE_PARA (__old_page_map_t)(0)
  191. #define _OLD_UNCOMMITTED_PAGE (-1)
  192. #define _OLD_NO_FAILED_ALLOC (size_t)(_OLD_PARAS_PER_PAGE + 1)
  193. // Small-block heap page. The first four fields of the structure below are
  194. // descriptor for the page. That is, they hold information about allocations
  195. // in the page. The last field (typed as an array of paragraphs) is the
  196. // allocation area.
  197. typedef struct __old_sbh_page_struct {
  198. __old_page_map_t * p_starting_alloc_map;
  199. size_t free_paras_at_start;
  200. __old_page_map_t alloc_map[_OLD_PARAS_PER_PAGE + 1];
  201. __old_page_map_t reserved[_OLD_PADDING_PER_PAGE];
  202. __old_para_t alloc_blocks[_OLD_PARAS_PER_PAGE];
  203. } __old_sbh_page_t;
  204. #define _OLD_NO_PAGES (__old_sbh_page_t *)-1
  205. // Type used in small block region desciptor type (see below).
  206. typedef struct {
  207. int free_paras_in_page;
  208. size_t last_failed_alloc;
  209. } __old_region_map_t;
  210. // Small-block heap region descriptor. Most often, the small-block heap
  211. // consists of a single region, described by the statically allocated
  212. // decriptor __small_block_heap (declared below).
  213. struct __old_sbh_region_struct {
  214. struct __old_sbh_region_struct *p_next_region;
  215. struct __old_sbh_region_struct *p_prev_region;
  216. __old_region_map_t * p_starting_region_map;
  217. __old_region_map_t * p_first_uncommitted;
  218. __old_sbh_page_t * p_pages_begin;
  219. __old_sbh_page_t * p_pages_end;
  220. __old_region_map_t region_map[_OLD_PAGES_PER_REGION + 1];
  221. };
  222. typedef struct __old_sbh_region_struct __old_sbh_region_t;
  223. // Global variable declarations for the old small-block heap.
  224. extern __old_sbh_region_t __old_small_block_heap;
  225. extern size_t __old_sbh_threshold;
  226. // Prototypes for internal functions of the old small-block heap.
  227. void * __cdecl __old_sbh_alloc_block(size_t);
  228. void * __cdecl __old_sbh_alloc_block_from_page(__old_sbh_page_t *, size_t,
  229. size_t);
  230. void __cdecl __old_sbh_decommit_pages(int);
  231. __old_page_map_t * __cdecl __old_sbh_find_block(void *, __old_sbh_region_t **,
  232. __old_sbh_page_t **);
  233. void __cdecl __old_sbh_free_block(__old_sbh_region_t *, __old_sbh_page_t *,
  234. __old_page_map_t *);
  235. int __cdecl __old_sbh_heap_check(void);
  236. __old_sbh_region_t * __cdecl __old_sbh_new_region(void);
  237. void __cdecl __old_sbh_release_region(__old_sbh_region_t *);
  238. int __cdecl __old_sbh_resize_block(__old_sbh_region_t *,
  239. __old_sbh_page_t *, __old_page_map_t *, size_t);
  240. #endif /* CRTDLL */
  241. #ifdef HEAPHOOK
  242. #ifndef _HEAPHOOK_DEFINED
  243. /* hook function type */
  244. typedef int (__cdecl * _HEAPHOOK)(int, size_t, void *, void *);
  245. #define _HEAPHOOK_DEFINED
  246. #endif /* _HEAPHOOK_DEFINED */
  247. extern _HEAPHOOK _heaphook;
  248. #endif /* HEAPHOOK */
  249. #ifdef __cplusplus
  250. }
  251. #endif
  252. #endif /* _INC_WINHEAP */