Changeset 1406 for trunk/psLib/test/sysUtils/tst_psMemory.c
- Timestamp:
- Aug 6, 2004, 12:34:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/sysUtils/tst_psMemory.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/sysUtils/tst_psMemory.c
r1365 r1406 6 6 * @author Robert DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1.2 1$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-08-0 2 19:43:23$8 * @version $Revision: 1.22 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-08-06 22:34:06 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 80 80 } 81 81 }; 82 82 83 83 int main( int argc, char* argv[] ) 84 84 { 85 85 psLogSetLevel( PS_LOG_INFO ); 86 86 87 87 return ( ! runTestSuite( stderr, "psMemory", tests, argc, argv ) ); 88 88 } … … 94 94 const int size = 100; 95 95 int failed = 0; 96 96 97 97 psLogMsg( __func__, PS_LOG_INFO, "psAlloc shall allocate memory blocks writeable by caller.\n" ); 98 98 99 99 mem = ( int* ) psAlloc( size * sizeof( int ) ); 100 100 if ( mem == NULL ) { 101 psError( __FILE__, "psAlloc returned a NULL value in %s!", __func__ ); 102 return 1; 101 psError( __FILE__, "psAlloc returned a NULL value in %s!", __func__ ); 102 return 1; 103 } 104 105 for ( int index = 0;index < size;index++ ) { 106 mem[ index ] = index; 107 } 108 109 for ( int index = 0;index < size;index++ ) { 110 if ( mem[ index ] != index ) { 111 failed++; 103 112 } 104 105 for ( int index = 0;index < size;index++ ) { 106 mem[ index ] = index; 107 } 108 109 for ( int index = 0;index < size;index++ ) { 110 if ( mem[ index ] != index ) { 111 failed++; 112 } 113 } 114 113 } 114 115 115 psFree( mem ); 116 116 117 117 return failed; 118 118 } … … 123 123 int * mem; 124 124 int ref = 0; 125 125 126 126 psLogMsg( __func__, PS_LOG_INFO, "memory reference count shall be incrementable/decrementable" ); 127 127 128 128 mem = ( int* ) psAlloc( 100 * sizeof( int ) ); 129 129 130 130 ref = psMemGetRefCounter( mem ); 131 131 if ( ref != 1 ) { 132 psError( __func__, "Expected to buffer reference count to be initially 1, but it was %d.", ref );133 return 1;134 }135 132 psError( __func__, "Expected to buffer reference count to be initially 1, but it was %d.", ref ); 133 return 1; 134 } 135 136 136 psMemIncrRefCounter( mem ); 137 137 psMemIncrRefCounter( mem ); 138 138 psMemIncrRefCounter( mem ); 139 139 140 140 ref = psMemGetRefCounter( mem ); 141 141 if ( ref != 4 ) { 142 psError( __func__, "Expected to find buffer reference count to be 4, but it was %d.", ref );143 return 1;144 }145 142 psError( __func__, "Expected to find buffer reference count to be 4, but it was %d.", ref ); 143 return 1; 144 } 145 146 146 psMemDecrRefCounter( mem ); 147 147 psMemDecrRefCounter( mem ); 148 148 149 149 ref = psMemGetRefCounter( mem ); 150 150 if ( ref != 2 ) { 151 psError( __func__, "Expected to find buffer reference count to be 2, but it was %d.", ref );152 return 1;153 }154 151 psError( __func__, "Expected to find buffer reference count to be 2, but it was %d.", ref ); 152 return 1; 153 } 154 155 155 psLogMsg( __func__, PS_LOG_INFO, "psFree shall be just decrement a multiple refererenced pointer." ); 156 156 157 157 psFree( mem ); 158 158 159 159 ref = psMemGetRefCounter( mem ); 160 160 if ( ref != 1 ) { 161 psError( __func__, "Expected to find buffer reference count to be 1, but it was %d.", ref );162 return 1;163 }164 161 psError( __func__, "Expected to find buffer reference count to be 1, but it was %d.", ref ); 162 return 1; 163 } 164 165 165 psFree( mem ); 166 166 167 167 return 0; 168 168 } … … 174 174 int * mem[ 100 ]; 175 175 psMemExhaustedCallback cb; 176 177 for ( int lcv = 0; lcv < 100; lcv++ ) { 178 mem[ lcv ] = NULL;179 }180 176 177 for ( int lcv = 0; lcv < 100; lcv++ ) { 178 mem[ lcv ] = NULL; 179 } 180 181 181 psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psRealloc shall call " 182 182 "the psMemExhaustedCallback.\n" ); 183 183 184 184 exhaustedCallbackCalled = 0; 185 185 186 186 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback ); 187 188 for ( int lcv = 0; lcv < 100; lcv++ ) { 189 mem[ lcv ] = ( int* ) psAlloc( 10 );190 }191 192 for ( int lcv = 0; lcv < 100; lcv++ ) { 193 mem[ lcv ] = ( int* ) psRealloc( mem[ lcv ], SIZE_MAX - 1000 );194 }195 187 188 for ( int lcv = 0; lcv < 100; lcv++ ) { 189 mem[ lcv ] = ( int* ) psAlloc( 10 ); 190 } 191 192 for ( int lcv = 0; lcv < 100; lcv++ ) { 193 mem[ lcv ] = ( int* ) psRealloc( mem[ lcv ], SIZE_MAX - 1000 ); 194 } 195 196 196 psMemExhaustedCallbackSet( cb ); 197 197 198 198 if ( exhaustedCallbackCalled == 0 ) { 199 psError( __FILE__, "Called psRealloc with HUGE memory requirement and survived in %s!", __func__ );200 return 1;201 }202 203 for ( int lcv = 0; lcv < 100; lcv++ ) { 204 psFree( mem[ lcv ] );205 }206 199 psError( __FILE__, "Called psRealloc with HUGE memory requirement and survived in %s!", __func__ ); 200 return 1; 201 } 202 203 for ( int lcv = 0; lcv < 100; lcv++ ) { 204 psFree( mem[ lcv ] ); 205 } 206 207 207 return 0; 208 208 } … … 213 213 int * mem[ 100 ]; 214 214 psMemExhaustedCallback cb; 215 216 for ( int lcv = 0; lcv < 100; lcv++ ) { 217 mem[ lcv ] = NULL;218 }219 215 216 for ( int lcv = 0; lcv < 100; lcv++ ) { 217 mem[ lcv ] = NULL; 218 } 219 220 220 psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psalloc shall call " 221 221 "the psMemExhaustedCallback.\n" ); 222 222 223 223 exhaustedCallbackCalled = 0; 224 224 225 225 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback ); 226 227 for ( int lcv = 0; lcv < 100; lcv++ ) { 228 mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX - 1000 );229 }230 226 227 for ( int lcv = 0; lcv < 100; lcv++ ) { 228 mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX - 1000 ); 229 } 230 231 231 psMemExhaustedCallbackSet( cb ); 232 232 233 233 if ( exhaustedCallbackCalled == 0 ) { 234 psError( __FILE__, "Called psAlloc with HUGE memory requirement and survived in %s!", __func__ );235 return 1;236 }237 238 for ( int lcv = 0; lcv < 100; lcv++ ) { 239 psFree( mem[ lcv ] );240 }241 234 psError( __FILE__, "Called psAlloc with HUGE memory requirement and survived in %s!", __func__ ); 235 return 1; 236 } 237 238 for ( int lcv = 0; lcv < 100; lcv++ ) { 239 psFree( mem[ lcv ] ); 240 } 241 242 242 return 0; 243 243 } … … 250 250 int* mem3; 251 251 const int initialSize = 100; 252 252 253 253 psLogMsg( __func__, PS_LOG_INFO, "psRealloc shall increase/decrease memory buffer while " 254 254 "preserving contents" ); 255 255 256 256 // allocate buffer with known values. 257 257 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); … … 259 259 mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 260 260 for ( int lcv = 0;lcv < initialSize;lcv++ ) { 261 mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv;262 }263 261 mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv; 262 } 263 264 264 psMemCheckCorruption( 1 ); 265 265 psLogMsg( __func__, PS_LOG_INFO, "Expanding memory buffer." ); 266 266 267 267 // realloc to 2x 268 268 mem1 = ( int* ) psRealloc( mem1, 2 * initialSize * sizeof( int ) ); 269 269 mem2 = ( int* ) psRealloc( mem2, 2 * initialSize * sizeof( int ) ); 270 270 mem3 = ( int* ) psRealloc( mem3, 2 * initialSize * sizeof( int ) ); 271 271 272 272 // check values of initial block 273 273 for ( int i = 0;i < initialSize;i++ ) { 274 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 275 psError( __FILE__, "Realloc didn't preserve the contents with expanding buffer in %s.", 276 __func__ ); 277 break; 278 } 274 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 275 psError( __FILE__, "Realloc didn't preserve the contents with expanding buffer in %s.", 276 __func__ ); 277 break; 279 278 } 280 279 } 280 281 281 psMemCheckCorruption( 1 ); 282 282 psLogMsg( __func__, PS_LOG_INFO, "Shrinking memory buffer." ); 283 283 284 284 // realloc to 1/2 initial value. 285 285 mem1 = ( int* ) psRealloc( mem1, ( initialSize / 2 ) * sizeof( int ) ); 286 286 mem2 = ( int* ) psRealloc( mem2, ( initialSize / 2 ) * sizeof( int ) ); 287 287 mem3 = ( int* ) psRealloc( mem3, ( initialSize / 2 ) * sizeof( int ) ); 288 288 289 289 // check values of initial block 290 290 for ( int i = 0;i < initialSize / 2;i++ ) { 291 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 292 psError( __FILE__, "Realloc didn't preserve the contents with shrinking buffer in %s.", 293 __func__ ); 294 break; 295 } 291 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 292 psError( __FILE__, "Realloc didn't preserve the contents with shrinking buffer in %s.", 293 __func__ ); 294 break; 296 295 } 297 296 } 297 298 298 psFree( mem1 ); 299 299 psFree( mem2 ); 300 300 psFree( mem3 ); 301 301 302 302 return 0; 303 303 } … … 311 311 const int initialSize = 100; 312 312 int mark; 313 313 314 314 allocCallbackCalled = 0; 315 315 freeCallbackCalled = 0; 316 316 psMemAllocateCallbackSet( memAllocateCallback ); 317 317 psMemFreeCallbackSet( memFreeCallback ); 318 318 319 319 psMemAllocateCallbackSetID( currentId + 1 ); 320 320 psMemFreeCallbackSetID( currentId + 1 ); 321 321 322 322 psLogMsg( __func__, PS_LOG_INFO, "call to psAlloc/psRealloc shall generate a callback if specified " 323 323 "memory ID is allocated." ); 324 324 325 325 // allocate buffer with known values. 326 326 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 327 327 mem2 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 328 328 mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 329 329 330 330 psFree( mem1 ); 331 331 psFree( mem2 ); 332 332 psFree( mem3 ); 333 333 334 334 if ( allocCallbackCalled != 2 || freeCallbackCalled != 2 ) { 335 psError( __FILE__, "alloc/free callbacks were not called the proper number of times in %s",336 __func__ );337 return 1;338 }339 335 psError( __FILE__, "alloc/free callbacks were not called the proper number of times in %s", 336 __func__ ); 337 return 1; 338 } 339 340 340 allocCallbackCalled = 0; 341 341 freeCallbackCalled = 0; 342 342 343 343 mark = psMemGetId(); 344 344 345 345 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 346 346 347 347 psMemAllocateCallbackSetID( mark ); 348 348 349 349 mem1 = ( int* ) psRealloc( mem1, initialSize * 2 * sizeof( int ) ); 350 350 351 351 psFree( mem1 ); 352 352 353 353 if ( allocCallbackCalled != 2 ) { 354 psError( __FILE__, "realloc callbacks were not called the proper number of times in %s",355 __func__ );356 return 1;357 }358 359 return 0; 360 354 psError( __FILE__, "realloc callbacks were not called the proper number of times in %s", 355 __func__ ); 356 return 1; 357 } 358 359 return 0; 360 361 361 } 362 362 … … 370 370 int nLeaks = 0; 371 371 int lineMark = 0; 372 372 373 373 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckLeaks shall return the number of blocks above an ID " 374 374 "that are still allocated" ); 375 375 376 376 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 377 lineMark = __LINE__ + 1;378 buffers[ lcv ] = psAlloc( sizeof( int ) );379 }380 377 lineMark = __LINE__ + 1; 378 buffers[ lcv ] = psAlloc( sizeof( int ) ); 379 } 380 381 381 for ( lcv = 1;lcv < numBuffers;lcv++ ) { 382 psFree( buffers[ lcv ] ); 383 } 384 385 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one instance." ); 386 387 nLeaks = psMemCheckLeaks( currentId, &blks, stderr ); 388 389 if ( nLeaks != 1 ) { 390 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ ); 391 return 1; 392 } 393 394 if ( blks[ 0 ] ->lineno != lineMark ) { 395 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.", 396 lineMark, blks[ 0 ] ->lineno, __func__ ); 397 return 1; 398 } 399 400 psFree( buffers[ 0 ] ); 401 psFree( blks ); 402 403 psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with a different leak location" ); 404 psMemCheckLeaks( currentId, NULL, stderr ); 405 406 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 407 lineMark = __LINE__ + 1; 408 buffers[ lcv ] = psAlloc( sizeof( int ) ); 409 } 410 411 for ( lcv = 0;lcv < numBuffers - 1;lcv++ ) { 412 psFree( buffers[ lcv ] ); 413 } 414 415 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one error." ); 416 417 nLeaks = psMemCheckLeaks( currentId, &blks, stderr ); 418 419 if ( nLeaks != 1 ) { 420 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ ); 421 return 1; 422 } 423 424 if ( blks[ 0 ] ->lineno != lineMark ) { 425 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ ); 426 return 1; 427 } 428 429 psFree( buffers[ 4 ] ); 430 psFree( blks ); 431 432 psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with multiple leak locations." ); 433 434 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 435 lineMark = __LINE__ + 1; 436 buffers[ lcv ] = psAlloc( sizeof( int ) ); 437 } 438 439 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 440 if ( lcv % 2 == 0 ) { 382 441 psFree( buffers[ lcv ] ); 383 442 } 384 385 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one instance." ); 386 443 } 444 445 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce two errors." ); 446 387 447 nLeaks = psMemCheckLeaks( currentId, &blks, stderr ); 388 389 if ( nLeaks != 1) {390 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ );391 return 1;392 }393 448 449 if ( nLeaks != 2 ) { 450 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ ); 451 return 1; 452 } 453 394 454 if ( blks[ 0 ] ->lineno != lineMark ) { 395 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.", 396 lineMark, blks[ 0 ] ->lineno, __func__ ); 397 return 1; 398 } 399 400 psFree( buffers[ 0 ] ); 401 psFree( blks ); 402 403 psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with a different leak location" ); 404 psMemCheckLeaks( currentId, NULL, stderr ); 405 406 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 407 lineMark = __LINE__ + 1; 408 buffers[ lcv ] = psAlloc( sizeof( int ) ); 409 } 410 411 for ( lcv = 0;lcv < numBuffers - 1;lcv++ ) { 412 psFree( buffers[ lcv ] ); 413 } 414 415 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce one error." ); 416 417 nLeaks = psMemCheckLeaks( currentId, &blks, stderr ); 418 419 if ( nLeaks != 1 ) { 420 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ ); 421 return 1; 422 } 423 424 if ( blks[ 0 ] ->lineno != lineMark ) { 425 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ ); 426 return 1; 427 } 428 429 psFree( buffers[ 4 ] ); 430 psFree( blks ); 431 432 psLogMsg( __func__, PS_LOG_INFO, "Testing psMemCheckLeaks again with multiple leak locations." ); 433 434 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 435 lineMark = __LINE__ + 1; 436 buffers[ lcv ] = psAlloc( sizeof( int ) ); 437 } 438 439 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 440 if ( lcv % 2 == 0 ) { 441 psFree( buffers[ lcv ] ); 442 } 443 } 444 445 psLogMsg( __func__, PS_LOG_INFO, "following psMemCheckLeaks call should produce two errors." ); 446 447 nLeaks = psMemCheckLeaks( currentId, &blks, stderr ); 448 449 if ( nLeaks != 2 ) { 450 psError( __FILE__, "psMemCheckLeaks should have found 1 leak, but found %d in %s.", nLeaks, __func__ ); 451 return 1; 452 } 453 454 if ( blks[ 0 ] ->lineno != lineMark ) { 455 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ ); 456 return 1; 457 } 458 455 psError( __FILE__, "psMemCheckLeaks found a leak other than the expected one in %s.", __func__ ); 456 return 1; 457 } 458 459 459 psFree( blks ); 460 460 psFree( buffers[ 1 ] ); 461 461 psFree( buffers[ 3 ] ); 462 462 463 463 return 0; 464 464 } … … 470 470 int corruptions = 0; 471 471 psMemProblemCallback cb; 472 472 473 473 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption shall detect memory corruptions" ); 474 474 475 475 buffer = psAlloc( sizeof( int ) ); 476 476 477 477 // cause memory corruption via buffer underflow 478 478 *buffer = 1; … … 480 480 oldValue = *buffer; 481 481 *buffer = 2; 482 482 483 483 problemCallbackCalled = 0; 484 484 cb = psMemProblemCallbackSet( memProblemCallback ); 485 485 486 486 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption should output an error message and " 487 487 "memProblemCallback callback should be called." ); 488 488 489 489 corruptions = psMemCheckCorruption( 0 ); 490 490 491 491 // restore the memory problem callback 492 492 psMemProblemCallbackSet( cb ); 493 493 494 494 // restore the value, 'uncorrupting' the buffer 495 495 *buffer = oldValue; 496 496 buffer++; 497 497 498 498 psFree( buffer ); 499 499 500 500 if ( corruptions != 1 ) { 501 psError( __FILE__, "Expected one memory corruption but found %d in %s.",502 corruptions, __func__ );503 return 1;504 }505 501 psError( __FILE__, "Expected one memory corruption but found %d in %s.", 502 corruptions, __func__ ); 503 return 1; 504 } 505 506 506 if ( problemCallbackCalled != 1 ) { 507 psError( __FILE__, "The memProblemCallback was not invoked but should have been in %s",508 __func__ );509 return 1;510 }511 512 return 0; 513 507 psError( __FILE__, "The memProblemCallback was not invoked but should have been in %s", 508 __func__ ); 509 return 1; 510 } 511 512 return 0; 513 514 514 } 515 515 … … 547 547 548 548 void * buffer = psAlloc( 1024 ); 549 549 550 550 psFree( buffer ); 551 551 552 552 psLogMsg( __func__, PS_LOG_INFO, "Next should be an error about multiple freeing." ); 553 553 psFree( buffer ); 554 555 return 0; 556 } 554 555 return 0; 556 }
Note:
See TracChangeset
for help on using the changeset viewer.
