Changeset 2204 for trunk/psLib/test/sysUtils/tst_psMemory.c
- Timestamp:
- Oct 26, 2004, 2:57:34 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/sysUtils/tst_psMemory.c (modified) (27 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/sysUtils/tst_psMemory.c
r1811 r2204 6 6 * @author Robert DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1.2 5$ $Name: not supported by cvs2svn $9 * @date $Date: 2004- 09-14 23:55:47$8 * @version $Revision: 1.26 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-10-27 00:57:33 $ 10 10 * 11 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 26 26 #include "pslib.h" 27 27 28 static intTPFreeReferencedMemory( void );29 static intTPOutOfMemory( void );30 static intTPReallocOutOfMemory( void );31 static void*TPOutOfMemoryExhaustedCallback( size_t size );32 static intTPCheckBufferPositive( void );33 static intTPrealloc( void );34 static intTPallocCallback( void );28 static psS32 TPFreeReferencedMemory( void ); 29 static psS32 TPOutOfMemory( void ); 30 static psS32 TPReallocOutOfMemory( void ); 31 static psPtr TPOutOfMemoryExhaustedCallback( size_t size ); 32 static psS32 TPCheckBufferPositive( void ); 33 static psS32 TPrealloc( void ); 34 static psS32 TPallocCallback( void ); 35 35 static psMemoryId memAllocateCallback( const psMemBlock *ptr ); 36 36 static psMemoryId memFreeCallback( const psMemBlock *ptr ); 37 static intTPcheckLeaks( void );38 static intTPmemCorruption( void );39 static intTPmultipleFree( void );40 void memProblemCallback( const psMemBlock *ptr, const char *file, intlineno );41 42 static intproblemCallbackCalled = 0;43 static intallocCallbackCalled = 0;44 static intfreeCallbackCalled = 0;45 static intexhaustedCallbackCalled = 0;37 static psS32 TPcheckLeaks( void ); 38 static psS32 TPmemCorruption( void ); 39 static psS32 TPmultipleFree( void ); 40 void memProblemCallback( const psMemBlock *ptr, const char *file, psS32 lineno ); 41 42 static psS32 problemCallbackCalled = 0; 43 static psS32 allocCallbackCalled = 0; 44 static psS32 freeCallbackCalled = 0; 45 static psS32 exhaustedCallbackCalled = 0; 46 46 47 47 testDescription tests[] = { … … 81 81 }; 82 82 83 int main( intargc, char* argv[] )83 psS32 main( psS32 argc, char* argv[] ) 84 84 { 85 85 psLogSetLevel( PS_LOG_INFO ); … … 89 89 90 90 // Testpoint #449, psAlloc shall allocate memory blocks writeable by caller. 91 intTPCheckBufferPositive( void )92 { 93 int* mem;94 const intsize = 100;95 intfailed = 0;91 psS32 TPCheckBufferPositive( void ) 92 { 93 psS32 * mem; 94 const psS32 size = 100; 95 psS32 failed = 0; 96 96 97 97 psLogMsg( __func__, PS_LOG_INFO, "psAlloc shall allocate memory blocks writeable by caller.\n" ); 98 98 99 mem = ( int* ) psAlloc( size * sizeof( int) );99 mem = ( psS32* ) psAlloc( size * sizeof( psS32 ) ); 100 100 if ( mem == NULL ) { 101 101 psError( __FILE__, "psAlloc returned a NULL value in %s!", __func__ ); … … 103 103 } 104 104 105 for ( intindex = 0;index < size;index++ ) {105 for ( psS32 index = 0;index < size;index++ ) { 106 106 mem[ index ] = index; 107 107 } 108 108 109 for ( intindex = 0;index < size;index++ ) {109 for ( psS32 index = 0;index < size;index++ ) { 110 110 if ( mem[ index ] != index ) { 111 111 failed++; … … 118 118 } 119 119 120 intTPFreeReferencedMemory( void )120 psS32 TPFreeReferencedMemory( void ) 121 121 { 122 122 // create memory 123 int* mem;124 intref = 0;123 psS32 * mem; 124 psS32 ref = 0; 125 125 126 126 psLogMsg( __func__, PS_LOG_INFO, "memory reference count shall be incrementable/decrementable" ); 127 127 128 mem = ( int* ) psAlloc( 100 * sizeof( int) );128 mem = ( psS32* ) psAlloc( 100 * sizeof( psS32 ) ); 129 129 130 130 ref = psMemGetRefCounter( mem ); … … 170 170 // Bug/Task #562 regression test. Upon requesting more memory than is available, psRealloc shall call 171 171 // the psMemExhaustedCallback. 172 intTPReallocOutOfMemory( void )173 { 174 int* mem[ 100 ];172 psS32 TPReallocOutOfMemory( void ) 173 { 174 psS32 * mem[ 100 ]; 175 175 psMemExhaustedCallback cb; 176 176 177 for ( intlcv = 0; lcv < 100; lcv++ ) {177 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 178 178 mem[ lcv ] = NULL; 179 179 } … … 186 186 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback ); 187 187 188 for ( intlcv = 0; lcv < 100; lcv++ ) {189 mem[ lcv ] = ( int* ) psAlloc( 10 );190 } 191 192 for ( intlcv = 0; lcv < 100; lcv++ ) {193 mem[ lcv ] = ( int* ) psRealloc( mem[ lcv ], SIZE_MAX/2 - 1000 );188 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 189 mem[ lcv ] = ( psS32* ) psAlloc( 10 ); 190 } 191 192 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 193 mem[ lcv ] = ( psS32* ) psRealloc( mem[ lcv ], SIZE_MAX/2 - 1000 ); 194 194 } 195 195 … … 201 201 } 202 202 203 for ( intlcv = 0; lcv < 100; lcv++ ) {203 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 204 204 psFree( mem[ lcv ] ); 205 205 } … … 209 209 // Testpoint #450, Upon requesting more memory than is available, psalloc shall call 210 210 // the psMemExhaustedCallback. 211 intTPOutOfMemory( void )212 { 213 int* mem[ 100 ];211 psS32 TPOutOfMemory( void ) 212 { 213 psS32 * mem[ 100 ]; 214 214 psMemExhaustedCallback cb; 215 215 216 for ( intlcv = 0; lcv < 100; lcv++ ) {216 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 217 217 mem[ lcv ] = NULL; 218 218 } … … 225 225 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback ); 226 226 227 for ( intlcv = 0; lcv < 100; lcv++ ) {228 mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX/2 - 1000 );227 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 228 mem[ lcv ] = ( psS32* ) psAlloc( SIZE_MAX/2 - 1000 ); 229 229 } 230 230 … … 236 236 } 237 237 238 for ( intlcv = 0; lcv < 100; lcv++ ) {238 for ( psS32 lcv = 0; lcv < 100; lcv++ ) { 239 239 psFree( mem[ lcv ] ); 240 240 } … … 244 244 245 245 // Testpoint #451, psRealloc shall increase/decrease memory buffer while preserving contents 246 intTPrealloc( void )247 { 248 int* mem1;249 int* mem2;250 int* mem3;251 const intinitialSize = 100;246 psS32 TPrealloc( void ) 247 { 248 psS32 * mem1; 249 psS32* mem2; 250 psS32* mem3; 251 const psS32 initialSize = 100; 252 252 253 253 psLogMsg( __func__, PS_LOG_INFO, "psRealloc shall increase/decrease memory buffer while " … … 255 255 256 256 // allocate buffer with known values. 257 mem1 = ( int* ) psAlloc( initialSize * sizeof( int) );258 mem2 = ( int* ) psAlloc( initialSize * sizeof( int) );259 mem3 = ( int* ) psAlloc( initialSize * sizeof( int) );260 for ( intlcv = 0;lcv < initialSize;lcv++ ) {257 mem1 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 258 mem2 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 259 mem3 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 260 for ( psS32 lcv = 0;lcv < initialSize;lcv++ ) { 261 261 mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv; 262 262 } … … 266 266 267 267 // realloc to 2x 268 mem1 = ( int* ) psRealloc( mem1, 2 * initialSize * sizeof( int) );269 mem2 = ( int* ) psRealloc( mem2, 2 * initialSize * sizeof( int) );270 mem3 = ( int* ) psRealloc( mem3, 2 * initialSize * sizeof( int) );268 mem1 = ( psS32* ) psRealloc( mem1, 2 * initialSize * sizeof( psS32 ) ); 269 mem2 = ( psS32* ) psRealloc( mem2, 2 * initialSize * sizeof( psS32 ) ); 270 mem3 = ( psS32* ) psRealloc( mem3, 2 * initialSize * sizeof( psS32 ) ); 271 271 272 272 // check values of initial block 273 for ( inti = 0;i < initialSize;i++ ) {273 for ( psS32 i = 0;i < initialSize;i++ ) { 274 274 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 275 275 psError( __FILE__, "Realloc didn't preserve the contents with expanding buffer in %s.", … … 283 283 284 284 // realloc to 1/2 initial value. 285 mem1 = ( int* ) psRealloc( mem1, ( initialSize / 2 ) * sizeof( int) );286 mem2 = ( int* ) psRealloc( mem2, ( initialSize / 2 ) * sizeof( int) );287 mem3 = ( int* ) psRealloc( mem3, ( initialSize / 2 ) * sizeof( int) );285 mem1 = ( psS32* ) psRealloc( mem1, ( initialSize / 2 ) * sizeof( psS32 ) ); 286 mem2 = ( psS32* ) psRealloc( mem2, ( initialSize / 2 ) * sizeof( psS32 ) ); 287 mem3 = ( psS32* ) psRealloc( mem3, ( initialSize / 2 ) * sizeof( psS32 ) ); 288 288 289 289 // check values of initial block 290 for ( inti = 0;i < initialSize / 2;i++ ) {290 for ( psS32 i = 0;i < initialSize / 2;i++ ) { 291 291 if ( mem1[ i ] != i || mem2[ i ] != i || mem3[ i ] != i ) { 292 292 psError( __FILE__, "Realloc didn't preserve the contents with shrinking buffer in %s.", … … 303 303 } 304 304 305 intTPallocCallback( void )306 { 307 int* mem1;308 int* mem2;309 int* mem3;310 intcurrentId = psMemGetId();311 const intinitialSize = 100;312 intmark;305 psS32 TPallocCallback( void ) 306 { 307 psS32 * mem1; 308 psS32* mem2; 309 psS32* mem3; 310 psS32 currentId = psMemGetId(); 311 const psS32 initialSize = 100; 312 psS32 mark; 313 313 314 314 allocCallbackCalled = 0; … … 324 324 325 325 // allocate buffer with known values. 326 mem1 = ( int* ) psAlloc( initialSize * sizeof( int) );327 mem2 = ( int* ) psAlloc( initialSize * sizeof( int) );328 mem3 = ( int* ) psAlloc( initialSize * sizeof( int) );326 mem1 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 327 mem2 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 328 mem3 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 329 329 330 330 psFree( mem1 ); … … 343 343 mark = psMemGetId(); 344 344 345 mem1 = ( int* ) psAlloc( initialSize * sizeof( int) );345 mem1 = ( psS32* ) psAlloc( initialSize * sizeof( psS32 ) ); 346 346 347 347 psMemAllocateCallbackSetID( mark ); 348 348 349 mem1 = ( int* ) psRealloc( mem1, initialSize * 2 * sizeof( int) );349 mem1 = ( psS32* ) psRealloc( mem1, initialSize * 2 * sizeof( psS32 ) ); 350 350 351 351 psFree( mem1 ); … … 361 361 } 362 362 363 intTPcheckLeaks( void )364 { 365 const intnumBuffers = 5;366 int* buffers[ 5 ];367 intlcv;368 intcurrentId = psMemGetId();363 psS32 TPcheckLeaks( void ) 364 { 365 const psS32 numBuffers = 5; 366 psS32* buffers[ 5 ]; 367 psS32 lcv; 368 psS32 currentId = psMemGetId(); 369 369 psMemBlock** blks; 370 intnLeaks = 0;371 intlineMark = 0;370 psS32 nLeaks = 0; 371 psS32 lineMark = 0; 372 372 373 373 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckLeaks shall return the number of blocks above an ID " … … 376 376 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 377 377 lineMark = __LINE__ + 1; 378 buffers[ lcv ] = psAlloc( sizeof( int) );378 buffers[ lcv ] = psAlloc( sizeof( psS32 ) ); 379 379 } 380 380 … … 406 406 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 407 407 lineMark = __LINE__ + 1; 408 buffers[ lcv ] = psAlloc( sizeof( int) );408 buffers[ lcv ] = psAlloc( sizeof( psS32 ) ); 409 409 } 410 410 … … 434 434 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 435 435 lineMark = __LINE__ + 1; 436 buffers[ lcv ] = psAlloc( sizeof( int) );436 buffers[ lcv ] = psAlloc( sizeof( psS32 ) ); 437 437 } 438 438 … … 464 464 } 465 465 466 intTPmemCorruption( void )467 { 468 int* buffer = NULL;469 intoldValue = 0;470 intcorruptions = 0;466 psS32 TPmemCorruption( void ) 467 { 468 psS32 * buffer = NULL; 469 psS32 oldValue = 0; 470 psS32 corruptions = 0; 471 471 psMemProblemCallback cb; 472 472 473 473 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption shall detect memory corruptions" ); 474 474 475 buffer = psAlloc( sizeof( int) );475 buffer = psAlloc( sizeof( psS32 ) ); 476 476 477 477 // cause memory corruption via buffer underflow … … 514 514 } 515 515 516 void memProblemCallback( const psMemBlock *ptr, const char *file, intlineno )516 void memProblemCallback( const psMemBlock *ptr, const char *file, psS32 lineno ) 517 517 { 518 518 psLogMsg( __func__, PS_LOG_INFO, "memory callback called for id %d (%s:%d).", … … 536 536 } 537 537 538 void*TPOutOfMemoryExhaustedCallback( size_t size )538 psPtr TPOutOfMemoryExhaustedCallback( size_t size ) 539 539 { 540 540 psLogMsg( __func__, PS_LOG_INFO, "Custom MemExhaustedCallback was invoked." ); … … 543 543 } 544 544 545 intTPmultipleFree( void )546 { 547 548 void *buffer = psAlloc( 1024 );545 psS32 TPmultipleFree( void ) 546 { 547 548 psPtr buffer = psAlloc( 1024 ); 549 549 550 550 psFree( buffer );
Note:
See TracChangeset
for help on using the changeset viewer.
