| 1 | /* $Id: heaptest.c,v 1.2 2000-01-23 03:20:52 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*2
|
|---|
| 17 | #define Int3() __interrupt(3)
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 | /*******************************************************************************
|
|---|
| 21 | * Internal Functions
|
|---|
| 22 | *******************************************************************************/
|
|---|
| 23 | #include "malloc.h"
|
|---|
| 24 | #include "rmalloc.h"
|
|---|
| 25 | #include "macros.h"
|
|---|
| 26 | #include <stdio.h>
|
|---|
| 27 | #include <stdlib.h>
|
|---|
| 28 | #include <memory.h>
|
|---|
| 29 | #include <builtin.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 |
|
|---|
| 45 | /*
|
|---|
| 46 | * Initiate the heap.
|
|---|
| 47 | */
|
|---|
| 48 | if (ResHeapInit(128*1024, 1024*1024*64) != 0)
|
|---|
| 49 | {
|
|---|
| 50 | printf("Failed to initiate the resident heap.\n");
|
|---|
| 51 | return 1;
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | /*
|
|---|
| 55 | *
|
|---|
| 56 | * resident heap tests
|
|---|
| 57 | * resident heap tests
|
|---|
| 58 | *
|
|---|
| 59 | */
|
|---|
| 60 | #if 1
|
|---|
| 61 | /*
|
|---|
| 62 | * Simple allocation test.
|
|---|
| 63 | */
|
|---|
| 64 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
|---|
| 65 | {
|
|---|
| 66 | do
|
|---|
| 67 | {
|
|---|
| 68 | acb[i] = rand();
|
|---|
| 69 | } while(acb[i] == 0 || acb[i] > 127);
|
|---|
| 70 |
|
|---|
| 71 | if ((i % (NUMBER_OF_POINTERS/3)) == 1)
|
|---|
| 72 | acb[i] += 1024*260;
|
|---|
| 73 | apv[i] = rmalloc(acb[i]);
|
|---|
| 74 | if (apv[i] == NULL)
|
|---|
| 75 | {
|
|---|
| 76 | printf("rmalloc failed: acb[%d]=%d\n", i, acb[i]);
|
|---|
| 77 | if (acb[i] > 1000)
|
|---|
| 78 | break;
|
|---|
| 79 | }
|
|---|
| 80 | memset(apv[i], 0xA, MIN(acb[i],16));
|
|---|
| 81 | cb += acb[i];
|
|---|
| 82 | }
|
|---|
| 83 |
|
|---|
| 84 | printf("Finished allocating memory: %d allocations %d bytes\n", i, cb);
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | printf("_res_dump_subheaps:\n");
|
|---|
| 88 | _res_dump_subheaps();
|
|---|
| 89 |
|
|---|
| 90 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
|---|
| 91 | {
|
|---|
| 92 | int cb = _res_msize(apv[i]);
|
|---|
| 93 | if (cb != ((acb[i] + 3) & ~3) && (cb < ((acb[i] + 3) & ~3) || cb > 52 + ((acb[i] + 3) & ~3)) )
|
|---|
| 94 | printf("size of avp[%d] (%d) != acb[%] (%d)\n", i, cb, i, acb[i]);
|
|---|
| 95 | rfree(apv[i]);
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 |
|
|---|
| 99 | /*
|
|---|
| 100 | * test _res_heapmin
|
|---|
| 101 | */
|
|---|
| 102 | printf("_res_memfree - before heapmin: %d\n", _res_memfree());
|
|---|
| 103 | _res_heapmin();
|
|---|
| 104 | printf("_res_memfree - after heapmin : %d\n", _res_memfree());
|
|---|
| 105 |
|
|---|
| 106 | /*
|
|---|
| 107 | * Test _res_dump_subheaps
|
|---|
| 108 | */
|
|---|
| 109 | printf("\n_res_dump_subheaps:\n");
|
|---|
| 110 | _res_dump_subheaps();
|
|---|
| 111 | #endif
|
|---|
| 112 |
|
|---|
| 113 |
|
|---|
| 114 | /*
|
|---|
| 115 | * Test 2 - random allocation and freeing of memory.
|
|---|
| 116 | */
|
|---|
| 117 | printf("\n"
|
|---|
| 118 | "Random allocation and freeing test:\n");
|
|---|
| 119 | for (i = 0; i < NUMBER_OF_POINTERS; i++)
|
|---|
| 120 | apv[i] = NULL, acb[i] = 0;
|
|---|
| 121 | cAllocations = 0;
|
|---|
| 122 | i = 0;
|
|---|
| 123 | cb = 0;
|
|---|
| 124 | crfree = 0;
|
|---|
| 125 | crmalloc = 0;
|
|---|
| 126 | crealloc = 0;
|
|---|
| 127 | while (i++ < RANDOMTEST_ITERATIONS || cAllocations > 0)
|
|---|
| 128 | {
|
|---|
| 129 | int j;
|
|---|
| 130 | j = rand();
|
|---|
| 131 | if (cAllocations + (NUMBER_OF_POINTERS/20) < NUMBER_OF_POINTERS &&
|
|---|
| 132 | (i < RANDOMTEST_ITERATIONS*1/4 ? (j % 8) > 2 :
|
|---|
| 133 | i < RANDOMTEST_ITERATIONS*2/4 ? (j % 8) > 3 :
|
|---|
| 134 | i < RANDOMTEST_ITERATIONS*3/4 ? (j % 8) > 4 :
|
|---|
| 135 | i < RANDOMTEST_ITERATIONS ? (j % 8) > 5 : 0
|
|---|
| 136 | )
|
|---|
| 137 | )
|
|---|
| 138 | { /* malloc */
|
|---|
| 139 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] != NULL; j++)
|
|---|
| 140 | (void)0;
|
|---|
| 141 | if (j < NUMBER_OF_POINTERS)
|
|---|
| 142 | {
|
|---|
| 143 | do
|
|---|
| 144 | {
|
|---|
| 145 | acb[j] = rand();
|
|---|
| 146 | } while (acb[j] == 0 || (acb[j] > 2048 && (i % 11) != 10));
|
|---|
| 147 | if ((i % (RANDOMTEST_ITERATIONS/20)) == 1)
|
|---|
| 148 | acb[j] += 1024*256;
|
|---|
| 149 | apv[j] = rmalloc(acb[j]);
|
|---|
| 150 | if (apv[j] == NULL)
|
|---|
| 151 | {
|
|---|
| 152 | printf("rmalloc failed, acb[%d] = %d\n", j, acb[j]);
|
|---|
| 153 | if (acb[j] > 10000)
|
|---|
| 154 | continue;
|
|---|
| 155 | break;
|
|---|
| 156 | }
|
|---|
| 157 | memset(apv[j], 0xA, MIN(acb[j],16));
|
|---|
| 158 | cAllocations++;
|
|---|
| 159 | cb += acb[j];
|
|---|
| 160 | crmalloc++;
|
|---|
| 161 | }
|
|---|
| 162 | }
|
|---|
| 163 | else
|
|---|
| 164 | { /* free or realloc */
|
|---|
| 165 | if (cAllocations == 0)
|
|---|
| 166 | continue;
|
|---|
| 167 |
|
|---|
| 168 | if (cAllocations < NUMBER_OF_POINTERS/10)
|
|---|
| 169 | {
|
|---|
| 170 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
|---|
| 171 | (void)0;
|
|---|
| 172 | }
|
|---|
| 173 | else
|
|---|
| 174 | {
|
|---|
| 175 | int k = 0;
|
|---|
| 176 | do
|
|---|
| 177 | {
|
|---|
| 178 | j = rand();
|
|---|
| 179 | } while (k++ < NUMBER_OF_POINTERS/2 && (j >= NUMBER_OF_POINTERS || apv[j] == NULL));
|
|---|
| 180 | if (k >= NUMBER_OF_POINTERS/2)
|
|---|
| 181 | {
|
|---|
| 182 | for (j = 0; j < NUMBER_OF_POINTERS && apv[j] == NULL; j++)
|
|---|
| 183 | (void)0;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 |
|
|---|
| 187 | if (j < NUMBER_OF_POINTERS && apv[j] != NULL)
|
|---|
| 188 | {
|
|---|
| 189 | int cb = _res_msize(apv[j]);
|
|---|
| 190 | if (cb != ((acb[j] + 3) & ~3) && (cb < ((acb[j] + 3) & ~3) || cb > 52 + ((acb[j] + 3) & ~3)) )
|
|---|
| 191 | printf("size of avp[%d] (%d) != acb[%d] (%d)\n", j, cb, j, acb[j]);
|
|---|
| 192 | if (i < RANDOMTEST_ITERATIONS*3/4 && j % 3 == 0)
|
|---|
| 193 | { /* realloc */
|
|---|
| 194 | int cb;
|
|---|
| 195 | void *pv;
|
|---|
| 196 | crealloc++;
|
|---|
| 197 | do
|
|---|
| 198 | {
|
|---|
| 199 | cb = rand();
|
|---|
| 200 | } while (cb == 0 || cb > 3072);
|
|---|
| 201 | /*
|
|---|
| 202 | if (i >= 0x1c14)
|
|---|
| 203 | Int3();
|
|---|
| 204 | */
|
|---|
| 205 | pv = rrealloc(apv[j], cb);
|
|---|
| 206 | if (pv == NULL)
|
|---|
| 207 | {
|
|---|
| 208 | printf("realloc(apv[%d](0x%08), %d) failed\n", j, apv[j], cb);
|
|---|
| 209 | continue;
|
|---|
| 210 | }
|
|---|
| 211 | apv[j] = pv;
|
|---|
| 212 | acb[j] = cb;
|
|---|
| 213 | }
|
|---|
| 214 | else
|
|---|
| 215 | { /* free */
|
|---|
| 216 | rfree(apv[j]);
|
|---|
| 217 | apv[j] = NULL;
|
|---|
| 218 | cAllocations--;
|
|---|
| 219 | crfree++;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 | /*_res_heap_check();*/
|
|---|
| 224 | if (RANDOMTEST_ITERATIONS/2 == i)
|
|---|
| 225 | _res_dump_subheaps();
|
|---|
| 226 | if ((i % 2048) == 0)
|
|---|
| 227 | printf("i=%d cAllocations=%d\n", i, cAllocations);
|
|---|
| 228 | }
|
|---|
| 229 |
|
|---|
| 230 | printf("cb=%d crfree=%d crmalloc=%d crealloc=%d\n", cb, crfree, crmalloc, crealloc);
|
|---|
| 231 |
|
|---|
| 232 | printf("_res_dump_subheaps:\n");
|
|---|
| 233 | _res_dump_subheaps();
|
|---|
| 234 |
|
|---|
| 235 | printf("_res_memfree - before heapmin: %d\n", _res_memfree());
|
|---|
| 236 | _res_heapmin();
|
|---|
| 237 | printf("_res_memfree - after heapmin : %d\n", _res_memfree());
|
|---|
| 238 |
|
|---|
| 239 | printf("_res_dump_subheaps:\n");
|
|---|
| 240 | _res_dump_subheaps();
|
|---|
| 241 |
|
|---|
| 242 |
|
|---|
| 243 |
|
|---|
| 244 | /*
|
|---|
| 245 | *
|
|---|
| 246 | * swappable heap tests
|
|---|
| 247 | * swappable heap tests
|
|---|
| 248 | *
|
|---|
| 249 | */
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 | /* unreferenced parameters */
|
|---|
| 255 | argc = argc;
|
|---|
| 256 | argv = argv;
|
|---|
| 257 |
|
|---|
| 258 | return 0;
|
|---|
| 259 | }
|
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
|
|---|