Changeset 469
- Timestamp:
- Apr 19, 2004, 4:58:46 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 2 edited
-
sys/psMemory.c (modified) (18 diffs)
-
sysUtils/psMemory.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.c
r468 r469 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-20 02:5 2:48$10 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-20 02:58:46 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 48 48 psMemExhaustedCallback old = memExhaustedCallback; 49 49 50 if (func != NULL) { 50 if (func != NULL) 51 { 51 52 memExhaustedCallback = func; 52 } else { 53 } 54 else 55 { 53 56 memExhaustedCallback = memExhaustedCallbackDefault; 54 57 } … … 62 65 checkMemBlock(ptr, __func__); 63 66 64 if (ptr->refCounter <= 0) { 67 if (ptr->refCounter <= 0) 68 { 65 69 psError(__func__, 66 70 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 67 71 ptr->id, ptr->file, ptr->lineno, file, lineno); 68 } else if (ptr->refCounter > 1) { 72 } 73 else if (ptr->refCounter > 1) 74 { 69 75 psError(__func__, 70 76 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", … … 72 78 } 73 79 74 if (lineno > 0) { 80 if (lineno > 0) 81 { 75 82 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 76 83 } … … 82 89 psMemProblemCallback old = memProblemCallback; 83 90 84 if (func != NULL) { 91 if (func != NULL) 92 { 85 93 memProblemCallback = func; 86 } else { 94 } 95 else 96 { 87 97 memProblemCallback = memProblemCallbackDefault; 88 98 } … … 144 154 psMemFreeCallback old = memAllocateCallback; 145 155 146 if (func != NULL) { 156 if (func != NULL) 157 { 147 158 memAllocateCallback = func; 148 } else { 159 } 160 else 161 { 149 162 memAllocateCallback = memAllocateCallbackDefault; 150 163 } … … 157 170 psMemFreeCallback old = memFreeCallback; 158 171 159 if (func != NULL) { 172 if (func != NULL) 173 { 160 174 memFreeCallback = func; 161 } else { 175 } 176 else 177 { 162 178 memFreeCallback = memFreeCallbackDefault; 163 179 } … … 188 204 // we shouldn't call such things as p_psAlloc/p_psFree here. 189 205 190 if (m == NULL) { 206 if (m == NULL) 207 { 191 208 psError(funcName,"Memory Corruption: NULL memory block found."); 192 209 return(1); 193 210 } 194 211 195 if (!ALIGNED(m)) { 212 if (!ALIGNED(m)) 213 { 196 214 psError(funcName, "psMemCheckCorruption: non-aligned memory block"); 197 215 return(1); 198 216 } 199 217 200 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 218 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) 219 { 201 220 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id); 202 221 return(1); … … 213 232 pthread_mutex_lock(&memBlockListMutex); 214 233 215 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 216 if (checkMemBlock(iter, __func__)) { 234 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 235 { 236 if (checkMemBlock(iter, __func__)) 237 { 217 238 nbad++; 218 239 219 240 memProblemCallback(iter, __func__, __LINE__); 220 241 221 if (abort_on_error) { 242 if (abort_on_error) 243 { 222 244 // release the lock on the memblock list 223 245 pthread_mutex_unlock(&memBlockListMutex); … … 237 259 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size); 238 260 239 if (ptr == NULL) { 261 if (ptr == NULL) 262 { 240 263 ptr = memExhaustedCallback(size); 241 if (ptr == NULL) { 264 if (ptr == NULL) 265 { 242 266 psAbort(__func__, "Failed to allocate %ld bytes at %s:%d", 243 267 size, file, lineno); … … 258 282 ptr->previousBlock = NULL; 259 283 ptr->nextBlock = lastMemBlockAllocated; 260 if (ptr->nextBlock != NULL) { 284 if (ptr->nextBlock != NULL) 285 { 261 286 ptr->nextBlock->previousBlock = ptr; 262 287 } … … 266 291 267 292 // Did the user ask to be informed about this allocation? 268 if (ptr->id == p_psMemAllocateID) { 293 if (ptr->id == p_psMemAllocateID) 294 { 269 295 p_psMemAllocateID += memAllocateCallback(ptr); 270 296 } … … 276 302 void *p_psRealloc(void *vptr, size_t size, const char *file, int lineno) 277 303 { 278 if (vptr == NULL) { 304 if (vptr == NULL) 305 { 279 306 return p_psAlloc(size, file, lineno); 280 } else { 307 } 308 else 309 { 281 310 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 282 311 … … 286 315 287 316 // the block location may have changed, so fix the linked list addresses. 288 if (ptr->nextBlock != NULL) { 317 if (ptr->nextBlock != NULL) 318 { 289 319 ptr->nextBlock->previousBlock = ptr; 290 320 } 291 if (ptr->previousBlock != NULL) { 321 if (ptr->previousBlock != NULL) 322 { 292 323 ptr->previousBlock->nextBlock = ptr; 293 324 } … … 301 332 void p_psFree(void *vptr, const char *file, int lineno) 302 333 { 303 if (vptr == NULL) { 334 if (vptr == NULL) 335 { 304 336 return; 305 337 } … … 308 340 309 341 // Did the user ask to be informed about this deallocation? 310 if (ptr->id == p_psMemFreeID) { 342 if (ptr->id == p_psMemFreeID) 343 { 311 344 p_psMemFreeID += memFreeCallback(ptr); 312 345 } 313 346 314 if (checkMemBlock(ptr, "psFree") != 0) { 347 if (checkMemBlock(ptr, "psFree") != 0) 348 { 315 349 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 316 } else { 317 if (ptr->refCounter > 1) { 350 } 351 else 352 { 353 if (ptr->refCounter > 1) 354 { 318 355 psError(__func__,"The buffer being freed is referenced elsewhere. " 319 356 "Buffer's reference count was decremented instead."); 320 357 ptr->refCounter--; 321 358 memProblemCallback(ptr, file, lineno); 322 } else { 359 } 360 else 361 { 323 362 324 363 // cut the memBlock out of the memBlock list 325 364 pthread_mutex_lock(&memBlockListMutex); 326 if (ptr->nextBlock != NULL) { 365 if (ptr->nextBlock != NULL) 366 { 327 367 ptr->nextBlock->previousBlock = ptr->previousBlock; 328 368 } 329 if (ptr->previousBlock != NULL) { 369 if (ptr->previousBlock != NULL) 370 { 330 371 ptr->previousBlock->nextBlock = ptr->nextBlock; 331 372 } 332 if (lastMemBlockAllocated == ptr) { 373 if (lastMemBlockAllocated == ptr) 374 { 333 375 lastMemBlockAllocated = ptr->nextBlock; 334 376 } … … 356 398 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 357 399 { 358 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 400 if ( (iter->refCounter > 0) && (iter->id >= id0) ) 401 { 359 402 nleak++; 360 403 361 if (fd != NULL) { 362 if (nleak == 1) { 404 if (fd != NULL) 405 { 406 if (nleak == 1) 407 { 363 408 fprintf(fd, " %20s:line ID\n", "file"); 364 409 } … … 384 429 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 385 430 { 386 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 431 if ( (iter->refCounter > 0) && (iter->id >= id0) ) 432 { 387 433 (*arr)[j++] = iter; 388 if (j == nleak) { // found them all 434 if (j == nleak) 435 { // found them all 389 436 break; 390 437 } -
trunk/psLib/src/sysUtils/psMemory.c
r468 r469 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.1 0$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-04-20 02:5 2:48$10 * @version $Revision: 1.11 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-04-20 02:58:46 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 48 48 psMemExhaustedCallback old = memExhaustedCallback; 49 49 50 if (func != NULL) { 50 if (func != NULL) 51 { 51 52 memExhaustedCallback = func; 52 } else { 53 } 54 else 55 { 53 56 memExhaustedCallback = memExhaustedCallbackDefault; 54 57 } … … 62 65 checkMemBlock(ptr, __func__); 63 66 64 if (ptr->refCounter <= 0) { 67 if (ptr->refCounter <= 0) 68 { 65 69 psError(__func__, 66 70 "Block %ld allocated at %s:%d freed more than once at %s:%d\n", 67 71 ptr->id, ptr->file, ptr->lineno, file, lineno); 68 } else if (ptr->refCounter > 1) { 72 } 73 else if (ptr->refCounter > 1) 74 { 69 75 psError(__func__, 70 76 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n", … … 72 78 } 73 79 74 if (lineno > 0) { 80 if (lineno > 0) 81 { 75 82 psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno); 76 83 } … … 82 89 psMemProblemCallback old = memProblemCallback; 83 90 84 if (func != NULL) { 91 if (func != NULL) 92 { 85 93 memProblemCallback = func; 86 } else { 94 } 95 else 96 { 87 97 memProblemCallback = memProblemCallbackDefault; 88 98 } … … 144 154 psMemFreeCallback old = memAllocateCallback; 145 155 146 if (func != NULL) { 156 if (func != NULL) 157 { 147 158 memAllocateCallback = func; 148 } else { 159 } 160 else 161 { 149 162 memAllocateCallback = memAllocateCallbackDefault; 150 163 } … … 157 170 psMemFreeCallback old = memFreeCallback; 158 171 159 if (func != NULL) { 172 if (func != NULL) 173 { 160 174 memFreeCallback = func; 161 } else { 175 } 176 else 177 { 162 178 memFreeCallback = memFreeCallbackDefault; 163 179 } … … 188 204 // we shouldn't call such things as p_psAlloc/p_psFree here. 189 205 190 if (m == NULL) { 206 if (m == NULL) 207 { 191 208 psError(funcName,"Memory Corruption: NULL memory block found."); 192 209 return(1); 193 210 } 194 211 195 if (!ALIGNED(m)) { 212 if (!ALIGNED(m)) 213 { 196 214 psError(funcName, "psMemCheckCorruption: non-aligned memory block"); 197 215 return(1); 198 216 } 199 217 200 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) { 218 if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) 219 { 201 220 psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id); 202 221 return(1); … … 213 232 pthread_mutex_lock(&memBlockListMutex); 214 233 215 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) { 216 if (checkMemBlock(iter, __func__)) { 234 for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 235 { 236 if (checkMemBlock(iter, __func__)) 237 { 217 238 nbad++; 218 239 219 240 memProblemCallback(iter, __func__, __LINE__); 220 241 221 if (abort_on_error) { 242 if (abort_on_error) 243 { 222 244 // release the lock on the memblock list 223 245 pthread_mutex_unlock(&memBlockListMutex); … … 237 259 psMemBlock *ptr = malloc(sizeof(psMemBlock) + size); 238 260 239 if (ptr == NULL) { 261 if (ptr == NULL) 262 { 240 263 ptr = memExhaustedCallback(size); 241 if (ptr == NULL) { 264 if (ptr == NULL) 265 { 242 266 psAbort(__func__, "Failed to allocate %ld bytes at %s:%d", 243 267 size, file, lineno); … … 258 282 ptr->previousBlock = NULL; 259 283 ptr->nextBlock = lastMemBlockAllocated; 260 if (ptr->nextBlock != NULL) { 284 if (ptr->nextBlock != NULL) 285 { 261 286 ptr->nextBlock->previousBlock = ptr; 262 287 } … … 266 291 267 292 // Did the user ask to be informed about this allocation? 268 if (ptr->id == p_psMemAllocateID) { 293 if (ptr->id == p_psMemAllocateID) 294 { 269 295 p_psMemAllocateID += memAllocateCallback(ptr); 270 296 } … … 276 302 void *p_psRealloc(void *vptr, size_t size, const char *file, int lineno) 277 303 { 278 if (vptr == NULL) { 304 if (vptr == NULL) 305 { 279 306 return p_psAlloc(size, file, lineno); 280 } else { 307 } 308 else 309 { 281 310 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 282 311 … … 286 315 287 316 // the block location may have changed, so fix the linked list addresses. 288 if (ptr->nextBlock != NULL) { 317 if (ptr->nextBlock != NULL) 318 { 289 319 ptr->nextBlock->previousBlock = ptr; 290 320 } 291 if (ptr->previousBlock != NULL) { 321 if (ptr->previousBlock != NULL) 322 { 292 323 ptr->previousBlock->nextBlock = ptr; 293 324 } … … 301 332 void p_psFree(void *vptr, const char *file, int lineno) 302 333 { 303 if (vptr == NULL) { 334 if (vptr == NULL) 335 { 304 336 return; 305 337 } … … 308 340 309 341 // Did the user ask to be informed about this deallocation? 310 if (ptr->id == p_psMemFreeID) { 342 if (ptr->id == p_psMemFreeID) 343 { 311 344 p_psMemFreeID += memFreeCallback(ptr); 312 345 } 313 346 314 if (checkMemBlock(ptr, "psFree") != 0) { 347 if (checkMemBlock(ptr, "psFree") != 0) 348 { 315 349 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 316 } else { 317 if (ptr->refCounter > 1) { 350 } 351 else 352 { 353 if (ptr->refCounter > 1) 354 { 318 355 psError(__func__,"The buffer being freed is referenced elsewhere. " 319 356 "Buffer's reference count was decremented instead."); 320 357 ptr->refCounter--; 321 358 memProblemCallback(ptr, file, lineno); 322 } else { 359 } 360 else 361 { 323 362 324 363 // cut the memBlock out of the memBlock list 325 364 pthread_mutex_lock(&memBlockListMutex); 326 if (ptr->nextBlock != NULL) { 365 if (ptr->nextBlock != NULL) 366 { 327 367 ptr->nextBlock->previousBlock = ptr->previousBlock; 328 368 } 329 if (ptr->previousBlock != NULL) { 369 if (ptr->previousBlock != NULL) 370 { 330 371 ptr->previousBlock->nextBlock = ptr->nextBlock; 331 372 } 332 if (lastMemBlockAllocated == ptr) { 373 if (lastMemBlockAllocated == ptr) 374 { 333 375 lastMemBlockAllocated = ptr->nextBlock; 334 376 } … … 356 398 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 357 399 { 358 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 400 if ( (iter->refCounter > 0) && (iter->id >= id0) ) 401 { 359 402 nleak++; 360 403 361 if (fd != NULL) { 362 if (nleak == 1) { 404 if (fd != NULL) 405 { 406 if (nleak == 1) 407 { 363 408 fprintf(fd, " %20s:line ID\n", "file"); 364 409 } … … 384 429 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) 385 430 { 386 if ( (iter->refCounter > 0) && (iter->id >= id0) ) { 431 if ( (iter->refCounter > 0) && (iter->id >= id0) ) 432 { 387 433 (*arr)[j++] = iter; 388 if (j == nleak) { // found them all 434 if (j == nleak) 435 { // found them all 389 436 break; 390 437 }
Note:
See TracChangeset
for help on using the changeset viewer.
