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.

306 lines
9.6 KiB

  1. /*++
  2. Copyright (c) 1989 Microsoft Corporation
  3. Module Name:
  4. theap.c
  5. Abstract:
  6. Test program for the Heap Procedures
  7. Author:
  8. Steven R. Wood [stevewo]
  9. Revision History:
  10. --*/
  11. #define THEAP
  12. #include "..\heap.c"
  13. #include "..\heapdll.c"
  14. #include "..\heapdbg.c"
  15. #include "..\heappage.c"
  16. #include <windows.h>
  17. #include <stdlib.h>
  18. ULONG NtGlobalFlag = FLG_HEAP_ENABLE_TAIL_CHECK |
  19. FLG_HEAP_ENABLE_FREE_CHECK |
  20. FLG_HEAP_VALIDATE_PARAMETERS |
  21. FLG_HEAP_VALIDATE_ALL |
  22. FLG_HEAP_ENABLE_TAGGING;
  23. BOOLEAN
  24. NtdllOkayToLockRoutine(
  25. IN PVOID Lock
  26. )
  27. {
  28. return TRUE;
  29. }
  30. PRTL_INITIALIZE_LOCK_ROUTINE RtlInitializeLockRoutine =
  31. (PRTL_INITIALIZE_LOCK_ROUTINE)RtlInitializeCriticalSection;
  32. PRTL_ACQUIRE_LOCK_ROUTINE RtlAcquireLockRoutine =
  33. (PRTL_ACQUIRE_LOCK_ROUTINE)RtlEnterCriticalSection;
  34. PRTL_RELEASE_LOCK_ROUTINE RtlReleaseLockRoutine =
  35. (PRTL_RELEASE_LOCK_ROUTINE)RtlLeaveCriticalSection;
  36. PRTL_DELETE_LOCK_ROUTINE RtlDeleteLockRoutine =
  37. (PRTL_DELETE_LOCK_ROUTINE)RtlDeleteCriticalSection;
  38. PRTL_OKAY_TO_LOCK_ROUTINE RtlOkayToLockRoutine =
  39. (PRTL_OKAY_TO_LOCK_ROUTINE)NtdllOkayToLockRoutine;
  40. RTL_HEAP_PARAMETERS HeapParameters;
  41. ULONG RtlpHeapValidateOnCall;
  42. ULONG RtlpHeapStopOnFree;
  43. ULONG RtlpHeapStopOnReAlloc;
  44. typedef struct _TEST_HEAP_ENTRY {
  45. PVOID AllocatedBlock;
  46. ULONG Size;
  47. } TEST_HEAP_ENTRY, *PTEST_HEAP_ENTRY;
  48. ULONG NumberOfHeapEntries;
  49. PTEST_HEAP_ENTRY HeapEntries;
  50. ULONG Seed = 14623;
  51. #define MAX_HEAP_ALLOC 0x120000
  52. #define REASONABLE_HEAP_ALLOC 0x200
  53. int
  54. _cdecl
  55. main(
  56. int argc,
  57. char *argv[]
  58. )
  59. {
  60. PVOID Heap, AllocatedBlock;
  61. ULONG i, n;
  62. PTEST_HEAP_ENTRY p;
  63. BOOLEAN Result;
  64. NTSTATUS Status;
  65. RTL_HEAP_USAGE Usage;
  66. PRTL_HEAP_USAGE_ENTRY pEntries;
  67. ULONG TagBaseIndex, Tag;
  68. ULONG TotalAllocated;
  69. RtlInitializeHeapManager();
  70. memset( &Usage, 0, sizeof( Usage ) );
  71. #if 0
  72. HeapParameters.Length = sizeof( HeapParameters );
  73. HeapParameters.DeCommitFreeBlockThreshold = 0x1000;
  74. HeapParameters.DeCommitTotalFreeThreshold = 0x4000;
  75. Heap = RtlCreateHeap( HEAP_GROWABLE | HEAP_NO_SERIALIZE,
  76. NULL,
  77. 256 * 4096,
  78. 4096,
  79. NULL,
  80. &HeapParameters
  81. );
  82. #endif
  83. Heap = RtlCreateHeap( HEAP_GROWABLE | HEAP_NO_SERIALIZE | HEAP_CLASS_3,
  84. NULL,
  85. 0x100000,
  86. 0x1000,
  87. NULL,
  88. NULL
  89. );
  90. if (Heap == NULL) {
  91. fprintf( stderr, "THEAP: Unable to create heap.\n" );
  92. exit( 1 );
  93. }
  94. fprintf( stderr, "THEAP: Created heap at %x\n", Heap );
  95. DebugBreak();
  96. TagBaseIndex = RtlCreateTagHeap( Heap, 0, L"THEAP!",
  97. L"!HeapName\0"
  98. L"Tag1\0"
  99. L"Tag2\0"
  100. L"Tag3\0"
  101. L"Tag4\0"
  102. L"Tag5\0"
  103. L"Tag6\0"
  104. L"Tag7\0"
  105. L"Tag8\0"
  106. L"Tag9\0"
  107. L"Tag10\0"
  108. L"Tag11\0"
  109. L"Tag12\0"
  110. L"Tag13\0"
  111. L"Tag14\0"
  112. L"Tag15\0"
  113. L"Tag16\0"
  114. L"Tag17\0"
  115. L"Tag18\0"
  116. L"Tag19\0"
  117. L"Tag20\0"
  118. L"Tag21\0"
  119. L"Tag22\0"
  120. L"Tag23\0"
  121. L"Tag24\0"
  122. L"Tag25\0"
  123. L"Tag26\0"
  124. L"Tag27\0"
  125. L"Tag28\0"
  126. L"Tag29\0"
  127. L"Tag30\0"
  128. L"Tag31\0"
  129. L"Tag32\0"
  130. L"Tag33\0"
  131. L"Tag34\0"
  132. L"Tag35\0"
  133. L"Tag36\0"
  134. L"Tag37\0"
  135. L"Tag38\0"
  136. L"Tag39\0"
  137. L"Tag40\0"
  138. L"Tag41\0"
  139. L"Tag42\0"
  140. L"Tag43\0"
  141. L"Tag44\0"
  142. L"Tag45\0"
  143. L"Tag46\0"
  144. L"Tag47\0"
  145. L"Tag48\0"
  146. L"Tag49\0"
  147. L"Tag50\0"
  148. L"Tag51\0"
  149. L"Tag52\0"
  150. L"Tag53\0"
  151. L"Tag54\0"
  152. L"Tag55\0"
  153. L"Tag56\0"
  154. L"Tag57\0"
  155. L"Tag58\0"
  156. L"Tag59\0"
  157. L"Tag60\0"
  158. );
  159. NumberOfHeapEntries = 1000;
  160. HeapEntries = VirtualAlloc( NULL,
  161. NumberOfHeapEntries * sizeof( *HeapEntries ),
  162. MEM_COMMIT,
  163. PAGE_READWRITE
  164. );
  165. if (HeapEntries == NULL) {
  166. fprintf( stderr, "THEAP: Unable to allocate space.\n" );
  167. exit( 1 );
  168. }
  169. RtlpHeapValidateOnCall=TRUE;
  170. // RtlpHeapStopOnAllocate=0x350f88;
  171. // RtlpHeapStopOnReAlloc=0x710040;
  172. TotalAllocated = 0;
  173. while (TotalAllocated < (2 * 1024 * 1024)) {
  174. i = RtlUniform( &Seed ) % NumberOfHeapEntries;
  175. if (RtlUniform( &Seed ) % 100) {
  176. n = RtlUniform( &Seed ) % REASONABLE_HEAP_ALLOC;
  177. }
  178. else {
  179. n = RtlUniform( &Seed ) % MAX_HEAP_ALLOC;
  180. }
  181. #if 0
  182. Usage.Length = sizeof( Usage );
  183. Status = RtlUsageHeap( Heap, HEAP_USAGE_ALLOCATED_BLOCKS , &Usage );
  184. if (NT_SUCCESS( Status )) {
  185. if (Status == STATUS_MORE_ENTRIES) {
  186. pEntries = Usage.AddedEntries;
  187. while (pEntries) {
  188. fprintf( stderr,
  189. "Added: %08x %06x\n",
  190. pEntries->Address,
  191. pEntries->Size
  192. );
  193. pEntries = pEntries->Next;
  194. }
  195. pEntries = Usage.RemovedEntries;
  196. while (pEntries) {
  197. fprintf( stderr,
  198. "Freed: %08x %06x\n",
  199. pEntries->Address,
  200. pEntries->Size
  201. );
  202. pEntries = pEntries->Next;
  203. }
  204. }
  205. fprintf( stderr, "%08x %08x %08x %08x ",
  206. Usage.BytesAllocated, Usage.BytesCommitted,
  207. Usage.BytesReserved, Usage.BytesReservedMaximum
  208. );
  209. }
  210. else {
  211. fprintf( stderr, "RtlUsageHeap failed with status %x\n", Status );
  212. DebugBreak();
  213. }
  214. if (i < 60) {
  215. Tag = (TagBaseIndex + i + 1) << 16;
  216. }
  217. else {
  218. Tag = 0;
  219. }
  220. #endif
  221. Tag = 0;
  222. p = &HeapEntries[ i ];
  223. if (p->AllocatedBlock == NULL) {
  224. TotalAllocated += n;
  225. p->AllocatedBlock = RtlAllocateHeap( Heap, Tag, n );
  226. fprintf( stderr, "Allocated %06x bytes at %08x\n", n, p->AllocatedBlock );
  227. if (p->AllocatedBlock != NULL) {
  228. p->Size = n;
  229. }
  230. else {
  231. DebugBreak();
  232. }
  233. }
  234. else
  235. if (RtlUniform( &Seed ) & 1) {
  236. TotalAllocated -= p->Size;
  237. TotalAllocated += n;
  238. AllocatedBlock = RtlReAllocateHeap( Heap, Tag, p->AllocatedBlock, n );
  239. fprintf( stderr, "ReAlloced %06x bytes at %08x to %06x bytes at %08x\n",
  240. p->Size,
  241. p->AllocatedBlock,
  242. n,
  243. AllocatedBlock
  244. );
  245. if (AllocatedBlock != NULL) {
  246. p->AllocatedBlock = AllocatedBlock;
  247. p->Size = n;
  248. }
  249. else {
  250. DebugBreak();
  251. }
  252. }
  253. else {
  254. TotalAllocated -= p->Size;
  255. Result = RtlFreeHeap( Heap, 0, p->AllocatedBlock );
  256. fprintf( stderr, "Freed %06x bytes at %08x\n",
  257. p->Size,
  258. p->AllocatedBlock
  259. );
  260. if (Result) {
  261. p->AllocatedBlock = NULL;
  262. p->Size = 0;
  263. }
  264. else {
  265. DebugBreak();
  266. }
  267. }
  268. }
  269. RtlResetHeap( Heap, 0 );
  270. RtlValidateHeap( Heap, 0, NULL );
  271. return 0;
  272. }