Changeset 640 for trunk/psLib/test/sysUtils/tst_psMemory.c
- Timestamp:
- May 11, 2004, 10:52:33 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/sysUtils/tst_psMemory.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/sysUtils/tst_psMemory.c
r637 r640 6 6 * @author Robert DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-05-11 03:14:49$8 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-05-11 20:52:33 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 34 34 static long memAllocateCallback(const psMemBlock *ptr); 35 35 static long memFreeCallback(const psMemBlock *ptr); 36 int TPcheckLeaks(void); 37 int TPmemCorruption(void); 38 void memProblemCallback(const psMemBlock *ptr, const char *file, int lineno); 39 40 static int problemCallbackCalled = 0; 36 41 static int allocCallbackCalled = 0; 37 42 static int freeCallbackCalled = 0; … … 43 48 {TPOutOfMemory,"450-TPOutOfMemory",0}, 44 49 {TPrealloc,"451-TPrealloc",0}, 45 {TPallocCallback,"452-TPallocCallback",0}, 46 {TPFreeReferencedMemory,"TPFreeReferencedMemory",6}, 50 {TPallocCallback,"452/453-TPallocCallback",0}, 51 {TPcheckLeaks,"454-TPcheckLeaks",0}, 52 {TPmemCorruption,"455-TPmemCorruption",0}, 53 {TPFreeReferencedMemory,"456-TPFreeReferencedMemory",0}, 47 54 {NULL} 48 55 }; … … 95 102 int* mem; 96 103 int currentId = psMemGetId(); 97 98 fprintf(stderr,"Allocating buffer.\n"); 104 int ref = 0; 105 psMemProblemCallback cb; 106 107 psLogMsg(__func__,PS_LOG_INFO,"memory reference count shall be incrementable/decrementable"); 108 99 109 mem = (int*) psAlloc(100*sizeof(int)); 100 110 111 ref = psMemGetRefCounter(mem); 112 if (ref != 1) { 113 psError(__func__,"Expected to buffer reference count to be initially 1, but it was %d.",ref); 114 return 1; 115 } 116 101 117 psMemIncrRefCounter(mem); 102 fprintf(stderr,"Freeing buffer after incrementing reference count.\n"); 118 psMemIncrRefCounter(mem); 119 psMemIncrRefCounter(mem); 120 121 ref = psMemGetRefCounter(mem); 122 if (ref != 4) { 123 psError(__func__,"Expected to find buffer reference count to be 4, but it was %d.",ref); 124 return 1; 125 } 126 127 psMemDecrRefCounter(mem); 128 psMemDecrRefCounter(mem); 129 130 ref = psMemGetRefCounter(mem); 131 if (ref != 2) { 132 psError(__func__,"Expected to find buffer reference count to be 2, but it was %d.",ref); 133 return 1; 134 } 135 136 psLogMsg(__func__,PS_LOG_INFO,"expect error from free as multiple referenced buffer is freed."); 137 138 problemCallbackCalled = 0; 139 cb = psMemProblemCallbackSet(memProblemCallback); 140 141 psFree(mem); 142 143 psMemProblemCallbackSet(cb); 144 145 if (problemCallbackCalled != 1) { 146 psError(__func__,"Expected memProblemCallback to be called once, but was called %d times.", 147 problemCallbackCalled); 148 return 1; 149 } 150 103 151 psFree(mem); 104 152 … … 267 315 return 1; 268 316 } 269 return 0; 270 317 318 psMemCheckLeaks(currentId,NULL,NULL); 319 psMemCheckCorruption(1); 320 321 return 0; 322 323 } 324 325 int TPcheckLeaks(void) 326 { 327 const int numBuffers = 5; 328 int* buffers[5]; 329 int lcv; 330 int currentId = psMemGetId(); 331 psMemBlock** blks; 332 int nLeaks = 0; 333 int lineMark = 0; 334 335 psLogMsg(__func__,PS_LOG_INFO,"psMemCheckLeaks shall return the number of blocks above an ID " 336 "that are still allocated"); 337 338 for (lcv=0;lcv<numBuffers;lcv++) { 339 lineMark = __LINE__+1; 340 buffers[lcv] = psAlloc(sizeof(int)); 341 } 342 343 for (lcv=1;lcv<numBuffers;lcv++) { 344 psFree(buffers[lcv]); 345 } 346 347 psLogMsg(__func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce one instance."); 348 349 nLeaks = psMemCheckLeaks(currentId, &blks, stderr); 350 351 if (nLeaks != 1) { 352 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__); 353 return 1; 354 } 355 356 if (blks[0]->lineno != lineMark) { 357 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.", 358 lineMark, blks[0]->lineno, __func__); 359 return 1; 360 } 361 362 psFree(buffers[0]); 363 psFree(blks); 364 365 psLogMsg(__func__,PS_LOG_INFO,"Testing psMemCheckLeaks again with a different leak location"); 366 psMemCheckLeaks(currentId,NULL,stderr); 367 368 for (lcv=0;lcv<numBuffers;lcv++) { 369 lineMark = __LINE__+1; 370 buffers[lcv] = psAlloc(sizeof(int)); 371 } 372 373 for (lcv=0;lcv<numBuffers-1;lcv++) { 374 psFree(buffers[lcv]); 375 } 376 377 psLogMsg(__func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce one error."); 378 379 nLeaks = psMemCheckLeaks(currentId, &blks, stderr); 380 381 if (nLeaks != 1) { 382 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__); 383 return 1; 384 } 385 386 if (blks[0]->lineno != lineMark) { 387 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one in %s.",__func__); 388 return 1; 389 } 390 391 psFree(buffers[4]); 392 psFree(blks); 393 394 psLogMsg(__func__,PS_LOG_INFO,"Testing psMemCheckLeaks again with multiple leak locations."); 395 396 for (lcv=0;lcv<numBuffers;lcv++) { 397 lineMark = __LINE__+1; 398 buffers[lcv] = psAlloc(sizeof(int)); 399 } 400 401 for (lcv=0;lcv<numBuffers;lcv++) { 402 if (lcv%2 == 0) { 403 psFree(buffers[lcv]); 404 } 405 } 406 407 psLogMsg(__func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce two errors."); 408 409 nLeaks = psMemCheckLeaks(currentId, &blks, stderr); 410 411 if (nLeaks != 2) { 412 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__); 413 return 1; 414 } 415 416 if (blks[0]->lineno != lineMark) { 417 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one in %s.",__func__); 418 return 1; 419 } 420 421 psFree(blks); 422 psFree(buffers[1]); 423 psFree(buffers[3]); 424 425 psMemCheckLeaks(currentId,NULL,NULL); 426 psMemCheckCorruption(1); 427 428 return 0; 429 } 430 431 int TPmemCorruption(void) 432 { 433 int* buffer = NULL; 434 int oldValue = 0; 435 int corruptions = 0; 436 psMemProblemCallback cb; 437 438 psLogMsg(__func__,PS_LOG_INFO,"psMemCheckCorruption shall detect memory corruptions"); 439 440 buffer = psAlloc(sizeof(int)); 441 442 // cause memory corruption via buffer underflow 443 *buffer = 1; 444 buffer--; 445 oldValue = *buffer; 446 *buffer = 2; 447 448 problemCallbackCalled = 0; 449 cb = psMemProblemCallbackSet(memProblemCallback); 450 451 psLogMsg(__func__,PS_LOG_INFO,"psMemCheckCorruption should output an error message and " 452 "memProblemCallback callback should be called."); 453 454 corruptions = psMemCheckCorruption(0); 455 456 // restore the memory problem callback 457 psMemProblemCallbackSet(cb); 458 459 // restore the value, 'uncorrupting' the buffer 460 *buffer = oldValue; 461 buffer++; 462 463 psFree(buffer); 464 465 if (corruptions != 1) { 466 psError(__FILE__,"Expected one memory corruption but found %d in %s.", 467 corruptions, __func__); 468 return 1; 469 } 470 471 if (problemCallbackCalled != 1) { 472 psError(__FILE__,"The memProblemCallback was not invoked but should have been in %s", 473 __func__); 474 return 1; 475 } 476 477 return 0; 478 479 } 480 481 void memProblemCallback(const psMemBlock *ptr, const char *file, int lineno) 482 { 483 psLogMsg(__func__,PS_LOG_INFO,"memory callback called for id %d (%s:%d).", 484 ptr->id, file, lineno); 485 problemCallbackCalled++; 486 return; 271 487 } 272 488
Note:
See TracChangeset
for help on using the changeset viewer.
