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

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

Corrections. Still some bugs in smalloc_avl.c.

File size: 16.1 KB
Line 
1/* $Id: heaptest.c,v 1.4 2000-01-24 03:05:13 bird Exp $
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*******************************************************************************/
15#define NUMBER_OF_POINTERS 1024
16#define RANDOMTEST_ITERATIONS 65536*2
17#define Int3() __interrupt(3)
18
19
20/*******************************************************************************
21* Internal Functions
22*******************************************************************************/
23/*#include "malloc.h"*/
24#include "rmalloc.h"
25#include "smalloc.h"
26#include "macros.h"
27#include <stdio.h>
28#include <stdlib.h>
29#include <memory.h>
30#include <builtin.h>
31
32
33
34
35int main(int argc, char *argv)
36{
37 static void * apv[NUMBER_OF_POINTERS];
38 static int acb[NUMBER_OF_POINTERS];
39 unsigned cAllocations;
40 unsigned cb = 0;
41 unsigned crmalloc;
42 unsigned crfree;
43 unsigned crealloc;
44 int i;
45 int fResTests = 0;
46 int fResSimple = 0;
47 int fResRandom = 0;
48 int fSwpTests = 1;
49 int fSwpSimple = 0;
50 int fSwpRandom = 1;
51 enum {malloc,realloc, free, unknown} enmLast = unknown;
52
53 /*
54 * Initiate the heap.
55 */
56 if (resHeapInit(128*1024, 1024*1024*64) != 0)
57 {
58 printf("Failed to initiate the resident heap.\n");
59 return 1;
60 }
61
62 if (swpHeapInit(128*1024, 1024*1024*64) != 0)
63 {
64 printf("Failed to initiate the swappable heap.\n");
65 return 2;
66 }
67
68
69
70 if (fResTests)
71 {
72 /*
73 *
74 * resident heap tests
75 * resident heap tests
76 *
77 */
78 if (fResSimple)
79 {
80 /*
81 * Simple allocation test.
82 */
83 for (i = 0; i < NUMBER_OF_POINTERS; i++)
84 {
85 do
86 {
87 acb[i] = rand();
88 } while(acb[i] == 0 || acb[i] > 127);
89
90 if ((i % (NUMBER_OF_POINTERS/3)) == 1)
91 acb[i] += 1024*260;
92 apv[i] = rmalloc(acb[i]);
93 enmLast = malloc;
94 if (apv[i] == NULL)
95 {
96 printf("rmalloc failed: acb[%d]=%d\n", i, acb[i]);
97 if (acb[i] > 1000)
98 break;
99 }
100 memset(apv[i], 0xA, MIN(acb[i],16));
101 cb += acb[i];
102 }
103
104 printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
105
106
107 printf("_res_dump_subheaps:\n");
108 _res_dump_subheaps();
109
110 for (i = 0; i < NUMBER_OF_POINTERS; i++)
111 {
112 int cb = _res_msize(apv[i]);
113 if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
114 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
115 rfree(apv[i]);
116 enmLast = free;
117 }
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)
151 {
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++)
163 (void)0;
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]);
173 enmLast = malloc;
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 }
181 memset(apv[j], 0xA, MIN(acb[j],16));
182 cAllocations++;
183 cb += acb[j];
184 crmalloc++;
185 }
186 }
187 else
188 { /* free or realloc */
189 if (cAllocations == 0)
190 continue;
191
192 if (cAllocations < NUMBER_OF_POINTERS/10)
193 {
194 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
195 (void)0;
196 }
197 else
198 {
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 }
209 }
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);
230 enmLast = realloc;
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;
238 }
239 else
240 { /* free */
241 rfree(apv[j]);
242 enmLast = free;
243 apv[j] = NULL;
244 cAllocations--;
245 crfree++;
246 }
247 }
248 }
249 _res_heap_check();
250 if (RANDOMTEST_ITERATIONS/2 == i)
251 _res_dump_subheaps();
252 if ((i % 2048) == 0)
253 printf("i=%d cAllocations=%d\n", i, cAllocations);
254 }
255
256 printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
257
258 printf("_res_dump_subheaps:\n");
259 _res_dump_subheaps();
260
261 printf("_res_memfree - before heapmin: %d\n", _res_memfree());
262 _res_heapmin();
263 printf("_res_memfree - after heapmin : %d\n", _res_memfree());
264
265 printf("_res_dump_subheaps:\n");
266 _res_dump_subheaps();
267 } /* fResRandom */
268 } /* fResTests */
269
270
271/*
272 *
273 * swappable heap tests
274 * swappable heap tests
275 *
276 */
277 if (fSwpTests)
278 {
279 if (fSwpSimple)
280 {
281 /*
282 * Simple allocation test.
283 */
284 for (i = 0; i < NUMBER_OF_POINTERS; i++)
285 {
286 do
287 {
288 acb[i] = rand();
289 } while(acb[i] == 0 || acb[i] > 127);
290
291 if ((i % (NUMBER_OF_POINTERS/3)) == 1)
292 acb[i] += 1024*260;
293 apv[i] = smalloc(acb[i]);
294 enmLast = malloc;
295 if (apv[i] == NULL)
296 {
297 printf("smalloc failed: acb[%d]=%d\n", i, acb[i]);
298 if (acb[i] > 1000)
299 break;
300 }
301 memset(apv[i], 0xA, MIN(acb[i],16));
302 cb += acb[i];
303 }
304
305 printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
306
307
308 printf("_swp_dump_subheaps:\n");
309 _swp_dump_subheaps();
310
311 for (i = 0; i < NUMBER_OF_POINTERS; i++)
312 {
313 int cb = _swp_msize(apv[i]);
314 if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
315 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
316 sfree(apv[i]);
317 enmLast = free;
318 }
319
320
321 /*
322 * test _swp_heapmin
323 */
324 printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
325 _swp_heapmin();
326 printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
327
328 /*
329 * Test _swp_dump_subheaps
330 */
331 printf("\n_swp_dump_subheaps:\n");
332 _swp_dump_subheaps();
333 } /* fSwpSimple */
334
335
336 if (fSwpRandom)
337 {
338 /*
339 * Test 2 - random allocation and freeing of memory.
340 */
341 printf("\n"
342 "Random allocation and freeing test:\n");
343 for (i = 0; i < NUMBER_OF_POINTERS; i++)
344 apv[i] = NULL, acb[i] = 0;
345 cAllocations = 0;
346 i = 0;
347 cb = 0;
348 crfree = 0;
349 crmalloc = 0;
350 crealloc = 0;
351 while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
352 {
353 int j;
354 j = rand();
355 if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
356 (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 4 :
357 i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 5 :
358 i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 6 :
359 i < RANDOMTEST_ITERATIONS ? (j % 8) > 6 : 0
360 )
361 )
362 { /* malloc */
363 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
364 (void)0;
365 if (j < NUMBER_OF_POINTERS)
366 {
367 do
368 {
369 acb[j] = rand();
370 } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
371 if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
372 acb[j] += 1024*256;
373 apv[j] = smalloc(acb[j]);
374 enmLast = malloc;
375 if (apv[j] == NULL)
376 {
377 printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
378 if (acb[j] > 10000)
379 continue;
380 break;
381 }
382 memset(apv[j], 0xA, MIN(acb[j],16));
383 cAllocations++;
384 cb += acb[j];
385 crmalloc++;
386 }
387 }
388 else
389 { /* free or realloc */
390 if (cAllocations == 0)
391 continue;
392
393 if (cAllocations < NUMBER_OF_POINTERS/10)
394 {
395 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
396 (void)0;
397 }
398 else
399 {
400 int k = 0;
401 do
402 {
403 j = rand();
404 } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
405 if (k >= NUMBER_OF_POINTERS/2)
406 {
407 for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
408 (void)0;
409 }
410 }
411
412 if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
413 {
414 int cb = _swp_msize(apv[j]);
415 if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
416 printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
417 if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
418 { /* realloc */
419 int cb;
420 void *pv;
421 crealloc++;
422 do
423 {
424 cb = rand();
425 } while (cb == 0 || cb > 3072);
426 /*
427 if (i >= 0x1c14)
428 Int3();
429 */
430 pv = srealloc(apv[j], cb);
431 enmLast = realloc;
432 if (pv == NULL)
433 {
434 printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
435 continue;
436 }
437 apv[j] = pv;
438 acb[j] = cb;
439 }
440 else
441 { /* free */
442 sfree(apv[j]);
443 enmLast = free;
444 apv[j] = NULL;
445 cAllocations--;
446 crfree++;
447 }
448 }
449 }
450 _swp_heap_check();
451 if (RANDOMTEST_ITERATIONS/2 == i)
452 _swp_dump_subheaps();
453 if ((i % 2048) == 0)
454 printf("i=%d cAllocations=%d\n", i, cAllocations);
455 }
456
457 printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
458
459 printf("_swp_dump_subheaps:\n");
460 _swp_dump_subheaps();
461
462 printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
463 _swp_heapmin();
464 printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
465
466 printf("_swp_dump_subheaps:\n");
467 _swp_dump_subheaps();
468 } /* fSwpRandom */
469 }
470
471
472
473 /* unreferenced parameters */
474 argc = argc;
475 argv = argv;
476
477 return 0;
478}
479
480
481
Note: See TracBrowser for help on using the repository browser.