00081 {
00082 PVOID Heap, AllocatedBlock;
00083 ULONG i,
n;
00084
PTEST_HEAP_ENTRY p;
00085 BOOLEAN Result;
00086
NTSTATUS Status;
00087 RTL_HEAP_USAGE
Usage;
00088 PRTL_HEAP_USAGE_ENTRY pEntries;
00089 ULONG TagBaseIndex, Tag;
00090 ULONG TotalAllocated;
00091
00092
RtlInitializeHeapManager();
00093 memset( &Usage, 0,
sizeof( Usage ) );
00094
00095
#if 0
00096
HeapParameters.Length =
sizeof(
HeapParameters );
00097
HeapParameters.DeCommitFreeBlockThreshold = 0x1000;
00098
HeapParameters.DeCommitTotalFreeThreshold = 0x4000;
00099 Heap =
RtlCreateHeap( HEAP_GROWABLE | HEAP_NO_SERIALIZE,
00100 NULL,
00101 256 * 4096,
00102 4096,
00103 NULL,
00104 &HeapParameters
00105 );
00106
#endif
00107
Heap =
RtlCreateHeap( HEAP_GROWABLE | HEAP_NO_SERIALIZE | HEAP_CLASS_3,
00108 NULL,
00109 0x100000,
00110 0x1000,
00111 NULL,
00112 NULL
00113 );
00114
if (Heap ==
NULL) {
00115 fprintf( stderr,
"THEAP: Unable to create heap.\n" );
00116
exit( 1 );
00117 }
00118 fprintf( stderr,
"THEAP: Created heap at %x\n", Heap );
00119 DebugBreak();
00120 TagBaseIndex =
RtlCreateTagHeap( Heap, 0, L
"THEAP!",
00121 L
"!HeapName\0"
00122 L
"Tag1\0"
00123 L
"Tag2\0"
00124 L
"Tag3\0"
00125 L
"Tag4\0"
00126 L
"Tag5\0"
00127 L
"Tag6\0"
00128 L
"Tag7\0"
00129 L
"Tag8\0"
00130 L
"Tag9\0"
00131 L
"Tag10\0"
00132 L
"Tag11\0"
00133 L
"Tag12\0"
00134 L
"Tag13\0"
00135 L
"Tag14\0"
00136 L
"Tag15\0"
00137 L
"Tag16\0"
00138 L
"Tag17\0"
00139 L
"Tag18\0"
00140 L
"Tag19\0"
00141 L
"Tag20\0"
00142 L
"Tag21\0"
00143 L
"Tag22\0"
00144 L
"Tag23\0"
00145 L
"Tag24\0"
00146 L
"Tag25\0"
00147 L
"Tag26\0"
00148 L
"Tag27\0"
00149 L
"Tag28\0"
00150 L
"Tag29\0"
00151 L
"Tag30\0"
00152 L
"Tag31\0"
00153 L
"Tag32\0"
00154 L
"Tag33\0"
00155 L
"Tag34\0"
00156 L
"Tag35\0"
00157 L
"Tag36\0"
00158 L
"Tag37\0"
00159 L
"Tag38\0"
00160 L
"Tag39\0"
00161 L
"Tag40\0"
00162 L
"Tag41\0"
00163 L
"Tag42\0"
00164 L
"Tag43\0"
00165 L
"Tag44\0"
00166 L
"Tag45\0"
00167 L
"Tag46\0"
00168 L
"Tag47\0"
00169 L
"Tag48\0"
00170 L
"Tag49\0"
00171 L
"Tag50\0"
00172 L
"Tag51\0"
00173 L
"Tag52\0"
00174 L
"Tag53\0"
00175 L
"Tag54\0"
00176 L
"Tag55\0"
00177 L
"Tag56\0"
00178 L
"Tag57\0"
00179 L
"Tag58\0"
00180 L
"Tag59\0"
00181 L
"Tag60\0"
00182 );
00183
00184
NumberOfHeapEntries = 1000;
00185
HeapEntries = VirtualAlloc( NULL,
00186 NumberOfHeapEntries *
sizeof( *HeapEntries ),
00187 MEM_COMMIT,
00188 PAGE_READWRITE
00189 );
00190
if (
HeapEntries ==
NULL) {
00191 fprintf( stderr,
"THEAP: Unable to allocate space.\n" );
00192
exit( 1 );
00193 }
00194
00195
RtlpHeapValidateOnCall=
TRUE;
00196
00197
00198
00199 TotalAllocated = 0;
00200
while (TotalAllocated < (2 * 1024 * 1024)) {
00201 i =
RtlUniform( &Seed ) %
NumberOfHeapEntries;
00202
if (
RtlUniform( &Seed ) % 100) {
00203
n =
RtlUniform( &Seed ) %
REASONABLE_HEAP_ALLOC;
00204 }
00205
else {
00206
n =
RtlUniform( &Seed ) %
MAX_HEAP_ALLOC;
00207 }
00208
00209
#if 0
00210
Usage.Length =
sizeof(
Usage );
00211
Status =
RtlUsageHeap( Heap, HEAP_USAGE_ALLOCATED_BLOCKS , &Usage );
00212
if (
NT_SUCCESS( Status )) {
00213
if (
Status == STATUS_MORE_ENTRIES) {
00214 pEntries =
Usage.AddedEntries;
00215
while (pEntries) {
00216 fprintf( stderr,
00217
"Added: %08x %06x\n",
00218 pEntries->Address,
00219 pEntries->Size
00220 );
00221 pEntries = pEntries->Next;
00222 }
00223
00224 pEntries =
Usage.RemovedEntries;
00225
while (pEntries) {
00226 fprintf( stderr,
00227
"Freed: %08x %06x\n",
00228 pEntries->Address,
00229 pEntries->Size
00230 );
00231 pEntries = pEntries->Next;
00232 }
00233 }
00234
00235 fprintf( stderr,
"%08x %08x %08x %08x ",
00236
Usage.BytesAllocated,
Usage.BytesCommitted,
00237
Usage.BytesReserved,
Usage.BytesReservedMaximum
00238 );
00239 }
00240
else {
00241 fprintf( stderr,
"RtlUsageHeap failed with status %x\n", Status );
00242 DebugBreak();
00243 }
00244
00245
if (i < 60) {
00246 Tag = (TagBaseIndex + i + 1) << 16;
00247 }
00248
else {
00249 Tag = 0;
00250 }
00251
#endif
00252
Tag = 0;
00253 p = &
HeapEntries[ i ];
00254
if (p->
AllocatedBlock ==
NULL) {
00255 TotalAllocated +=
n;
00256 p->
AllocatedBlock =
RtlAllocateHeap( Heap, Tag, n );
00257 fprintf( stderr,
"Allocated %06x bytes at %08x\n", n, p->
AllocatedBlock );
00258
if (p->
AllocatedBlock !=
NULL) {
00259 p->
Size =
n;
00260 }
00261
else {
00262 DebugBreak();
00263 }
00264 }
00265
else
00266
if (
RtlUniform( &Seed ) & 1) {
00267 TotalAllocated -= p->
Size;
00268 TotalAllocated +=
n;
00269 AllocatedBlock =
RtlReAllocateHeap( Heap, Tag, p->
AllocatedBlock, n );
00270 fprintf( stderr,
"ReAlloced %06x bytes at %08x to %06x bytes at %08x\n",
00271 p->
Size,
00272 p->
AllocatedBlock,
00273 n,
00274 AllocatedBlock
00275 );
00276
if (AllocatedBlock !=
NULL) {
00277 p->
AllocatedBlock = AllocatedBlock;
00278 p->
Size =
n;
00279 }
00280
else {
00281 DebugBreak();
00282 }
00283 }
00284
else {
00285 TotalAllocated -= p->
Size;
00286 Result =
RtlFreeHeap( Heap, 0, p->
AllocatedBlock );
00287 fprintf( stderr,
"Freed %06x bytes at %08x\n",
00288 p->
Size,
00289 p->
AllocatedBlock
00290 );
00291
if (Result) {
00292 p->
AllocatedBlock =
NULL;
00293 p->
Size = 0;
00294 }
00295
else {
00296 DebugBreak();
00297 }
00298 }
00299
00300 }
00301
00302 RtlResetHeap( Heap, 0 );
00303
RtlValidateHeap( Heap, 0, NULL );
00304
00305
return 0;
00306 }