1 | /* $Id: heaptest.c,v 1.5 2000-01-24 18:18:59 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 16384
|
---|
16 | #define RANDOMTEST_ITERATIONS 65536
|
---|
17 | #define EXTRA_HEAPCHECK
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Internal Functions
|
---|
21 | *******************************************************************************/
|
---|
22 | /*#include "malloc.h"*/
|
---|
23 | #include "rmalloc.h"
|
---|
24 | #include "smalloc.h"
|
---|
25 | #include "macros.h"
|
---|
26 | #include "asmutils.h"
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <memory.h>
|
---|
30 |
|
---|
31 |
|
---|
32 |
|
---|
33 |
|
---|
34 | int 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;
|
---|
42 | unsigned crealloc;
|
---|
43 | int i;
|
---|
44 | int fResTests = 1;
|
---|
45 | int fResSimple = 1;
|
---|
46 | int fResRandom = 1;
|
---|
47 | int fSwpTests = 1;
|
---|
48 | int fSwpSimple = 1;
|
---|
49 | int fSwpRandom = 1;
|
---|
50 | enum {malloc,realloc, free, unknown} enmLast = unknown;
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * Initiate the heap.
|
---|
54 | */
|
---|
55 | if (resHeapInit(128*1024, 1024*1024*64) != 0)
|
---|
56 | {
|
---|
57 | printf("Failed to initiate the resident heap.\n");
|
---|
58 | return 1;
|
---|
59 | }
|
---|
60 |
|
---|
61 | if (swpHeapInit(128*1024, 1024*1024*64) != 0)
|
---|
62 | {
|
---|
63 | printf("Failed to initiate the swappable heap.\n");
|
---|
64 | return 2;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 |
|
---|
69 | if (fResTests)
|
---|
70 | {
|
---|
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++)
|
---|
83 | {
|
---|
84 | do
|
---|
85 | {
|
---|
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]);
|
---|
92 | enmLast = malloc;
|
---|
93 | if (apv[i] == NULL)
|
---|
94 | {
|
---|
95 | printf("rmalloc failed: acb[%d]=%d\n", i, acb[i]);
|
---|
96 | if (acb[i] > 1000)
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | memset(apv[i], 0xA, acb[i]);
|
---|
100 | cb += acb[i];
|
---|
101 | }
|
---|
102 |
|
---|
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++)
|
---|
110 | {
|
---|
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]);
|
---|
114 | memset(apv[i], 0xF, 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, acb[j]);
|
---|
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 | memset(apv[j], 0xB, acb[j]);
|
---|
239 | }
|
---|
240 | else
|
---|
241 | { /* free */
|
---|
242 | memset(apv[j], 0xF, acb[j]);
|
---|
243 | rfree(apv[j]);
|
---|
244 | enmLast = free;
|
---|
245 | apv[j] = NULL;
|
---|
246 | cAllocations--;
|
---|
247 | crfree++;
|
---|
248 | }
|
---|
249 | }
|
---|
250 | }
|
---|
251 | #ifdef EXTRA_HEAPCHECK
|
---|
252 | _res_heap_check();
|
---|
253 | #endif
|
---|
254 | if (RANDOMTEST_ITERATIONS/2 == i)
|
---|
255 | _res_dump_subheaps();
|
---|
256 | if ((i % 2048) == 0)
|
---|
257 | printf("i=%d cAllocations=%d\n", i, cAllocations);
|
---|
258 | }
|
---|
259 |
|
---|
260 | printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
|
---|
261 |
|
---|
262 | printf("_res_dump_subheaps:\n");
|
---|
263 | _res_dump_subheaps();
|
---|
264 |
|
---|
265 | printf("_res_memfree - before heapmin: %d\n", _res_memfree());
|
---|
266 | _res_heapmin();
|
---|
267 | printf("_res_memfree - after heapmin : %d\n", _res_memfree());
|
---|
268 |
|
---|
269 | printf("_res_dump_subheaps:\n");
|
---|
270 | _res_dump_subheaps();
|
---|
271 | } /* fResRandom */
|
---|
272 | } /* fResTests */
|
---|
273 |
|
---|
274 |
|
---|
275 | /*
|
---|
276 | *
|
---|
277 | * swappable heap tests
|
---|
278 | * swappable heap tests
|
---|
279 | *
|
---|
280 | */
|
---|
281 | if (fSwpTests)
|
---|
282 | {
|
---|
283 | printf("\nSwappable heap tests\nSwappable heap tests\n");
|
---|
284 | if (fSwpSimple)
|
---|
285 | {
|
---|
286 | /*
|
---|
287 | * Simple allocation test.
|
---|
288 | */
|
---|
289 | printf("\nSimple swappable heap tests\nSimple swappable heap tests\n");
|
---|
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);
|
---|
296 |
|
---|
297 | if ((i % (NUMBER_OF_POINTERS/3)) == 1)
|
---|
298 | acb[i] += 1024*260;
|
---|
299 | apv[i] = smalloc(acb[i]);
|
---|
300 | enmLast = malloc;
|
---|
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 | }
|
---|
307 | memset(apv[i], 0xA, acb[i]);
|
---|
308 | cb += acb[i];
|
---|
309 | }
|
---|
310 |
|
---|
311 | printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
|
---|
312 |
|
---|
313 |
|
---|
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]);
|
---|
322 | memset(apv[i], 0xF, acb[i]);
|
---|
323 | sfree(apv[i]);
|
---|
324 | enmLast = free;
|
---|
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"
|
---|
349 | "Random allocation and freeing test (swappable)\n"
|
---|
350 | "Random allocation and freeing test (swappable)\n"
|
---|
351 | );
|
---|
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]);
|
---|
383 | enmLast = malloc;
|
---|
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 | }
|
---|
391 | memset(apv[j], 0xA, acb[j]);
|
---|
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);
|
---|
440 | enmLast = realloc;
|
---|
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;
|
---|
448 | memset(apv[j], 0xB, acb[j]);
|
---|
449 | }
|
---|
450 | else
|
---|
451 | { /* free */
|
---|
452 | memset(apv[j], 0xF, acb[j]);
|
---|
453 | sfree(apv[j]);
|
---|
454 | enmLast = free;
|
---|
455 | apv[j] = NULL;
|
---|
456 | cAllocations--;
|
---|
457 | crfree++;
|
---|
458 | }
|
---|
459 | }
|
---|
460 | }
|
---|
461 | #ifdef EXTRA_HEAPCHECK
|
---|
462 | _swp_heap_check();
|
---|
463 | #endif
|
---|
464 | if (RANDOMTEST_ITERATIONS/2 == i)
|
---|
465 | {
|
---|
466 | _swp_dump_subheaps();
|
---|
467 | _res_dump_subheaps();
|
---|
468 | }
|
---|
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();
|
---|
477 | printf("_res_dump_subheaps:\n");
|
---|
478 | _res_dump_subheaps();
|
---|
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();
|
---|
486 | printf("_res_dump_subheaps:\n");
|
---|
487 | _res_dump_subheaps();
|
---|
488 | } /* fSwpRandom */
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 |
|
---|
493 | /* unreferenced parameters */
|
---|
494 | argc = argc;
|
---|
495 | argv = argv;
|
---|
496 |
|
---|
497 | return 0;
|
---|
498 | }
|
---|
499 |
|
---|
500 |
|
---|
501 |
|
---|