Changeset 452
- Timestamp:
- Apr 19, 2004, 10:19:22 AM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 4 edited
-
sys/psMemory.c (modified) (19 diffs)
-
sys/psMemory.h (modified) (5 diffs)
-
sysUtils/psMemory.c (modified) (19 diffs)
-
sysUtils/psMemory.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.c
r451 r452 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-19 20:1 4:04$10 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-19 20:19:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 49 49 50 50 if (func != NULL) { 51 memExhaustedCallback = func;52 } else {53 memExhaustedCallback = memExhaustedCallbackDefault;54 }51 memExhaustedCallback = func; 52 } else { 53 memExhaustedCallback = memExhaustedCallbackDefault; 54 } 55 55 56 56 return old; … … 58 58 59 59 static void memProblemCallbackDefault(const psMemBlock *ptr, 60 const char *file, int lineno)60 const char *file, int lineno) 61 61 { 62 62 checkMemBlock(ptr, __func__); 63 63 64 if (ptr->refCounter <= 0) {65 psError(__func__,66 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",67 ptr->id, ptr->file, ptr->lineno, file, lineno);68 } else if (ptr->refCounter > 1) {69 psError(__func__,70 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",71 ptr->id, ptr->file, ptr->lineno, file, lineno);72 }73 74 if (lineno > 0) {64 if (ptr->refCounter <= 0) { 65 psError(__func__, 66 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 67 ptr->id, ptr->file, ptr->lineno, file, lineno); 68 } else if (ptr->refCounter > 1) { 69 psError(__func__, 70 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 71 ptr->id, ptr->file, ptr->lineno, file, lineno); 72 } 73 74 if (lineno > 0) { 75 75 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 76 76 } … … 83 83 84 84 if (func != NULL) { 85 memProblemCallback = func;86 } else {87 memProblemCallback = memProblemCallbackDefault;88 }85 memProblemCallback = func; 86 } else { 87 memProblemCallback = memProblemCallbackDefault; 88 } 89 89 90 90 return old; … … 145 145 146 146 if (func != NULL) { 147 memAllocateCallback = func;148 } else {149 memAllocateCallback = memAllocateCallbackDefault;150 }147 memAllocateCallback = func; 148 } else { 149 memAllocateCallback = memAllocateCallbackDefault; 150 } 151 151 152 152 return old; … … 158 158 159 159 if (func != NULL) { 160 memFreeCallback = func;161 } else {162 memFreeCallback = memFreeCallbackDefault;163 }160 memFreeCallback = func; 161 } else { 162 memFreeCallback = memFreeCallbackDefault; 163 } 164 164 165 165 return old; … … 185 185 static int 186 186 checkMemBlock(const psMemBlock *m, 187 const char* funcName)187 const char* funcName) 188 188 { 189 189 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, … … 212 212 int nbad = 0; // number of bad blocks 213 213 214 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 215 pthread_mutex_lock(&memBlockListMutex); 216 217 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 218 { 214 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 215 pthread_mutex_lock(&memBlockListMutex); 216 217 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 219 218 if (checkMemBlock(iter, __func__)) { 220 nbad++;221 222 memProblemCallback(iter, __func__, __LINE__);223 224 if (abort_on_error) {225 // release the lock on the memblock list226 pthread_mutex_unlock(&memBlockListMutex);227 psAbort(__func__, "Detected memory corruption");228 return nbad;229 }230 } 231 } 232 233 // release the lock on the memblock list234 pthread_mutex_unlock(&memBlockListMutex);219 nbad++; 220 221 memProblemCallback(iter, __func__, __LINE__); 222 223 if (abort_on_error) { 224 // release the lock on the memblock list 225 pthread_mutex_unlock(&memBlockListMutex); 226 psAbort(__func__, "Detected memory corruption"); 227 return nbad; 228 } 229 } 230 } 231 232 // release the lock on the memblock list 233 pthread_mutex_unlock(&memBlockListMutex); 235 234 return nbad; 236 235 } … … 254 253 *(unsigned int*)&ptr->lineno = lineno; 255 254 ptr->startblock = P_PS_MEMMAGIC; 256 ptr->endblock = P_PS_MEMMAGIC;255 ptr->endblock = P_PS_MEMMAGIC; 257 256 ptr->refCounter = 1; // one user so far 258 257 259 // need exclusive access of the memory block list now...260 pthread_mutex_lock(&memBlockListMutex);261 262 // insert the new block to the front of the memBlock linked-list263 ptr->previousBlock = NULL;264 ptr->nextBlock = lastMemBlockAllocated;265 if (ptr->nextBlock != NULL) {266 ptr->nextBlock->previousBlock = ptr;267 }268 lastMemBlockAllocated = ptr;269 270 pthread_mutex_unlock(&memBlockListMutex);258 // need exclusive access of the memory block list now... 259 pthread_mutex_lock(&memBlockListMutex); 260 261 // insert the new block to the front of the memBlock linked-list 262 ptr->previousBlock = NULL; 263 ptr->nextBlock = lastMemBlockAllocated; 264 if (ptr->nextBlock != NULL) { 265 ptr->nextBlock->previousBlock = ptr; 266 } 267 lastMemBlockAllocated = ptr; 268 269 pthread_mutex_unlock(&memBlockListMutex); 271 270 272 271 // Did the user ask to be informed about this allocation? … … 275 274 } 276 275 277 // And return the user the memory that they allocated276 // And return the user the memory that they allocated 278 277 return ptr + 1; // user memory 279 278 } … … 286 285 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 287 286 288 pthread_mutex_lock(&memBlockListMutex);289 290 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);291 292 // the block location may have changed, so fix the linked list addresses.293 if (ptr->nextBlock != NULL) {294 ptr->nextBlock->previousBlock = ptr;295 }296 if (ptr->previousBlock != NULL) {297 ptr->previousBlock->nextBlock = ptr;298 }299 300 pthread_mutex_unlock(&memBlockListMutex);301 302 return ptr + 1; // usr memory287 pthread_mutex_lock(&memBlockListMutex); 288 289 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size); 290 291 // the block location may have changed, so fix the linked list addresses. 292 if (ptr->nextBlock != NULL) { 293 ptr->nextBlock->previousBlock = ptr; 294 } 295 if (ptr->previousBlock != NULL) { 296 ptr->previousBlock->nextBlock = ptr; 297 } 298 299 pthread_mutex_unlock(&memBlockListMutex); 300 301 return ptr + 1; // usr memory 303 302 } 304 303 } … … 312 311 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 313 312 314 // Did the user ask to be informed about this deallocation?313 // Did the user ask to be informed about this deallocation? 315 314 if (ptr->id == p_psMemFreeID) { 316 315 p_psMemFreeID += memFreeCallback(ptr); … … 321 320 } else { 322 321 if (ptr->refCounter > 1) { 323 psError(__func__,"The buffer being freed is referenced elsewhere. "324 "Buffer's reference count was decremented instead.");322 psError(__func__,"The buffer being freed is referenced elsewhere. " 323 "Buffer's reference count was decremented instead."); 325 324 ptr->refCounter--; 326 325 memProblemCallback(ptr, file, lineno); 327 326 } else { 328 327 329 // cut the memBlock out of the memBlock list330 pthread_mutex_lock(&memBlockListMutex);331 if (ptr->nextBlock != NULL) {332 ptr->nextBlock->previousBlock = ptr->previousBlock;333 }334 if (ptr->previousBlock != NULL) {335 ptr->previousBlock->nextBlock = ptr->nextBlock;336 }337 if (lastMemBlockAllocated == ptr) {338 lastMemBlockAllocated = ptr->nextBlock;339 }340 341 pthread_mutex_unlock(&memBlockListMutex);342 343 free(ptr);344 }328 // cut the memBlock out of the memBlock list 329 pthread_mutex_lock(&memBlockListMutex); 330 if (ptr->nextBlock != NULL) { 331 ptr->nextBlock->previousBlock = ptr->previousBlock; 332 } 333 if (ptr->previousBlock != NULL) { 334 ptr->previousBlock->nextBlock = ptr->nextBlock; 335 } 336 if (lastMemBlockAllocated == ptr) { 337 lastMemBlockAllocated = ptr->nextBlock; 338 } 339 340 pthread_mutex_unlock(&memBlockListMutex); 341 342 free(ptr); 343 } 345 344 } 346 345 } … … 356 355 { 357 356 int nleak = 0; 358 int j = 0; 359 360 pthread_mutex_lock(&memBlockListMutex); 361 362 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 357 int j = 0; 358 359 pthread_mutex_lock(&memBlockListMutex); 360 361 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 362 { 363 363 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 364 nleak++; 365 366 if (fd != NULL) { 367 if (nleak == 1) { 368 fprintf(fd, " %20s:line ID\n", "file"); 369 } 370 371 fprintf(fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id); 372 } 373 } 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 if (nleak == 0 || arr == NULL) { 364 nleak++; 365 366 if (fd != NULL) { 367 if (nleak == 1) { 368 fprintf(fd, " %20s:line ID\n", "file"); 369 } 370 371 fprintf(fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id); 372 } 373 } 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 if (nleak == 0 || arr == NULL) 379 { 379 380 return nleak; 380 381 } … … 384 385 *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__); 385 386 386 pthread_mutex_lock(&memBlockListMutex); 387 388 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 387 pthread_mutex_lock(&memBlockListMutex); 388 389 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 390 { 389 391 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 390 (*arr)[j++] = iter;391 if (j == nleak) { // found them all392 break;393 }392 (*arr)[j++] = iter; 393 if (j == nleak) { // found them all 394 break; 395 } 394 396 } 395 397 } … … 418 420 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 419 421 { 420 if (vptr == NULL) { 422 if (vptr == NULL) 423 { 421 424 return vptr; 422 425 } … … 424 427 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 425 428 426 if (checkMemBlock(ptr, __func__)) { 429 if (checkMemBlock(ptr, __func__)) 430 { 427 431 memProblemCallback(ptr, __func__, __LINE__); 428 432 } … … 435 439 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter 436 440 { 437 if (vptr == NULL) { 441 if (vptr == NULL) 442 { 438 443 return vptr; 439 444 } … … 441 446 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 442 447 443 if (checkMemBlock(ptr, __func__)) { 448 if (checkMemBlock(ptr, __func__)) 449 { 444 450 memProblemCallback(ptr, __func__, __LINE__); 445 451 } -
trunk/psLib/src/sys/psMemory.h
r451 r452 14 14 * @author Robert Lupton, Princeton University 15 15 * 16 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-04-19 20:1 4:04$16 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-19 20:19:22 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 28 28 { 29 29 const void* startblock; ///< initialised to p_psMEMMAGIC 30 struct psMemBlock* previousBlock; ///< previous block in allocation list31 struct psMemBlock* nextBlock; ///< next block allocation list30 struct psMemBlock* previousBlock; ///< previous block in allocation list 31 struct psMemBlock* nextBlock; ///< next block allocation list 32 32 const unsigned long id; ///< a unique ID for this allocation 33 33 const char* file; ///< set from __FILE__ in e.g. p_psAlloc … … 85 85 /// Check for memory leaks 86 86 int psMemCheckLeaks( 87 int id0, ///< don't list blocks with id < id088 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL89 FILE *fd ///< print list of leaks to fd (or NULL)87 int id0, ///< don't list blocks with id < id0 88 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 89 FILE *fd ///< print list of leaks to fd (or NULL) 90 90 ); 91 91 92 92 /// Check for memory corruption 93 93 int psMemCheckCorruption( 94 int abort_on_error ///< Abort on detecting corruption?94 int abort_on_error ///< Abort on detecting corruption? 95 95 ); 96 96 97 97 /// Return reference counter 98 98 int psMemGetRefCounter( 99 void *vptr ///< Pointer to get refCounter for99 void *vptr ///< Pointer to get refCounter for 100 100 ); 101 101 102 102 /// Increment reference counter and return the pointer 103 103 void *psMemIncrRefCounter( 104 void *vptr ///< Pointer to increment refCounter, and return104 void *vptr ///< Pointer to increment refCounter, and return 105 105 ); 106 106 107 107 /// Decrement reference counter and return the pointer 108 108 void *psMemDecrRefCounter( 109 void *vptr ///< Pointer to decrement refCounter, and return109 void *vptr ///< Pointer to decrement refCounter, and return 110 110 ); 111 111 112 112 /// Set callback for problems 113 113 psMemProblemCallback psMemProblemCallbackSet( 114 psMemProblemCallback func ///< Function to run114 psMemProblemCallback func ///< Function to run 115 115 ); 116 116 117 117 /// Set callback for out-of-memory 118 118 psMemExhaustedCallback psMemExhaustedCallbackSet( 119 psMemExhaustedCallback func ///< Function to run119 psMemExhaustedCallback func ///< Function to run 120 120 ); 121 121 122 122 /// Set call back for when a particular memory block is allocated 123 123 psMemAllocateCallback psMemAllocateCallbackSet( 124 psMemAllocateCallback func ///< Function to run124 psMemAllocateCallback func ///< Function to run 125 125 ); 126 126 127 127 /// Set call back for when a particular memory block is freed 128 128 psMemFreeCallback psMemFreeCallbackSet( 129 psMemFreeCallback func ///< Function to run129 psMemFreeCallback func ///< Function to run 130 130 ); 131 131 … … 136 136 /// set p_psMemAllocateID to id 137 137 long psMemAllocateIDSet( 138 long id ///< ID to set138 long id ///< ID to set 139 139 ); 140 140 141 141 /// set p_psMemFreeID to id 142 142 long psMemFreeIDSet( 143 long id ///< ID to set143 long id ///< ID to set 144 144 ); 145 145 … … 159 159 */ 160 160 #ifndef PS_ALLOW_MALLOC 161 #define malloc(S) #pragma error Use of malloc is not allowed. Use psAlloc instead.162 #define realloc(P,S) #pragma error Use of realloc is not allowed. Use psRealloc instead.163 #define calloc(S) #pragma error Use of calloc is not allowed. Use psAlloc instead.164 #define free(P) #pragma error Use of free is not allowed. Use psFree instead.161 #define malloc(S) #pragma error Use of malloc is not allowed. Use psAlloc instead. 162 #define realloc(P,S) #pragma error Use of realloc is not allowed. Use psRealloc instead. 163 #define calloc(S) #pragma error Use of calloc is not allowed. Use psAlloc instead. 164 #define free(P) #pragma error Use of free is not allowed. Use psFree instead. 165 165 #endif 166 166 -
trunk/psLib/src/sysUtils/psMemory.c
r451 r452 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-19 20:1 4:04$10 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-19 20:19:22 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 49 49 50 50 if (func != NULL) { 51 memExhaustedCallback = func;52 } else {53 memExhaustedCallback = memExhaustedCallbackDefault;54 }51 memExhaustedCallback = func; 52 } else { 53 memExhaustedCallback = memExhaustedCallbackDefault; 54 } 55 55 56 56 return old; … … 58 58 59 59 static void memProblemCallbackDefault(const psMemBlock *ptr, 60 const char *file, int lineno)60 const char *file, int lineno) 61 61 { 62 62 checkMemBlock(ptr, __func__); 63 63 64 if (ptr->refCounter <= 0) {65 psError(__func__,66 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",67 ptr->id, ptr->file, ptr->lineno, file, lineno);68 } else if (ptr->refCounter > 1) {69 psError(__func__,70 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",71 ptr->id, ptr->file, ptr->lineno, file, lineno);72 }73 74 if (lineno > 0) {64 if (ptr->refCounter <= 0) { 65 psError(__func__, 66 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 67 ptr->id, ptr->file, ptr->lineno, file, lineno); 68 } else if (ptr->refCounter > 1) { 69 psError(__func__, 70 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", 71 ptr->id, ptr->file, ptr->lineno, file, lineno); 72 } 73 74 if (lineno > 0) { 75 75 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 76 76 } … … 83 83 84 84 if (func != NULL) { 85 memProblemCallback = func;86 } else {87 memProblemCallback = memProblemCallbackDefault;88 }85 memProblemCallback = func; 86 } else { 87 memProblemCallback = memProblemCallbackDefault; 88 } 89 89 90 90 return old; … … 145 145 146 146 if (func != NULL) { 147 memAllocateCallback = func;148 } else {149 memAllocateCallback = memAllocateCallbackDefault;150 }147 memAllocateCallback = func; 148 } else { 149 memAllocateCallback = memAllocateCallbackDefault; 150 } 151 151 152 152 return old; … … 158 158 159 159 if (func != NULL) { 160 memFreeCallback = func;161 } else {162 memFreeCallback = memFreeCallbackDefault;163 }160 memFreeCallback = func; 161 } else { 162 memFreeCallback = memFreeCallbackDefault; 163 } 164 164 165 165 return old; … … 185 185 static int 186 186 checkMemBlock(const psMemBlock *m, 187 const char* funcName)187 const char* funcName) 188 188 { 189 189 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, … … 212 212 int nbad = 0; // number of bad blocks 213 213 214 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 215 pthread_mutex_lock(&memBlockListMutex); 216 217 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 218 { 214 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 215 pthread_mutex_lock(&memBlockListMutex); 216 217 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 219 218 if (checkMemBlock(iter, __func__)) { 220 nbad++;221 222 memProblemCallback(iter, __func__, __LINE__);223 224 if (abort_on_error) {225 // release the lock on the memblock list226 pthread_mutex_unlock(&memBlockListMutex);227 psAbort(__func__, "Detected memory corruption");228 return nbad;229 }230 } 231 } 232 233 // release the lock on the memblock list234 pthread_mutex_unlock(&memBlockListMutex);219 nbad++; 220 221 memProblemCallback(iter, __func__, __LINE__); 222 223 if (abort_on_error) { 224 // release the lock on the memblock list 225 pthread_mutex_unlock(&memBlockListMutex); 226 psAbort(__func__, "Detected memory corruption"); 227 return nbad; 228 } 229 } 230 } 231 232 // release the lock on the memblock list 233 pthread_mutex_unlock(&memBlockListMutex); 235 234 return nbad; 236 235 } … … 254 253 *(unsigned int*)&ptr->lineno = lineno; 255 254 ptr->startblock = P_PS_MEMMAGIC; 256 ptr->endblock = P_PS_MEMMAGIC;255 ptr->endblock = P_PS_MEMMAGIC; 257 256 ptr->refCounter = 1; // one user so far 258 257 259 // need exclusive access of the memory block list now...260 pthread_mutex_lock(&memBlockListMutex);261 262 // insert the new block to the front of the memBlock linked-list263 ptr->previousBlock = NULL;264 ptr->nextBlock = lastMemBlockAllocated;265 if (ptr->nextBlock != NULL) {266 ptr->nextBlock->previousBlock = ptr;267 }268 lastMemBlockAllocated = ptr;269 270 pthread_mutex_unlock(&memBlockListMutex);258 // need exclusive access of the memory block list now... 259 pthread_mutex_lock(&memBlockListMutex); 260 261 // insert the new block to the front of the memBlock linked-list 262 ptr->previousBlock = NULL; 263 ptr->nextBlock = lastMemBlockAllocated; 264 if (ptr->nextBlock != NULL) { 265 ptr->nextBlock->previousBlock = ptr; 266 } 267 lastMemBlockAllocated = ptr; 268 269 pthread_mutex_unlock(&memBlockListMutex); 271 270 272 271 // Did the user ask to be informed about this allocation? … … 275 274 } 276 275 277 // And return the user the memory that they allocated276 // And return the user the memory that they allocated 278 277 return ptr + 1; // user memory 279 278 } … … 286 285 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 287 286 288 pthread_mutex_lock(&memBlockListMutex);289 290 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);291 292 // the block location may have changed, so fix the linked list addresses.293 if (ptr->nextBlock != NULL) {294 ptr->nextBlock->previousBlock = ptr;295 }296 if (ptr->previousBlock != NULL) {297 ptr->previousBlock->nextBlock = ptr;298 }299 300 pthread_mutex_unlock(&memBlockListMutex);301 302 return ptr + 1; // usr memory287 pthread_mutex_lock(&memBlockListMutex); 288 289 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size); 290 291 // the block location may have changed, so fix the linked list addresses. 292 if (ptr->nextBlock != NULL) { 293 ptr->nextBlock->previousBlock = ptr; 294 } 295 if (ptr->previousBlock != NULL) { 296 ptr->previousBlock->nextBlock = ptr; 297 } 298 299 pthread_mutex_unlock(&memBlockListMutex); 300 301 return ptr + 1; // usr memory 303 302 } 304 303 } … … 312 311 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 313 312 314 // Did the user ask to be informed about this deallocation?313 // Did the user ask to be informed about this deallocation? 315 314 if (ptr->id == p_psMemFreeID) { 316 315 p_psMemFreeID += memFreeCallback(ptr); … … 321 320 } else { 322 321 if (ptr->refCounter > 1) { 323 psError(__func__,"The buffer being freed is referenced elsewhere. "324 "Buffer's reference count was decremented instead.");322 psError(__func__,"The buffer being freed is referenced elsewhere. " 323 "Buffer's reference count was decremented instead."); 325 324 ptr->refCounter--; 326 325 memProblemCallback(ptr, file, lineno); 327 326 } else { 328 327 329 // cut the memBlock out of the memBlock list330 pthread_mutex_lock(&memBlockListMutex);331 if (ptr->nextBlock != NULL) {332 ptr->nextBlock->previousBlock = ptr->previousBlock;333 }334 if (ptr->previousBlock != NULL) {335 ptr->previousBlock->nextBlock = ptr->nextBlock;336 }337 if (lastMemBlockAllocated == ptr) {338 lastMemBlockAllocated = ptr->nextBlock;339 }340 341 pthread_mutex_unlock(&memBlockListMutex);342 343 free(ptr);344 }328 // cut the memBlock out of the memBlock list 329 pthread_mutex_lock(&memBlockListMutex); 330 if (ptr->nextBlock != NULL) { 331 ptr->nextBlock->previousBlock = ptr->previousBlock; 332 } 333 if (ptr->previousBlock != NULL) { 334 ptr->previousBlock->nextBlock = ptr->nextBlock; 335 } 336 if (lastMemBlockAllocated == ptr) { 337 lastMemBlockAllocated = ptr->nextBlock; 338 } 339 340 pthread_mutex_unlock(&memBlockListMutex); 341 342 free(ptr); 343 } 345 344 } 346 345 } … … 356 355 { 357 356 int nleak = 0; 358 int j = 0; 359 360 pthread_mutex_lock(&memBlockListMutex); 361 362 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 357 int j = 0; 358 359 pthread_mutex_lock(&memBlockListMutex); 360 361 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 362 { 363 363 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 364 nleak++; 365 366 if (fd != NULL) { 367 if (nleak == 1) { 368 fprintf(fd, " %20s:line ID\n", "file"); 369 } 370 371 fprintf(fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id); 372 } 373 } 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 if (nleak == 0 || arr == NULL) { 364 nleak++; 365 366 if (fd != NULL) { 367 if (nleak == 1) { 368 fprintf(fd, " %20s:line ID\n", "file"); 369 } 370 371 fprintf(fd, " %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id); 372 } 373 } 374 } 375 376 pthread_mutex_unlock(&memBlockListMutex); 377 378 if (nleak == 0 || arr == NULL) 379 { 379 380 return nleak; 380 381 } … … 384 385 *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__); 385 386 386 pthread_mutex_lock(&memBlockListMutex); 387 388 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 387 pthread_mutex_lock(&memBlockListMutex); 388 389 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 390 { 389 391 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 390 (*arr)[j++] = iter;391 if (j == nleak) { // found them all392 break;393 }392 (*arr)[j++] = iter; 393 if (j == nleak) { // found them all 394 break; 395 } 394 396 } 395 397 } … … 418 420 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter 419 421 { 420 if (vptr == NULL) { 422 if (vptr == NULL) 423 { 421 424 return vptr; 422 425 } … … 424 427 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 425 428 426 if (checkMemBlock(ptr, __func__)) { 429 if (checkMemBlock(ptr, __func__)) 430 { 427 431 memProblemCallback(ptr, __func__, __LINE__); 428 432 } … … 435 439 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter 436 440 { 437 if (vptr == NULL) { 441 if (vptr == NULL) 442 { 438 443 return vptr; 439 444 } … … 441 446 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 442 447 443 if (checkMemBlock(ptr, __func__)) { 448 if (checkMemBlock(ptr, __func__)) 449 { 444 450 memProblemCallback(ptr, __func__, __LINE__); 445 451 } -
trunk/psLib/src/sysUtils/psMemory.h
r451 r452 14 14 * @author Robert Lupton, Princeton University 15 15 * 16 * @version $Revision: 1. 3$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-04-19 20:1 4:04$16 * @version $Revision: 1.4 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-04-19 20:19:22 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 28 28 { 29 29 const void* startblock; ///< initialised to p_psMEMMAGIC 30 struct psMemBlock* previousBlock; ///< previous block in allocation list31 struct psMemBlock* nextBlock; ///< next block allocation list30 struct psMemBlock* previousBlock; ///< previous block in allocation list 31 struct psMemBlock* nextBlock; ///< next block allocation list 32 32 const unsigned long id; ///< a unique ID for this allocation 33 33 const char* file; ///< set from __FILE__ in e.g. p_psAlloc … … 85 85 /// Check for memory leaks 86 86 int psMemCheckLeaks( 87 int id0, ///< don't list blocks with id < id088 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL89 FILE *fd ///< print list of leaks to fd (or NULL)87 int id0, ///< don't list blocks with id < id0 88 psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL 89 FILE *fd ///< print list of leaks to fd (or NULL) 90 90 ); 91 91 92 92 /// Check for memory corruption 93 93 int psMemCheckCorruption( 94 int abort_on_error ///< Abort on detecting corruption?94 int abort_on_error ///< Abort on detecting corruption? 95 95 ); 96 96 97 97 /// Return reference counter 98 98 int psMemGetRefCounter( 99 void *vptr ///< Pointer to get refCounter for99 void *vptr ///< Pointer to get refCounter for 100 100 ); 101 101 102 102 /// Increment reference counter and return the pointer 103 103 void *psMemIncrRefCounter( 104 void *vptr ///< Pointer to increment refCounter, and return104 void *vptr ///< Pointer to increment refCounter, and return 105 105 ); 106 106 107 107 /// Decrement reference counter and return the pointer 108 108 void *psMemDecrRefCounter( 109 void *vptr ///< Pointer to decrement refCounter, and return109 void *vptr ///< Pointer to decrement refCounter, and return 110 110 ); 111 111 112 112 /// Set callback for problems 113 113 psMemProblemCallback psMemProblemCallbackSet( 114 psMemProblemCallback func ///< Function to run114 psMemProblemCallback func ///< Function to run 115 115 ); 116 116 117 117 /// Set callback for out-of-memory 118 118 psMemExhaustedCallback psMemExhaustedCallbackSet( 119 psMemExhaustedCallback func ///< Function to run119 psMemExhaustedCallback func ///< Function to run 120 120 ); 121 121 122 122 /// Set call back for when a particular memory block is allocated 123 123 psMemAllocateCallback psMemAllocateCallbackSet( 124 psMemAllocateCallback func ///< Function to run124 psMemAllocateCallback func ///< Function to run 125 125 ); 126 126 127 127 /// Set call back for when a particular memory block is freed 128 128 psMemFreeCallback psMemFreeCallbackSet( 129 psMemFreeCallback func ///< Function to run129 psMemFreeCallback func ///< Function to run 130 130 ); 131 131 … … 136 136 /// set p_psMemAllocateID to id 137 137 long psMemAllocateIDSet( 138 long id ///< ID to set138 long id ///< ID to set 139 139 ); 140 140 141 141 /// set p_psMemFreeID to id 142 142 long psMemFreeIDSet( 143 long id ///< ID to set143 long id ///< ID to set 144 144 ); 145 145 … … 159 159 */ 160 160 #ifndef PS_ALLOW_MALLOC 161 #define malloc(S) #pragma error Use of malloc is not allowed. Use psAlloc instead.162 #define realloc(P,S) #pragma error Use of realloc is not allowed. Use psRealloc instead.163 #define calloc(S) #pragma error Use of calloc is not allowed. Use psAlloc instead.164 #define free(P) #pragma error Use of free is not allowed. Use psFree instead.161 #define malloc(S) #pragma error Use of malloc is not allowed. Use psAlloc instead. 162 #define realloc(P,S) #pragma error Use of realloc is not allowed. Use psRealloc instead. 163 #define calloc(S) #pragma error Use of calloc is not allowed. Use psAlloc instead. 164 #define free(P) #pragma error Use of free is not allowed. Use psFree instead. 165 165 #endif 166 166
Note:
See TracChangeset
for help on using the changeset viewer.
