source: trunk/src/win32k/misc/heaptest.c

Last change on this file was 2511, checked in by bird, 26 years ago

Heapchanges: Heap is splitted into a swappable and a resident. The heaps
are dynamically growable.

File size: 16.9 KB
RevLine 
[2511]1/* $Id: heaptest.c,v 1.5 2000-01-24 18:18:59 bird Exp $
[2501]2 *
3 * Test of resident and swappable heaps.
4 *
5 *
6 * Copyright (c) 2000 knut st. osmundsen
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11
12/******************************************************************************
13* Defined Constants
14*******************************************************************************/
[2511]15#define NUMBER_OF_POINTERS 16384
16#define RANDOMTEST_ITERATIONS 65536
17#define EXTRA_HEAPCHECK
[2501]18
19/*******************************************************************************
20* Internal Functions
21*******************************************************************************/
[2507]22/*#include "malloc.h"*/
[2501]23#include "rmalloc.h"
[2507]24#include "smalloc.h"
[2503]25#include "macros.h"
[2511]26#include "asmutils.h"
[2501]27#include <stdio.h>
28#include <stdlib.h>
[2503]29#include <memory.h>
[2501]30
31
32
33
34int main(int argc, char *argv)
35{
36 static void * apv[NUMBER_OF_POINTERS];
37 static int acb[NUMBER_OF_POINTERS];
38 unsigned cAllocations;
39 unsigned cb = 0;
40 unsigned crmalloc;
41 unsigned crfree;
[2503]42 unsigned crealloc;
[2501]43 int i;
[2511]44 int fResTests = 1;
45 int fResSimple = 1;
46 int fResRandom = 1;
[2507]47 int fSwpTests = 1;
[2511]48 int fSwpSimple = 1;
[2507]49 int fSwpRandom = 1;
[2508]50 enum {malloc,realloc, free, unknown} enmLast = unknown;
[2501]51
52 /*
53 * Initiate the heap.
54 */
[2507]55 if (resHeapInit(128*1024, 1024*1024*64) != 0)
[2501]56 {
57 printf("Failed to initiate the resident heap.\n");
58 return 1;
59 }
60
[2507]61 if (swpHeapInit(128*1024, 1024*1024*64) != 0)
[2501]62 {
[2507]63 printf("Failed to initiate the swappable heap.\n");
64 return 2;
[2501]65 }
66
67
68
[2507]69 if (fResTests)
[2501]70 {
[2507]71 /*
72 *
73 * resident heap tests
74 * resident heap tests
75 *
76 */
77 if (fResSimple)
78 {
79 /*
80 * Simple allocation test.
81 */
82 for (i = 0; i < NUMBER_OF_POINTERS; i++)
[2501]83 {
84 do
85 {
[2507]86 acb[i] = rand();
87 } while(acb[i] == 0 || acb[i] > 127);
88
89 if ((i % (NUMBER_OF_POINTERS/3)) == 1)
90 acb[i] += 1024*260;
91 apv[i] = rmalloc(acb[i]);
[2508]92 enmLast = malloc;
[2507]93 if (apv[i] == NULL)
[2501]94 {
[2507]95 printf("rmalloc failed: acb[%d]=%d\n", i, acb[i]);
96 if (acb[i] > 1000)
97 break;
[2501]98 }
[2511]99 memset(apv[i], 0xA, acb[i]);
[2507]100 cb += acb[i];
[2501]101 }
[2503]102
[2507]103 printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
104
105
106 printf("_res_dump_subheaps:\n");
107 _res_dump_subheaps();
108
109 for (i = 0; i < NUMBER_OF_POINTERS; i++)
[2501]110 {
[2507]111 int cb = _res_msize(apv[i]);
112 if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
113 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
[2511]114 memset(apv[i], 0xF, acb[i]);
[2507]115 rfree(apv[i]);
[2508]116 enmLast = free;
[2501]117 }
[2507]118
119
120 /*
121 * test _res_heapmin
122 */
123 printf("_res_memfree - before heapmin: %d\n", _res_memfree());
124 _res_heapmin();
125 printf("_res_memfree - after heapmin : %d\n", _res_memfree());
126
127 /*
128 * Test _res_dump_subheaps
129 */
130 printf("\n_res_dump_subheaps:\n");
131 _res_dump_subheaps();
132 } /* fResSimple */
133
134
135 if (fResRandom)
136 {
137 /*
138 * Test 2 - random allocation and freeing of memory.
139 */
140 printf("\n"
141 "Random allocation and freeing test:\n");
142 for (i = 0; i < NUMBER_OF_POINTERS; i++)
143 apv[i] = NULL, acb[i] = 0;
144 cAllocations = 0;
145 i = 0;
146 cb = 0;
147 crfree = 0;
148 crmalloc = 0;
149 crealloc = 0;
150 while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
[2501]151 {
[2507]152 int j;
153 j = rand();
154 if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
155 (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 4 :
156 i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 5 :
157 i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 6 :
158 i < RANDOMTEST_ITERATIONS ? (j % 8) > 6 : 0
159 )
160 )
161 { /* malloc */
162 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
[2501]163 (void)0;
[2507]164 if (j < NUMBER_OF_POINTERS)
165 {
166 do
167 {
168 acb[j] = rand();
169 } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
170 if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
171 acb[j] += 1024*256;
172 apv[j] = rmalloc(acb[j]);
[2508]173 enmLast = malloc;
[2507]174 if (apv[j] == NULL)
175 {
176 printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
177 if (acb[j] > 10000)
178 continue;
179 break;
180 }
[2511]181 memset(apv[j], 0xA, acb[j]);
[2507]182 cAllocations++;
183 cb += acb[j];
184 crmalloc++;
185 }
[2501]186 }
[2507]187 else
188 { /* free or realloc */
189 if (cAllocations == 0)
190 continue;
[2501]191
[2507]192 if (cAllocations < NUMBER_OF_POINTERS/10)
[2503]193 {
[2507]194 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
195 (void)0;
196 }
197 else
[2503]198 {
[2507]199 int k = 0;
200 do
201 {
202 j = rand();
203 } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
204 if (k >= NUMBER_OF_POINTERS/2)
205 {
206 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
207 (void)0;
208 }
[2503]209 }
[2507]210
211 if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
212 {
213 int cb = _res_msize(apv[j]);
214 if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
215 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
216 if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
217 { /* realloc */
218 int cb;
219 void *pv;
220 crealloc++;
221 do
222 {
223 cb = rand();
224 } while (cb == 0 || cb > 3072);
225 /*
226 if (i >= 0x1c14)
227 Int3();
228 */
229 pv = rrealloc(apv[j], cb);
[2508]230 enmLast = realloc;
[2507]231 if (pv == NULL)
232 {
233 printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
234 continue;
235 }
236 apv[j] = pv;
237 acb[j] = cb;
[2511]238 memset(apv[j], 0xB, acb[j]);
[2507]239 }
240 else
241 { /* free */
[2511]242 memset(apv[j], 0xF, acb[j]);
[2507]243 rfree(apv[j]);
[2508]244 enmLast = free;
[2507]245 apv[j] = NULL;
246 cAllocations--;
247 crfree++;
248 }
249 }
[2503]250 }
[2511]251 #ifdef EXTRA_HEAPCHECK
[2507]252 _res_heap_check();
[2511]253 #endif
[2507]254 if (RANDOMTEST_ITERATIONS/2 == i)
255 _res_dump_subheaps();
256 if ((i % 2048) == 0)
257 printf("i=%d cAllocations=%d\n", i, cAllocations);
[2501]258 }
259
[2507]260 printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
[2501]261
[2507]262 printf("_res_dump_subheaps:\n");
263 _res_dump_subheaps();
[2501]264
[2507]265 printf("_res_memfree - before heapmin: %d\n", _res_memfree());
266 _res_heapmin();
267 printf("_res_memfree - after heapmin : %d\n", _res_memfree());
[2501]268
[2507]269 printf("_res_dump_subheaps:\n");
270 _res_dump_subheaps();
271 } /* fResRandom */
272 } /* fResTests */
[2501]273
274
275/*
276 *
277 * swappable heap tests
278 * swappable heap tests
279 *
280 */
[2507]281 if (fSwpTests)
282 {
[2511]283 printf("\nSwappable heap tests\nSwappable heap tests\n");
[2507]284 if (fSwpSimple)
285 {
286 /*
287 * Simple allocation test.
288 */
[2511]289 printf("\nSimple swappable heap tests\nSimple swappable heap tests\n");
[2507]290 for (i = 0; i < NUMBER_OF_POINTERS; i++)
291 {
292 do
293 {
294 acb[i] = rand();
295 } while(acb[i] == 0 || acb[i] > 127);
[2501]296
[2507]297 if ((i % (NUMBER_OF_POINTERS/3)) == 1)
298 acb[i] += 1024*260;
299 apv[i] = smalloc(acb[i]);
[2508]300 enmLast = malloc;
[2507]301 if (apv[i] == NULL)
302 {
303 printf("smalloc failed: acb[%d]=%d\n", i, acb[i]);
304 if (acb[i] > 1000)
305 break;
306 }
[2511]307 memset(apv[i], 0xA, acb[i]);
[2507]308 cb += acb[i];
309 }
[2501]310
[2507]311 printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
[2501]312
313
[2507]314 printf("_swp_dump_subheaps:\n");
315 _swp_dump_subheaps();
316
317 for (i = 0; i < NUMBER_OF_POINTERS; i++)
318 {
319 int cb = _swp_msize(apv[i]);
320 if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
321 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
[2511]322 memset(apv[i], 0xF, acb[i]);
[2507]323 sfree(apv[i]);
[2508]324 enmLast = free;
[2507]325 }
326
327
328 /*
329 * test _swp_heapmin
330 */
331 printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
332 _swp_heapmin();
333 printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
334
335 /*
336 * Test _swp_dump_subheaps
337 */
338 printf("\n_swp_dump_subheaps:\n");
339 _swp_dump_subheaps();
340 } /* fSwpSimple */
341
342
343 if (fSwpRandom)
344 {
345 /*
346 * Test 2 - random allocation and freeing of memory.
347 */
348 printf("\n"
[2511]349 "Random allocation and freeing test (swappable)\n"
350 "Random allocation and freeing test (swappable)\n"
351 );
[2507]352 for (i = 0; i < NUMBER_OF_POINTERS; i++)
353 apv[i] = NULL, acb[i] = 0;
354 cAllocations = 0;
355 i = 0;
356 cb = 0;
357 crfree = 0;
358 crmalloc = 0;
359 crealloc = 0;
360 while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
361 {
362 int j;
363 j = rand();
364 if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
365 (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 4 :
366 i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 5 :
367 i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 6 :
368 i < RANDOMTEST_ITERATIONS ? (j % 8) > 6 : 0
369 )
370 )
371 { /* malloc */
372 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
373 (void)0;
374 if (j < NUMBER_OF_POINTERS)
375 {
376 do
377 {
378 acb[j] = rand();
379 } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
380 if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
381 acb[j] += 1024*256;
382 apv[j] = smalloc(acb[j]);
[2508]383 enmLast = malloc;
[2507]384 if (apv[j] == NULL)
385 {
386 printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
387 if (acb[j] > 10000)
388 continue;
389 break;
390 }
[2511]391 memset(apv[j], 0xA, acb[j]);
[2507]392 cAllocations++;
393 cb += acb[j];
394 crmalloc++;
395 }
396 }
397 else
398 { /* free or realloc */
399 if (cAllocations == 0)
400 continue;
401
402 if (cAllocations < NUMBER_OF_POINTERS/10)
403 {
404 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
405 (void)0;
406 }
407 else
408 {
409 int k = 0;
410 do
411 {
412 j = rand();
413 } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
414 if (k >= NUMBER_OF_POINTERS/2)
415 {
416 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
417 (void)0;
418 }
419 }
420
421 if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
422 {
423 int cb = _swp_msize(apv[j]);
424 if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
425 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
426 if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
427 { /* realloc */
428 int cb;
429 void *pv;
430 crealloc++;
431 do
432 {
433 cb = rand();
434 } while (cb == 0 || cb > 3072);
435 /*
436 if (i >= 0x1c14)
437 Int3();
438 */
439 pv = srealloc(apv[j], cb);
[2508]440 enmLast = realloc;
[2507]441 if (pv == NULL)
442 {
443 printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
444 continue;
445 }
446 apv[j] = pv;
447 acb[j] = cb;
[2511]448 memset(apv[j], 0xB, acb[j]);
[2507]449 }
450 else
451 { /* free */
[2511]452 memset(apv[j], 0xF, acb[j]);
[2507]453 sfree(apv[j]);
[2508]454 enmLast = free;
[2507]455 apv[j] = NULL;
456 cAllocations--;
457 crfree++;
458 }
459 }
460 }
[2511]461 #ifdef EXTRA_HEAPCHECK
[2507]462 _swp_heap_check();
[2511]463 #endif
[2507]464 if (RANDOMTEST_ITERATIONS/2 == i)
[2511]465 {
[2507]466 _swp_dump_subheaps();
[2511]467 _res_dump_subheaps();
468 }
[2507]469 if ((i % 2048) == 0)
470 printf("i=%d cAllocations=%d\n", i, cAllocations);
471 }
472
473 printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
474
475 printf("_swp_dump_subheaps:\n");
476 _swp_dump_subheaps();
[2511]477 printf("_res_dump_subheaps:\n");
478 _res_dump_subheaps();
[2507]479
480 printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
481 _swp_heapmin();
482 printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
483
484 printf("_swp_dump_subheaps:\n");
485 _swp_dump_subheaps();
[2511]486 printf("_res_dump_subheaps:\n");
487 _res_dump_subheaps();
[2507]488 } /* fSwpRandom */
489 }
490
491
492
[2501]493 /* unreferenced parameters */
494 argc = argc;
495 argv = argv;
496
497 return 0;
498}
499
500
501
Note: See TracBrowser for help on using the repository browser.