1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
#include "omtTest.h"
#if CHECK_LEVEL > 0
#define OM_CHECK CHECK_LEVEL
#endif
#include "omalloc.h"
omMemCell_t cells[MAX_CELLS];
int errors = 0;
int missed_errors = 0;
int used_regions = 0;
int seed;
#if defined (__hpux) || defined (__alpha) || defined (__svr4__) || defined (__SVR4)
/* SF1 cosimo.medicis.polytechnique.fr V4.0 1229 alpha works */
#if defined (__hpux) || defined (__svr4__) || defined (__SVR4)
/* HPUX lacks random(). DEC OSF/1 1.2 random() returns a double. */
long mrand48 ();
void srand48();
static long
random ()
{
return mrand48 ();
}
static void srandom(long seed)
{
srand48(seed);
}
#endif
#endif
#if CHECK_LEVEL > 0
void omtTestDebug(omMemCell cell)
{
size_t size = GET_SIZE(cell->spec);
size_t is_size;
if (om_ErrorStatus != omError_NoError) return;
if (cell->bin != NULL)
{
if (IS_ALIGNED(cell->spec))
omDebugAddrAlignedBin(cell->addr, cell->bin);
else
omDebugAddrBin(cell->addr, cell->bin);
}
else
{
if (IS_ALIGNED(cell->spec))
omDebugAddrAlignedSize(cell->addr, size);
else
omDebugAddrSize(cell->addr, size);
}
if (om_ErrorStatus != omError_NoError) return;
if (!OM_IS_ALIGNED(cell->addr))
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"addr:%p is unaligned", cell->addr);
return;
}
if (IS_ALIGNED(cell->spec) && !OM_IS_STRICT_ALIGNED(cell->addr))
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"addr:%p is not strict unaligned", cell->addr);
return;
}
is_size = omSizeOfAddr(cell->addr);
if (!OM_IS_ALIGNED(is_size))
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"is_size == %u is unaligned", is_size);
return;
}
if (is_size < size)
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"is_size==%u < size==%u", is_size, size);
return;
}
if (is_size >> LOG_SIZEOF_LONG != omSizeWOfAddr(cell->addr))
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"is_sizeW==%u < sizeW==%u", is_size >> LOG_SIZEOF_LONG, omSizeWOfAddr(cell->addr));
return;
}
TestAddrContent(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), is_size);
}
void TestAddrContentEqual(void* s1, void* s2, size_t size)
{
int i;
size_t sizeW = OM_ALIGN_SIZE(size) >> LOG_SIZEOF_LONG;
for (i=0; i<sizeW; i++)
{
if (((unsigned long*)s1)[i] != ((unsigned long*)s2)[i])
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"s1[%u]==%d != s2[%u]==%d", i, ((unsigned long*)s1)[i], i, ((unsigned long*)s2)[i]);
return;
}
}
}
void TestAddrContent(void* addr, unsigned long value, size_t size)
{
size_t sizeW = OM_ALIGN_SIZE(size) >> LOG_SIZEOF_LONG;
int i;
if (!OM_IS_ALIGNED(addr))
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"addr %p unaligned", addr);
return;
}
for (i=0; i<sizeW; i++)
{
if (((unsigned long*)addr)[i] != value)
{
omReportError(omError_Unknown, omError_NoError, OM_FLR,
"word %d modified: is %u should be %u", i, ((unsigned long*)addr)[i], value);
return;
}
}
}
#endif
void InitCellAddrContent(omMemCell cell)
{
size_t sizeW = omSizeWOfAddr(cell->addr);
omMemsetW(cell->addr, (IS_ZERO(cell->spec) ? 0 : cell->spec), sizeW);
}
void omCheckCells(int n, int level, omMemCell_t* cells)
{
#if END_CHECK_LEVEL > 0
int l = om_Opts.MinCheck;
int i;
omTestMemory(level);
om_Opts.MinCheck = 1;
for (i=0; i<n; i++)
{
omtTestDebug(&cells[i]);
if (om_ErrorStatus != omError_NoError)
{
errors++;
om_ErrorStatus = omError_NoError;
}
if ((i % 10000) == 0)
{
printf(".");
fflush(stdout);
}
}
om_Opts.MinCheck = l;
#endif
}
int size_range = RANGE_MIN;
int size_range_number = RANGE_MAX / RANGE_MIN;
int MyRandSpec()
{
unsigned long spec = random() + 1;
if (! size_range_number)
{
size_range = size_range << 1;
if (size_range > RANGE_MAX) size_range = RANGE_MIN;
size_range_number = RANGE_MAX / size_range;
}
SET_SIZE(spec, GET_SIZE(spec) & (size_range -1));
size_range_number--;
if (GET_SIZE(spec) == 0) spec++;
return spec;
}
void TestAlloc(omMemCell cell, unsigned long spec)
{
if (DO_CHECK(spec))
{
if (DO_TRACK(spec))
om_Opts.MinTrack = GET_TRACK(spec);
else
om_Opts.MinTrack = 0;
if (DO_KEEP(spec))
omtTestAllocKeep(cell, spec);
else
omtTestAllocDebug(cell, spec);
}
else
omtTestAlloc(cell, spec);
if (om_ErrorStatus != omError_NoError)
{
errors++;
om_ErrorStatus = omError_NoError;
}
}
void TestRealloc(omMemCell cell, unsigned long spec)
{
if (DO_CHECK(spec))
{
if (DO_TRACK(spec))
om_Opts.MinTrack = GET_TRACK(spec);
else
om_Opts.MinTrack = 0;
if (DO_KEEP(spec))
omtTestReallocKeep(cell, spec);
else
omtTestReallocDebug(cell, spec);
}
else
omtTestRealloc(cell, spec);
if (om_ErrorStatus != omError_NoError)
{
errors++;
om_ErrorStatus = omError_NoError;
}
}
void TestDup(omMemCell cell, unsigned long spec)
{
if (DO_CHECK(spec))
{
if (DO_TRACK(spec))
om_Opts.MinTrack = GET_TRACK(spec);
else
om_Opts.MinTrack = 0;
if (DO_KEEP(spec))
omtTestDupKeep(cell, spec);
else
omtTestDupDebug(cell, spec);
}
else
omtTestDup(cell, spec);
if (om_ErrorStatus != omError_NoError)
{
errors++;
om_ErrorStatus = omError_NoError;
}
}
void TestFree(omMemCell cell)
{
if (cell->addr != NULL)
{
if (DO_FREE_CHECK(cell->spec))
{
if (DO_KEEP(cell->spec))
omtTestFreeKeep(cell);
else
omtTestFreeDebug(cell);
}
else
{
omtTestFree(cell);
}
if (om_ErrorStatus != omError_NoError)
{
errors++;
om_ErrorStatus = omError_NoError;
}
}
}
omBin omtGetStickyBin(omBin bin)
{
omBin sticky_bin = omFindInGList(om_StickyBins, next, max_blocks, bin->max_blocks);
if (sticky_bin == NULL)
sticky_bin = omGetStickyBinOfBin(bin);
return sticky_bin;
}
void omtMergeStickyBins(omMemCell cell, int n)
{
int i;
omBin bin;
for (i=0; i<n; i++)
{
if (cell[i].orig_bin != NULL)
{
if (omIsOnGList(om_StickyBins, next, cell[i].bin))
omMergeStickyBinIntoBin(cell[i].bin, cell[i].orig_bin);
cell[i].bin = cell[i].orig_bin;
cell[i].orig_bin = NULL;
}
}
bin = om_StickyBins;
while (bin != NULL)
{
if (bin->current_page == om_ZeroPage)
{
omBin next_bin = bin->next;
om_StickyBins = omRemoveFromGList(om_StickyBins, next, bin);
__omFreeBinAddr(bin);
bin = next_bin;
}
else
{
bin = bin->next;
}
}
}
void my_exit()
{
printf("\nomtTest Summary: ");
if (errors || missed_errors || used_regions)
{
printf("***FAILED***errors:%d, missed_errors:%d, used_regions:%d, seed=%d\n", errors, missed_errors, used_regions, seed);
if (errors) exit(errors);
if (missed_errors) exit(missed_errors);
if (used_regions) exit(used_regions);
}
else
{
printf("OK\n");
exit(0);
}
}
int main(int argc, char* argv[])
{
int i=0, error_test = 1;
unsigned long spec, j;
int n = 1;
int n_cells = MAX_CELLS;
int decr = 2;
int last_kept_freed = 0;
om_Opts.MinCheck = CHECK_LEVEL;
om_Opts.Keep = KEEP_ADDR;
seed = time(NULL);
omInitRet_2_Info(argv[0]);
omInitGetBackTrace();
omInitInfo();
om_Opts.PagesPerRegion = PAGES_PER_REGION;
if (argc > 1) sscanf(argv[1], "%d", &error_test);
if (argc > 2) sscanf(argv[2], "%d", &seed);
srandom(seed);
if (argc > 3) sscanf(argv[3], "%d", &n);
if (argc > 4) sscanf(argv[4], "%d", &decr);
if (decr < 2) decr = 2;
printf("seed == %d\n", seed);
fflush(stdout);
while (1)
{
if (i == n_cells)
{
i = 0;
printf("\nCells: %d KeptAddr:%d AlwaysKeptAddr:%d\n", n_cells,
#ifndef OM_NDEBUG
omListLength(om_KeptAddr), omListLength(om_AlwaysKeptAddrs)
#else
0, 0
#endif
);
printf("Checking Memory and all cells ");
fflush(stdout);
omCheckCells(n_cells, END_CHECK_LEVEL, cells);
printf("\n");
omPrintStats(stdout);
omPrintInfo(stdout);
if (om_Info.CurrentRegionsAlloc > 0) omPrintBinStats(stdout);
fflush(stdout);
#if CHECK_LEVEL > 0 && TRACK_LEVEL > 0
if (error_test && errors == 0)
{
missed_errors = omtTestErrors();
if (missed_errors < 0)
{
my_exit();
}
}
#endif
omtMergeStickyBins(cells, n_cells);
while (i< n_cells)
{
TestFree(&cells[i]);
i++;
}
omFreeKeptAddr();
omtMergeStickyBins(cells, -1);
omPrintStats(stdout);
omPrintInfo(stdout);
if (om_Info.CurrentRegionsAlloc > 0)
{
omPrintBinStats(stdout);
used_regions += om_Info.CurrentRegionsAlloc;
}
omPrintUsedAddrs(stdout, 5);
i=0;
n--;
if (n <= 0 || n_cells <= 100)
{
my_exit();
}
else
{
n_cells = n_cells / decr;
}
}
spec = MyRandSpec();
myprintf("%d:%lu:%ld:%ld", i, spec, GET_SIZE(spec), GET_TRACK(spec));
myfflush(stdout);
if (DO_FREE(spec))
{
if (i != 0)
{
myprintf(" FREE");
j = spec % i;
myprintf(" %ld ", j);
myfflush(stdout);
TestFree(&cells[j]);
TestAlloc(&cells[j], spec);
}
}
else if (DO_REALLOC(spec))
{
if (i != 0)
{
myprintf(" REALLOC");
j = spec % i;
myprintf(" %ld ", j);
myfflush(stdout);
TestRealloc(&cells[j], spec);
}
}
else if (DO_DUP(spec))
{
if (i != 0)
{
myprintf(" DUP");
j = spec % i;
myprintf(" %ld ", j);
myfflush(stdout);
TestDup(&cells[j], spec);
}
}
else
{
myprintf(" ALLOC");
myfflush(stdout);
TestAlloc(&cells[i], spec);
i++;
if (i % 1000 == 0)
{
printf("%d:", i / 1000);
fflush(stdout);
}
}
myprintf("\n");
myfflush(stdout);
// free kept addresses from time to time
if ((i % 10000) == 0 && i != n_cells && i!=last_kept_freed)
{
printf("F:");
omFreeKeptAddr();
last_kept_freed = i;
}
#if 0
if (CHECK_LEVEL > 2)
{
for (j=0; j<i; j++)
{
omtTestDebug(&cells[j]);
}
}
#endif
}
return 0;
}
|