Changeset 1073 for trunk/psLib/src/sysUtils/psMemory.c
- Timestamp:
- Jun 23, 2004, 1:00:17 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sysUtils/psMemory.c (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sysUtils/psMemory.c
r876 r1073 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.2 4$ $Name: not supported by cvs2svn $11 * @date $Date: 2004-06- 04 23:46:48$10 * @version $Revision: 1.25 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-06-23 23:00:15 $ 12 12 * 13 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 30 30 static int checkMemBlock(const psMemBlock *m, const char* funcName); 31 31 static psMemBlock *lastMemBlockAllocated = NULL; 32 pthread_mutex_tmemBlockListMutex = PTHREAD_MUTEX_INITIALIZER;33 pthread_mutex_tmemIdMutex = PTHREAD_MUTEX_INITIALIZER;32 static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 33 static pthread_mutex_t memIdMutex = PTHREAD_MUTEX_INITIALIZER; 34 34 35 35 /** … … 96 96 psMemoryId p_psMemFreeID = 0; // notify user this block is freed 97 97 98 psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id98 psMemoryId psMemAllocateCallbackSetID(psMemoryId id) 99 99 { 100 100 psMemoryId old = p_psMemAllocateID; … … 104 104 } 105 105 106 psMemoryId psMemFreeCallbackSetID(psMemoryId id) // set p_psMemFreeID to id106 psMemoryId psMemFreeCallbackSetID(psMemoryId id) 107 107 { 108 108 psMemoryId old = p_psMemFreeID; … … 184 184 #define ALIGNED(P) ((void *)((long)(P) & ~03) == (P)) 185 185 186 static int 187 checkMemBlock(const psMemBlock *m, 188 const char* funcName) 186 static int checkMemBlock(const psMemBlock *m, const char* funcName) 189 187 { 190 188 // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked, … … 260 258 261 259 ptr->file = file; 260 ptr->freeFcn = NULL; 262 261 *(unsigned int*)&ptr->lineno = lineno; 263 262 ptr->startblock = P_PS_MEMMAGIC; … … 346 345 if (checkMemBlock(ptr, __func__) != 0) { 347 346 memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it 348 } 349 350 psMemDecrRefCounter(vptr); // this handles the free, if required. 347 return; 348 } 349 350 (void)psMemDecrRefCounter(vptr); // this handles the free, if required. 351 351 } 352 352 … … 354 354 * Check for memory leaks. Not production quality code 355 355 */ 356 int psMemCheckLeaks( 357 psMemoryId id0, // don't list blocks with id < id0 358 psMemBlock ***arr, // pointer to array of pointers to leaked blocks, or NULL 359 FILE *fd) // print list of leaks to fd (or NULL) 356 int psMemCheckLeaks(psMemoryId id0,psMemBlock ***arr,FILE *fd) 360 357 { 361 358 int nleak = 0; … … 365 362 pthread_mutex_lock(&memBlockListMutex); 366 363 367 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 368 { 364 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) { 369 365 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 370 366 nleak++; … … 382 378 pthread_mutex_unlock(&memBlockListMutex); 383 379 384 if (nleak == 0 || arr == NULL) 385 { 380 if (nleak == 0 || arr == NULL) { 386 381 return nleak; 387 382 } … … 390 385 pthread_mutex_lock(&memBlockListMutex); 391 386 392 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) 393 { 387 for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) { 394 388 if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) { 395 389 (*arr)[j++] = iter; … … 408 402 * Reference counting APIs 409 403 */ 410 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter 404 // return refCounter 405 psReferenceCount psMemGetRefCounter(void *vptr) 411 406 { 412 407 psMemBlock *ptr; 413 408 unsigned int refCount; 414 409 415 if (vptr == NULL) 416 { 410 if (vptr == NULL) { 417 411 return 0; 418 412 } … … 420 414 ptr = ((psMemBlock *)vptr) - 1; 421 415 422 if (checkMemBlock(ptr, __func__) != 0) 423 { 416 if (checkMemBlock(ptr, __func__) != 0) { 424 417 memProblemCallback(ptr, __func__, __LINE__); 425 418 } … … 431 424 return refCount; 432 425 } 433 434 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter426 // increment and return refCounter 427 void *psMemIncrRefCounter(void *vptr) 435 428 { 436 429 psMemBlock *ptr; 437 430 438 if (vptr == NULL) 439 { 431 if (vptr == NULL) { 440 432 return vptr; 441 433 } … … 443 435 ptr = ((psMemBlock *)vptr) - 1; 444 436 445 if (checkMemBlock(ptr, __func__)) 446 { 437 if (checkMemBlock(ptr, __func__)) { 447 438 memProblemCallback(ptr, __func__, __LINE__); 448 439 } … … 455 446 } 456 447 457 void *psMemDecrRefCounter(void *vptr)// decrement and return refCounter458 { 459 if (vptr == NULL) 460 {448 // decrement and return refCounter 449 void *psMemDecrRefCounter(void *vptr) 450 { 451 if (vptr == NULL) { 461 452 return NULL; 462 453 } … … 467 458 pthread_mutex_lock(&ptr->refCounterMutex); 468 459 469 if (ptr->refCounter > 1) 470 { 460 if (ptr->refCounter > 1) { 471 461 /// XXX - Probably should have another mutex here. 472 462 ptr->refCounter--; // multiple references, just decrement the count. 473 463 pthread_mutex_unlock(&ptr->refCounterMutex); 474 464 475 } else 476 { 465 } else { 477 466 pthread_mutex_unlock(&ptr->refCounterMutex); 478 467 … … 482 471 } 483 472 473 if (ptr->freeFcn != NULL) { 474 ptr->freeFcn(vptr); 475 } 476 484 477 pthread_mutex_lock(&memBlockListMutex); 485 478 … … 507 500 } 508 501 509 void p_psCustomFree(psFreeFcn fcn, void* ptr) 510 { 511 512 if (fcn == NULL) { 502 void p_psMemSetDeallocator(void* vptr, psFreeFcn freeFcn) 503 { 504 if (vptr == NULL) { 513 505 return; 514 } else { 515 if (fcn == PS_FREE) { 516 psFree(ptr); 517 } else { 518 fcn(ptr); 519 } 520 } 521 } 506 } 507 508 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 509 510 ptr->freeFcn = freeFcn; 511 512 } 513 psFreeFcn p_psMemGetDeallocator(void* vptr) 514 { 515 if (vptr == NULL) { 516 return NULL; 517 } 518 519 psMemBlock *ptr = ((psMemBlock *)vptr) - 1; 520 521 return ptr->freeFcn; 522 }
Note:
See TracChangeset
for help on using the changeset viewer.
