Changeset 1365 for trunk/psLib/test/sysUtils/tst_psMemory.c
- Timestamp:
- Aug 2, 2004, 9:43:23 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/test/sysUtils/tst_psMemory.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/test/sysUtils/tst_psMemory.c
r1242 r1365 1 1 /** @file tst_psMemory.c 2 *3 * @brief Contains the tests for psMemory.[ch]4 *5 *6 * @author Robert DeSonia, MHPCC7 *8 * @version $Revision: 1.20$ $Name: not supported by cvs2svn $9 * @date $Date: 2004-07-19 22:38:36$10 *11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii12 */2 * 3 * @brief Contains the tests for psMemory.[ch] 4 * 5 * 6 * @author Robert DeSonia, MHPCC 7 * 8 * @version $Revision: 1.21 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2004-08-02 19:43:23 $ 10 * 11 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 12 */ 13 13 14 14 // need to allow malloc for callback use … … 26 26 #include "pslib.h" 27 27 28 static int TPFreeReferencedMemory( void);29 static int TPOutOfMemory( void);30 static int TPReallocOutOfMemory( void);31 static void* TPOutOfMemoryExhaustedCallback( size_t size);32 static int TPCheckBufferPositive( void);33 static int TPrealloc( void);34 static int TPallocCallback( void);35 static psMemoryId memAllocateCallback( const psMemBlock *ptr);36 static psMemoryId memFreeCallback( const psMemBlock *ptr);37 static int TPcheckLeaks( void);38 static int TPmemCorruption( void);39 static int TPmultipleFree( void);40 void memProblemCallback( const psMemBlock *ptr, const char *file, int lineno);28 static int TPFreeReferencedMemory( void ); 29 static int TPOutOfMemory( void ); 30 static int TPReallocOutOfMemory( void ); 31 static void* TPOutOfMemoryExhaustedCallback( size_t size ); 32 static int TPCheckBufferPositive( void ); 33 static int TPrealloc( void ); 34 static int TPallocCallback( void ); 35 static psMemoryId memAllocateCallback( const psMemBlock *ptr ); 36 static psMemoryId memFreeCallback( const psMemBlock *ptr ); 37 static int TPcheckLeaks( void ); 38 static int TPmemCorruption( void ); 39 static int TPmultipleFree( void ); 40 void memProblemCallback( const psMemBlock *ptr, const char *file, int lineno ); 41 41 42 42 static int problemCallbackCalled = 0; … … 46 46 47 47 testDescription tests[] = { 48 {TPCheckBufferPositive,449,"checkBufferPositive",0,false}, 49 {TPOutOfMemory,450,"outOfMemory",-6,false}, 50 {TPReallocOutOfMemory,562,"reallocOutOfMemory",-6,false}, 51 {TPrealloc,451,"psRealloc",0,false}, 52 {TPallocCallback,452,"allocCallback",0,false}, 53 {TPallocCallback,453,"allocCallback2",0,true}, 54 {TPcheckLeaks,454,"checkLeaks",0,false}, 55 {TPmemCorruption,455,"psMemCorruption",0,false}, 56 {TPFreeReferencedMemory,456,"freeReferencedMemory",0,false}, 57 {TPmultipleFree,699,"multipleFree",-6,false}, 58 {NULL} 48 { 49 TPCheckBufferPositive, 449, "checkBufferPositive", 0, false 50 }, 51 { 52 TPOutOfMemory, 450, "outOfMemory", -6, false 53 }, 54 { 55 TPReallocOutOfMemory, 562, "reallocOutOfMemory", -6, false 56 }, 57 { 58 TPrealloc, 451, "psRealloc", 0, false 59 }, 60 { 61 TPallocCallback, 452, "allocCallback", 0, false 62 }, 63 { 64 TPallocCallback, 453, "allocCallback2", 0, true 65 }, 66 { 67 TPcheckLeaks, 454, "checkLeaks", 0, false 68 }, 69 { 70 TPmemCorruption, 455, "psMemCorruption", 0, false 71 }, 72 { 73 TPFreeReferencedMemory, 456, "freeReferencedMemory", 0, false 74 }, 75 { 76 TPmultipleFree, 699, "multipleFree", -6, false 77 }, 78 { 79 NULL 80 } 59 81 }; 60 61 int main(int argc, char* argv[]) 62 { 63 psLogSetLevel(PS_LOG_INFO); 64 65 if (! runTestSuite(stderr,"psMemory",tests,argc,argv) ) { 66 psError(__FILE__,"One or more tests failed"); 67 return 1; 68 } 69 return 0; 82 83 int main( int argc, char* argv[] ) 84 { 85 psLogSetLevel( PS_LOG_INFO ); 86 87 return ( ! runTestSuite( stderr, "psMemory", tests, argc, argv ) ); 70 88 } 71 89 72 90 // Testpoint #449, psAlloc shall allocate memory blocks writeable by caller. 73 int TPCheckBufferPositive( void)74 { 75 int * mem;91 int TPCheckBufferPositive( void ) 92 { 93 int * mem; 76 94 const int size = 100; 77 95 int failed = 0; 78 79 psLogMsg( __func__,PS_LOG_INFO,"psAlloc shall allocate memory blocks writeable by caller.\n");80 81 mem = ( int*) psAlloc(size*sizeof(int));82 if ( mem == NULL) {83 psError(__FILE__,"psAlloc returned a NULL value in %s!",__func__);84 return 1;85 }86 87 for ( int index=0;index<size;index++) {88 mem[index]=index;89 }90 91 for ( int index=0;index<size;index++) {92 if (mem[index] != index) {93 failed++;94 }95 }96 97 psFree( mem);98 96 97 psLogMsg( __func__, PS_LOG_INFO, "psAlloc shall allocate memory blocks writeable by caller.\n" ); 98 99 mem = ( int* ) psAlloc( size * sizeof( int ) ); 100 if ( mem == NULL ) { 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++; 112 } 113 } 114 115 psFree( mem ); 116 99 117 return failed; 100 118 } 101 119 102 int TPFreeReferencedMemory( void)120 int TPFreeReferencedMemory( void ) 103 121 { 104 122 // create memory 105 int * mem;123 int * mem; 106 124 int ref = 0; 107 108 psLogMsg( __func__,PS_LOG_INFO,"memory reference count shall be incrementable/decrementable");109 110 mem = ( int*) psAlloc(100*sizeof(int));111 112 ref = psMemGetRefCounter( mem);113 if ( ref != 1) {114 psError(__func__,"Expected to buffer reference count to be initially 1, but it was %d.",ref);115 return 1;116 }117 118 psMemIncrRefCounter( mem);119 psMemIncrRefCounter( mem);120 psMemIncrRefCounter( mem);121 122 ref = psMemGetRefCounter( mem);123 if ( ref != 4) {124 psError(__func__,"Expected to find buffer reference count to be 4, but it was %d.",ref);125 return 1;126 }127 128 psMemDecrRefCounter( mem);129 psMemDecrRefCounter( mem);130 131 ref = psMemGetRefCounter( mem);132 if ( ref != 2) {133 psError(__func__,"Expected to find buffer reference count to be 2, but it was %d.",ref);134 return 1;135 }136 137 psLogMsg( __func__,PS_LOG_INFO,"psFree shall be just decrement a multiple refererenced pointer.");138 139 psFree( mem);140 141 ref = psMemGetRefCounter( mem);142 if ( ref != 1) {143 psError(__func__,"Expected to find buffer reference count to be 1, but it was %d.",ref);144 return 1;145 }146 147 psFree( mem);148 125 126 psLogMsg( __func__, PS_LOG_INFO, "memory reference count shall be incrementable/decrementable" ); 127 128 mem = ( int* ) psAlloc( 100 * sizeof( int ) ); 129 130 ref = psMemGetRefCounter( mem ); 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 136 psMemIncrRefCounter( mem ); 137 psMemIncrRefCounter( mem ); 138 psMemIncrRefCounter( mem ); 139 140 ref = psMemGetRefCounter( mem ); 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 146 psMemDecrRefCounter( mem ); 147 psMemDecrRefCounter( mem ); 148 149 ref = psMemGetRefCounter( mem ); 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 155 psLogMsg( __func__, PS_LOG_INFO, "psFree shall be just decrement a multiple refererenced pointer." ); 156 157 psFree( mem ); 158 159 ref = psMemGetRefCounter( mem ); 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 165 psFree( mem ); 166 149 167 return 0; 150 168 } … … 152 170 // Bug/Task #562 regression test. Upon requesting more memory than is available, psRealloc shall call 153 171 // the psMemExhaustedCallback. 154 int TPReallocOutOfMemory( void)155 { 156 int * mem[100];172 int TPReallocOutOfMemory( void ) 173 { 174 int * mem[ 100 ]; 157 175 psMemExhaustedCallback cb; 158 159 for ( int lcv = 0; lcv<100; lcv++) {160 mem[lcv] = NULL;161 }162 163 psLogMsg( __func__,PS_LOG_INFO,"Upon requesting more memory than is available, psRealloc shall call "164 "the psMemExhaustedCallback.\n");165 176 177 for ( int lcv = 0; lcv < 100; lcv++ ) { 178 mem[ lcv ] = NULL; 179 } 180 181 psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psRealloc shall call " 182 "the psMemExhaustedCallback.\n" ); 183 166 184 exhaustedCallbackCalled = 0; 167 168 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback);169 170 for ( int lcv = 0; lcv<100; lcv++) {171 mem[lcv] = (int*) psAlloc(10);172 }173 174 for ( int lcv = 0; lcv<100; lcv++) {175 mem[lcv] = (int*) psRealloc(mem[lcv],SIZE_MAX-1000);176 }177 178 psMemExhaustedCallbackSet( cb);179 180 if ( exhaustedCallbackCalled == 0) {181 psError(__FILE__,"Called psRealloc with HUGE memory requirement and survived in %s!",__func__);182 return 1;183 }184 185 for ( int lcv = 0; lcv<100; lcv++) {186 psFree(mem[lcv]);187 }188 185 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 196 psMemExhaustedCallbackSet( cb ); 197 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 189 207 return 0; 190 208 } 191 209 // Testpoint #450, Upon requesting more memory than is available, psalloc shall call 192 210 // the psMemExhaustedCallback. 193 int TPOutOfMemory( void)194 { 195 int * mem[100];211 int TPOutOfMemory( void ) 212 { 213 int * mem[ 100 ]; 196 214 psMemExhaustedCallback cb; 197 198 for ( int lcv = 0; lcv<100; lcv++) {199 mem[lcv] = NULL;200 }201 202 psLogMsg( __func__,PS_LOG_INFO,"Upon requesting more memory than is available, psalloc shall call "203 "the psMemExhaustedCallback.\n");204 215 216 for ( int lcv = 0; lcv < 100; lcv++ ) { 217 mem[ lcv ] = NULL; 218 } 219 220 psLogMsg( __func__, PS_LOG_INFO, "Upon requesting more memory than is available, psalloc shall call " 221 "the psMemExhaustedCallback.\n" ); 222 205 223 exhaustedCallbackCalled = 0; 206 207 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback);208 209 for ( int lcv = 0; lcv<100; lcv++) {210 mem[lcv] = (int*) psAlloc(SIZE_MAX-1000);211 }212 213 psMemExhaustedCallbackSet( cb);214 215 if ( exhaustedCallbackCalled == 0) {216 psError(__FILE__,"Called psAlloc with HUGE memory requirement and survived in %s!",__func__);217 return 1;218 }219 220 for ( int lcv = 0; lcv<100; lcv++) {221 psFree(mem[lcv]);222 }223 224 225 cb = psMemExhaustedCallbackSet( TPOutOfMemoryExhaustedCallback ); 226 227 for ( int lcv = 0; lcv < 100; lcv++ ) { 228 mem[ lcv ] = ( int* ) psAlloc( SIZE_MAX - 1000 ); 229 } 230 231 psMemExhaustedCallbackSet( cb ); 232 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 224 242 return 0; 225 243 } 226 244 227 245 // Testpoint #451, psRealloc shall increase/decrease memory buffer while preserving contents 228 int TPrealloc( void)229 { 230 int * mem1;246 int TPrealloc( void ) 247 { 248 int * mem1; 231 249 int* mem2; 232 250 int* mem3; 233 251 const int initialSize = 100; 234 235 psLogMsg( __func__,PS_LOG_INFO,"psRealloc shall increase/decrease memory buffer while "236 "preserving contents");237 252 253 psLogMsg( __func__, PS_LOG_INFO, "psRealloc shall increase/decrease memory buffer while " 254 "preserving contents" ); 255 238 256 // allocate buffer with known values. 239 mem1 = ( int*) psAlloc(initialSize*sizeof(int));240 mem2 = ( int*) psAlloc(initialSize*sizeof(int));241 mem3 = ( int*) psAlloc(initialSize*sizeof(int));242 for ( int lcv=0;lcv<initialSize;lcv++) {243 mem1[lcv] = mem2[lcv] = mem3[lcv] = lcv;244 }245 246 psMemCheckCorruption( 1);247 psLogMsg( __func__,PS_LOG_INFO,"Expanding memory buffer.");248 257 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 258 mem2 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 259 mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 260 for ( int lcv = 0;lcv < initialSize;lcv++ ) { 261 mem1[ lcv ] = mem2[ lcv ] = mem3[ lcv ] = lcv; 262 } 263 264 psMemCheckCorruption( 1 ); 265 psLogMsg( __func__, PS_LOG_INFO, "Expanding memory buffer." ); 266 249 267 // realloc to 2x 250 mem1 = ( int*) psRealloc(mem1, 2*initialSize*sizeof(int));251 mem2 = ( int*) psRealloc(mem2, 2*initialSize*sizeof(int));252 mem3 = ( int*) psRealloc(mem3, 2*initialSize*sizeof(int));253 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 ) ); 271 254 272 // check values of initial block 255 for ( int i=0;i<initialSize;i++) {256 if (mem1[i] != i || mem2[i] != i || mem3[i] != i) {257 psError(__FILE__,"Realloc didn't preserve the contents with expanding buffer in %s.",258 __func__);259 break;260 }261 }262 263 psMemCheckCorruption( 1);264 psLogMsg( __func__,PS_LOG_INFO,"Shrinking memory buffer.");265 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 } 279 } 280 281 psMemCheckCorruption( 1 ); 282 psLogMsg( __func__, PS_LOG_INFO, "Shrinking memory buffer." ); 283 266 284 // realloc to 1/2 initial value. 267 mem1 = ( int*) psRealloc(mem1, (initialSize/2)*sizeof(int));268 mem2 = ( int*) psRealloc(mem2, (initialSize/2)*sizeof(int));269 mem3 = ( int*) psRealloc(mem3, (initialSize/2)*sizeof(int));270 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 ) ); 288 271 289 // check values of initial block 272 for ( int i=0;i<initialSize/2;i++) {273 if (mem1[i] != i || mem2[i] != i || mem3[i] != i) {274 psError(__FILE__,"Realloc didn't preserve the contents with shrinking buffer in %s.",275 __func__);276 break;277 }278 }279 280 psFree( mem1);281 psFree( mem2);282 psFree( mem3);283 284 return 0; 285 } 286 287 int TPallocCallback( void)288 { 289 int * mem1;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 } 296 } 297 298 psFree( mem1 ); 299 psFree( mem2 ); 300 psFree( mem3 ); 301 302 return 0; 303 } 304 305 int TPallocCallback( void ) 306 { 307 int * mem1; 290 308 int* mem2; 291 309 int* mem3; … … 293 311 const int initialSize = 100; 294 312 int mark; 295 313 296 314 allocCallbackCalled = 0; 297 315 freeCallbackCalled = 0; 298 psMemAllocateCallbackSet( memAllocateCallback);299 psMemFreeCallbackSet( memFreeCallback);300 301 psMemAllocateCallbackSetID( currentId+1);302 psMemFreeCallbackSetID( currentId+1);303 304 psLogMsg( __func__,PS_LOG_INFO,"call to psAlloc/psRealloc shall generate a callback if specified "305 "memory ID is allocated.");306 316 psMemAllocateCallbackSet( memAllocateCallback ); 317 psMemFreeCallbackSet( memFreeCallback ); 318 319 psMemAllocateCallbackSetID( currentId + 1 ); 320 psMemFreeCallbackSetID( currentId + 1 ); 321 322 psLogMsg( __func__, PS_LOG_INFO, "call to psAlloc/psRealloc shall generate a callback if specified " 323 "memory ID is allocated." ); 324 307 325 // allocate buffer with known values. 308 mem1 = ( int*) psAlloc(initialSize*sizeof(int));309 mem2 = ( int*) psAlloc(initialSize*sizeof(int));310 mem3 = ( int*) psAlloc(initialSize*sizeof(int));311 312 psFree( mem1);313 psFree( mem2);314 psFree( mem3);315 316 if ( allocCallbackCalled != 2 || freeCallbackCalled != 2) {317 psError(__FILE__,"alloc/free callbacks were not called the proper number of times in %s",318 __func__);319 return 1;320 }321 326 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 327 mem2 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 328 mem3 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 329 330 psFree( mem1 ); 331 psFree( mem2 ); 332 psFree( mem3 ); 333 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 322 340 allocCallbackCalled = 0; 323 341 freeCallbackCalled = 0; 324 342 325 343 mark = psMemGetId(); 326 327 mem1 = ( int*) psAlloc(initialSize*sizeof(int));328 329 psMemAllocateCallbackSetID( mark);330 331 mem1 = ( int*) psRealloc(mem1,initialSize*2*sizeof(int));332 333 psFree( mem1);334 335 if ( allocCallbackCalled != 2) {336 psError(__FILE__,"realloc callbacks were not called the proper number of times in %s",337 __func__);338 return 1;339 }340 341 return 0; 342 343 } 344 345 int TPcheckLeaks( void)344 345 mem1 = ( int* ) psAlloc( initialSize * sizeof( int ) ); 346 347 psMemAllocateCallbackSetID( mark ); 348 349 mem1 = ( int* ) psRealloc( mem1, initialSize * 2 * sizeof( int ) ); 350 351 psFree( mem1 ); 352 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 361 } 362 363 int TPcheckLeaks( void ) 346 364 { 347 365 const int numBuffers = 5; 348 int* buffers[ 5];366 int* buffers[ 5 ]; 349 367 int lcv; 350 368 int currentId = psMemGetId(); … … 352 370 int nLeaks = 0; 353 371 int lineMark = 0; 354 355 psLogMsg( __func__,PS_LOG_INFO,"psMemCheckLeaks shall return the number of blocks above an ID "356 "that are still allocated");357 358 for ( lcv=0;lcv<numBuffers;lcv++) {359 lineMark = __LINE__+1;360 buffers[lcv] = psAlloc(sizeof(int));361 }362 363 for ( lcv=1;lcv<numBuffers;lcv++) {364 psFree(buffers[lcv]);365 }366 367 psLogMsg( __func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce one instance.");368 369 nLeaks = psMemCheckLeaks( currentId, &blks, stderr);370 371 if ( nLeaks != 1) {372 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__);373 return 1;374 }375 376 if ( blks[0]->lineno != lineMark) {377 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one (line %d vs %d) in %s.",378 lineMark, blks[0]->lineno, __func__);379 return 1;380 }381 382 psFree( buffers[0]);383 psFree( blks);384 385 psLogMsg( __func__,PS_LOG_INFO,"Testing psMemCheckLeaks again with a different leak location");386 psMemCheckLeaks( currentId,NULL,stderr);387 388 for ( lcv=0;lcv<numBuffers;lcv++) {389 lineMark = __LINE__+1;390 buffers[lcv] = psAlloc(sizeof(int));391 }392 393 for ( lcv=0;lcv<numBuffers-1;lcv++) {394 psFree(buffers[lcv]);395 }396 397 psLogMsg( __func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce one error.");398 399 nLeaks = psMemCheckLeaks( currentId, &blks, stderr);400 401 if ( nLeaks != 1) {402 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__);403 return 1;404 }405 406 if ( blks[0]->lineno != lineMark) {407 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one in %s.",__func__);408 return 1;409 }410 411 psFree( buffers[4]);412 psFree( blks);413 414 psLogMsg( __func__,PS_LOG_INFO,"Testing psMemCheckLeaks again with multiple leak locations.");415 416 for ( lcv=0;lcv<numBuffers;lcv++) {417 lineMark = __LINE__+1;418 buffers[lcv] = psAlloc(sizeof(int));419 }420 421 for ( lcv=0;lcv<numBuffers;lcv++) {422 if (lcv%2 == 0) {423 psFree(buffers[lcv]);424 }425 }426 427 psLogMsg( __func__,PS_LOG_INFO,"following psMemCheckLeaks call should produce two errors.");428 429 nLeaks = psMemCheckLeaks( currentId, &blks, stderr);430 431 if ( nLeaks != 2) {432 psError(__FILE__,"psMemCheckLeaks should have found 1 leak, but found %d in %s.",nLeaks,__func__);433 return 1;434 }435 436 if ( blks[0]->lineno != lineMark) {437 psError(__FILE__,"psMemCheckLeaks found a leak other than the expected one in %s.",__func__);438 return 1;439 }440 441 psFree( blks);442 psFree( buffers[1]);443 psFree( buffers[3]);444 445 return 0; 446 } 447 448 int TPmemCorruption( void)449 { 450 int * buffer = NULL;372 373 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckLeaks shall return the number of blocks above an ID " 374 "that are still allocated" ); 375 376 for ( lcv = 0;lcv < numBuffers;lcv++ ) { 377 lineMark = __LINE__ + 1; 378 buffers[ lcv ] = psAlloc( sizeof( int ) ); 379 } 380 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 ) { 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 459 psFree( blks ); 460 psFree( buffers[ 1 ] ); 461 psFree( buffers[ 3 ] ); 462 463 return 0; 464 } 465 466 int TPmemCorruption( void ) 467 { 468 int * buffer = NULL; 451 469 int oldValue = 0; 452 470 int corruptions = 0; 453 471 psMemProblemCallback cb; 454 455 psLogMsg( __func__,PS_LOG_INFO,"psMemCheckCorruption shall detect memory corruptions");456 457 buffer = psAlloc( sizeof(int));458 472 473 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption shall detect memory corruptions" ); 474 475 buffer = psAlloc( sizeof( int ) ); 476 459 477 // cause memory corruption via buffer underflow 460 478 *buffer = 1; … … 462 480 oldValue = *buffer; 463 481 *buffer = 2; 464 482 465 483 problemCallbackCalled = 0; 466 cb = psMemProblemCallbackSet( memProblemCallback);467 468 psLogMsg( __func__,PS_LOG_INFO,"psMemCheckCorruption should output an error message and "469 "memProblemCallback callback should be called.");470 471 corruptions = psMemCheckCorruption( 0);472 484 cb = psMemProblemCallbackSet( memProblemCallback ); 485 486 psLogMsg( __func__, PS_LOG_INFO, "psMemCheckCorruption should output an error message and " 487 "memProblemCallback callback should be called." ); 488 489 corruptions = psMemCheckCorruption( 0 ); 490 473 491 // restore the memory problem callback 474 psMemProblemCallbackSet( cb);475 492 psMemProblemCallbackSet( cb ); 493 476 494 // restore the value, 'uncorrupting' the buffer 477 495 *buffer = oldValue; 478 496 buffer++; 479 480 psFree( buffer);481 482 if ( corruptions != 1) {483 psError(__FILE__,"Expected one memory corruption but found %d in %s.",484 corruptions, __func__);485 return 1;486 }487 488 if ( problemCallbackCalled != 1) {489 psError(__FILE__,"The memProblemCallback was not invoked but should have been in %s",490 __func__);491 return 1;492 }493 494 return 0; 495 496 } 497 498 void memProblemCallback( const psMemBlock *ptr, const char *file, int lineno)499 { 500 psLogMsg( __func__,PS_LOG_INFO,"memory callback called for id %d (%s:%d).",501 ptr->id, file, lineno);497 498 psFree( buffer ); 499 500 if ( corruptions != 1 ) { 501 psError( __FILE__, "Expected one memory corruption but found %d in %s.", 502 corruptions, __func__ ); 503 return 1; 504 } 505 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 514 } 515 516 void memProblemCallback( const psMemBlock *ptr, const char *file, int lineno ) 517 { 518 psLogMsg( __func__, PS_LOG_INFO, "memory callback called for id %d (%s:%d).", 519 ptr->id, file, lineno ); 502 520 problemCallbackCalled++; 503 return ;504 } 505 506 psMemoryId memAllocateCallback( const psMemBlock *ptr)507 { 508 psLogMsg( __func__,PS_LOG_INFO,"block %d was (re)allocated",ptr->id);521 return ; 522 } 523 524 psMemoryId memAllocateCallback( const psMemBlock *ptr ) 525 { 526 psLogMsg( __func__, PS_LOG_INFO, "block %d was (re)allocated", ptr->id ); 509 527 allocCallbackCalled++; 510 528 return 1; 511 529 } 512 530 513 psMemoryId memFreeCallback( const psMemBlock *ptr)514 { 515 psLogMsg( __func__,PS_LOG_INFO,"block %d was freed",ptr->id);531 psMemoryId memFreeCallback( const psMemBlock *ptr ) 532 { 533 psLogMsg( __func__, PS_LOG_INFO, "block %d was freed", ptr->id ); 516 534 freeCallbackCalled++; 517 535 return 1; 518 536 } 519 537 520 void* TPOutOfMemoryExhaustedCallback( size_t size)521 { 522 psLogMsg( __func__,PS_LOG_INFO,"Custom MemExhaustedCallback was invoked.");538 void* TPOutOfMemoryExhaustedCallback( size_t size ) 539 { 540 psLogMsg( __func__, PS_LOG_INFO, "Custom MemExhaustedCallback was invoked." ); 523 541 exhaustedCallbackCalled++; 524 542 return NULL; 525 543 } 526 544 527 int TPmultipleFree( void)528 { 529 530 void * buffer = psAlloc(1024);531 532 psFree( buffer);533 534 psLogMsg( __func__,PS_LOG_INFO,"Next should be an error about multiple freeing.");535 psFree( buffer);536 537 return 0; 538 } 545 int TPmultipleFree( void ) 546 { 547 548 void * buffer = psAlloc( 1024 ); 549 550 psFree( buffer ); 551 552 psLogMsg( __func__, PS_LOG_INFO, "Next should be an error about multiple freeing." ); 553 psFree( buffer ); 554 555 return 0; 556 }
Note:
See TracChangeset
for help on using the changeset viewer.
