1 | /* $Id: heaptest.c,v 1.3 2000-01-24 01:45:20 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 |
|
---|
35 | int 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 = 1;
|
---|
50 | int fSwpRandom = 1;
|
---|
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 | if (apv[i] == NULL)
|
---|
93 | {
|
---|
94 | printf("rmalloc failed: acb[%d]=%d\n", i, acb[i]);
|
---|
95 | if (acb[i] > 1000)
|
---|
96 | break;
|
---|
97 | }
|
---|
98 | memset(apv[i], 0xA, MIN(acb[i],16));
|
---|
99 | cb += acb[i];
|
---|
100 | }
|
---|
101 |
|
---|
102 | printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
|
---|
103 |
|
---|
104 |
|
---|
105 | printf("_res_dump_subheaps:\n");
|
---|
106 | _res_dump_subheaps();
|
---|
107 |
|
---|
108 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
---|
109 | {
|
---|
110 | int cb = _res_msize(apv[i]);
|
---|
111 | if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
|
---|
112 | printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
|
---|
113 | rfree(apv[i]);
|
---|
114 | }
|
---|
115 |
|
---|
116 |
|
---|
117 | /*
|
---|
118 | * test _res_heapmin
|
---|
119 | */
|
---|
120 | printf("_res_memfree - before heapmin: %d\n", _res_memfree());
|
---|
121 | _res_heapmin();
|
---|
122 | printf("_res_memfree - after heapmin : %d\n", _res_memfree());
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Test _res_dump_subheaps
|
---|
126 | */
|
---|
127 | printf("\n_res_dump_subheaps:\n");
|
---|
128 | _res_dump_subheaps();
|
---|
129 | } /* fResSimple */
|
---|
130 |
|
---|
131 |
|
---|
132 | if (fResRandom)
|
---|
133 | {
|
---|
134 | /*
|
---|
135 | * Test 2 - random allocation and freeing of memory.
|
---|
136 | */
|
---|
137 | printf("\n"
|
---|
138 | "Random allocation and freeing test:\n");
|
---|
139 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
---|
140 | apv[i] = NULL, acb[i] = 0;
|
---|
141 | cAllocations = 0;
|
---|
142 | i = 0;
|
---|
143 | cb = 0;
|
---|
144 | crfree = 0;
|
---|
145 | crmalloc = 0;
|
---|
146 | crealloc = 0;
|
---|
147 | while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
|
---|
148 | {
|
---|
149 | int j;
|
---|
150 | j = rand();
|
---|
151 | if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
|
---|
152 | (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 4 :
|
---|
153 | i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 5 :
|
---|
154 | i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 6 :
|
---|
155 | i < RANDOMTEST_ITERATIONS ? (j % 8) > 6 : 0
|
---|
156 | )
|
---|
157 | )
|
---|
158 | { /* malloc */
|
---|
159 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
|
---|
160 | (void)0;
|
---|
161 | if (j < NUMBER_OF_POINTERS)
|
---|
162 | {
|
---|
163 | do
|
---|
164 | {
|
---|
165 | acb[j] = rand();
|
---|
166 | } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
|
---|
167 | if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
|
---|
168 | acb[j] += 1024*256;
|
---|
169 | apv[j] = rmalloc(acb[j]);
|
---|
170 | if (apv[j] == NULL)
|
---|
171 | {
|
---|
172 | printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
|
---|
173 | if (acb[j] > 10000)
|
---|
174 | continue;
|
---|
175 | break;
|
---|
176 | }
|
---|
177 | memset(apv[j], 0xA, MIN(acb[j],16));
|
---|
178 | cAllocations++;
|
---|
179 | cb += acb[j];
|
---|
180 | crmalloc++;
|
---|
181 | }
|
---|
182 | }
|
---|
183 | else
|
---|
184 | { /* free or realloc */
|
---|
185 | if (cAllocations == 0)
|
---|
186 | continue;
|
---|
187 |
|
---|
188 | if (cAllocations < NUMBER_OF_POINTERS/10)
|
---|
189 | {
|
---|
190 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
---|
191 | (void)0;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | {
|
---|
195 | int k = 0;
|
---|
196 | do
|
---|
197 | {
|
---|
198 | j = rand();
|
---|
199 | } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
|
---|
200 | if (k >= NUMBER_OF_POINTERS/2)
|
---|
201 | {
|
---|
202 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
---|
203 | (void)0;
|
---|
204 | }
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
|
---|
208 | {
|
---|
209 | int cb = _res_msize(apv[j]);
|
---|
210 | if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
|
---|
211 | printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
|
---|
212 | if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
|
---|
213 | { /* realloc */
|
---|
214 | int cb;
|
---|
215 | void *pv;
|
---|
216 | crealloc++;
|
---|
217 | do
|
---|
218 | {
|
---|
219 | cb = rand();
|
---|
220 | } while (cb == 0 || cb > 3072);
|
---|
221 | /*
|
---|
222 | if (i >= 0x1c14)
|
---|
223 | Int3();
|
---|
224 | */
|
---|
225 | pv = rrealloc(apv[j], cb);
|
---|
226 | if (pv == NULL)
|
---|
227 | {
|
---|
228 | printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
|
---|
229 | continue;
|
---|
230 | }
|
---|
231 | apv[j] = pv;
|
---|
232 | acb[j] = cb;
|
---|
233 | }
|
---|
234 | else
|
---|
235 | { /* free */
|
---|
236 | rfree(apv[j]);
|
---|
237 | apv[j] = NULL;
|
---|
238 | cAllocations--;
|
---|
239 | crfree++;
|
---|
240 | }
|
---|
241 | }
|
---|
242 | }
|
---|
243 | _res_heap_check();
|
---|
244 | if (RANDOMTEST_ITERATIONS/2 == i)
|
---|
245 | _res_dump_subheaps();
|
---|
246 | if ((i % 2048) == 0)
|
---|
247 | printf("i=%d cAllocations=%d\n", i, cAllocations);
|
---|
248 | }
|
---|
249 |
|
---|
250 | printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
|
---|
251 |
|
---|
252 | printf("_res_dump_subheaps:\n");
|
---|
253 | _res_dump_subheaps();
|
---|
254 |
|
---|
255 | printf("_res_memfree - before heapmin: %d\n", _res_memfree());
|
---|
256 | _res_heapmin();
|
---|
257 | printf("_res_memfree - after heapmin : %d\n", _res_memfree());
|
---|
258 |
|
---|
259 | printf("_res_dump_subheaps:\n");
|
---|
260 | _res_dump_subheaps();
|
---|
261 | } /* fResRandom */
|
---|
262 | } /* fResTests */
|
---|
263 |
|
---|
264 |
|
---|
265 | /*
|
---|
266 | *
|
---|
267 | * swappable heap tests
|
---|
268 | * swappable heap tests
|
---|
269 | *
|
---|
270 | */
|
---|
271 | if (fSwpTests)
|
---|
272 | {
|
---|
273 | if (fSwpSimple)
|
---|
274 | {
|
---|
275 | /*
|
---|
276 | * Simple allocation test.
|
---|
277 | */
|
---|
278 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
---|
279 | {
|
---|
280 | do
|
---|
281 | {
|
---|
282 | acb[i] = rand();
|
---|
283 | } while(acb[i] == 0 || acb[i] > 127);
|
---|
284 |
|
---|
285 | if ((i % (NUMBER_OF_POINTERS/3)) == 1)
|
---|
286 | acb[i] += 1024*260;
|
---|
287 | apv[i] = smalloc(acb[i]);
|
---|
288 | if (apv[i] == NULL)
|
---|
289 | {
|
---|
290 | printf("smalloc failed: acb[%d]=%d\n", i, acb[i]);
|
---|
291 | if (acb[i] > 1000)
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | memset(apv[i], 0xA, MIN(acb[i],16));
|
---|
295 | cb += acb[i];
|
---|
296 | }
|
---|
297 |
|
---|
298 | printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
|
---|
299 |
|
---|
300 |
|
---|
301 | printf("_swp_dump_subheaps:\n");
|
---|
302 | _swp_dump_subheaps();
|
---|
303 |
|
---|
304 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
---|
305 | {
|
---|
306 | int cb = _swp_msize(apv[i]);
|
---|
307 | if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
|
---|
308 | printf("size of avp[%d] (%d) != acb[%d] (%d)\n", i, cb, i, acb[i]);
|
---|
309 | sfree(apv[i]);
|
---|
310 | }
|
---|
311 |
|
---|
312 |
|
---|
313 | /*
|
---|
314 | * test _swp_heapmin
|
---|
315 | */
|
---|
316 | printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
|
---|
317 | _swp_heapmin();
|
---|
318 | printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
|
---|
319 |
|
---|
320 | /*
|
---|
321 | * Test _swp_dump_subheaps
|
---|
322 | */
|
---|
323 | printf("\n_swp_dump_subheaps:\n");
|
---|
324 | _swp_dump_subheaps();
|
---|
325 | } /* fSwpSimple */
|
---|
326 |
|
---|
327 |
|
---|
328 | if (fSwpRandom)
|
---|
329 | {
|
---|
330 | /*
|
---|
331 | * Test 2 - random allocation and freeing of memory.
|
---|
332 | */
|
---|
333 | printf("\n"
|
---|
334 | "Random allocation and freeing test:\n");
|
---|
335 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
---|
336 | apv[i] = NULL, acb[i] = 0;
|
---|
337 | cAllocations = 0;
|
---|
338 | i = 0;
|
---|
339 | cb = 0;
|
---|
340 | crfree = 0;
|
---|
341 | crmalloc = 0;
|
---|
342 | crealloc = 0;
|
---|
343 | while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
|
---|
344 | {
|
---|
345 | int j;
|
---|
346 | j = rand();
|
---|
347 | if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
|
---|
348 | (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 4 :
|
---|
349 | i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 5 :
|
---|
350 | i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 6 :
|
---|
351 | i < RANDOMTEST_ITERATIONS ? (j % 8) > 6 : 0
|
---|
352 | )
|
---|
353 | )
|
---|
354 | { /* malloc */
|
---|
355 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
|
---|
356 | (void)0;
|
---|
357 | if (j < NUMBER_OF_POINTERS)
|
---|
358 | {
|
---|
359 | do
|
---|
360 | {
|
---|
361 | acb[j] = rand();
|
---|
362 | } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
|
---|
363 | if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
|
---|
364 | acb[j] += 1024*256;
|
---|
365 | apv[j] = smalloc(acb[j]);
|
---|
366 | if (apv[j] == NULL)
|
---|
367 | {
|
---|
368 | printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
|
---|
369 | if (acb[j] > 10000)
|
---|
370 | continue;
|
---|
371 | break;
|
---|
372 | }
|
---|
373 | memset(apv[j], 0xA, MIN(acb[j],16));
|
---|
374 | cAllocations++;
|
---|
375 | cb += acb[j];
|
---|
376 | crmalloc++;
|
---|
377 | }
|
---|
378 | }
|
---|
379 | else
|
---|
380 | { /* free or realloc */
|
---|
381 | if (cAllocations == 0)
|
---|
382 | continue;
|
---|
383 |
|
---|
384 | if (cAllocations < NUMBER_OF_POINTERS/10)
|
---|
385 | {
|
---|
386 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
---|
387 | (void)0;
|
---|
388 | }
|
---|
389 | else
|
---|
390 | {
|
---|
391 | int k = 0;
|
---|
392 | do
|
---|
393 | {
|
---|
394 | j = rand();
|
---|
395 | } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
|
---|
396 | if (k >= NUMBER_OF_POINTERS/2)
|
---|
397 | {
|
---|
398 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
---|
399 | (void)0;
|
---|
400 | }
|
---|
401 | }
|
---|
402 |
|
---|
403 | if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
|
---|
404 | {
|
---|
405 | int cb = _swp_msize(apv[j]);
|
---|
406 | if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
|
---|
407 | printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
|
---|
408 | if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
|
---|
409 | { /* realloc */
|
---|
410 | int cb;
|
---|
411 | void *pv;
|
---|
412 | crealloc++;
|
---|
413 | do
|
---|
414 | {
|
---|
415 | cb = rand();
|
---|
416 | } while (cb == 0 || cb > 3072);
|
---|
417 | /*
|
---|
418 | if (i >= 0x1c14)
|
---|
419 | Int3();
|
---|
420 | */
|
---|
421 | pv = srealloc(apv[j], cb);
|
---|
422 | if (pv == NULL)
|
---|
423 | {
|
---|
424 | printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
|
---|
425 | continue;
|
---|
426 | }
|
---|
427 | apv[j] = pv;
|
---|
428 | acb[j] = cb;
|
---|
429 | }
|
---|
430 | else
|
---|
431 | { /* free */
|
---|
432 | sfree(apv[j]);
|
---|
433 | apv[j] = NULL;
|
---|
434 | cAllocations--;
|
---|
435 | crfree++;
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 | _swp_heap_check();
|
---|
440 | if (RANDOMTEST_ITERATIONS/2 == i)
|
---|
441 | _swp_dump_subheaps();
|
---|
442 | if ((i % 2048) == 0)
|
---|
443 | printf("i=%d cAllocations=%d\n", i, cAllocations);
|
---|
444 | }
|
---|
445 |
|
---|
446 | printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
|
---|
447 |
|
---|
448 | printf("_swp_dump_subheaps:\n");
|
---|
449 | _swp_dump_subheaps();
|
---|
450 |
|
---|
451 | printf("_swp_memfree - before heapmin: %d\n", _swp_memfree());
|
---|
452 | _swp_heapmin();
|
---|
453 | printf("_swp_memfree - after heapmin : %d\n", _swp_memfree());
|
---|
454 |
|
---|
455 | printf("_swp_dump_subheaps:\n");
|
---|
456 | _swp_dump_subheaps();
|
---|
457 | } /* fSwpRandom */
|
---|
458 | }
|
---|
459 |
|
---|
460 |
|
---|
461 |
|
---|
462 | /* unreferenced parameters */
|
---|
463 | argc = argc;
|
---|
464 | argv = argv;
|
---|
465 |
|
---|
466 | return 0;
|
---|
467 | }
|
---|
468 |
|
---|
469 |
|
---|
470 |
|
---|