Changeset 10886 for branches/jch-memory/psLib/src/sys/psMemory.c
- Timestamp:
- Jan 2, 2007, 6:29:10 PM (19 years ago)
- File:
-
- 1 edited
-
branches/jch-memory/psLib/src/sys/psMemory.c (modified) (23 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/jch-memory/psLib/src/sys/psMemory.c
r10884 r10886 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.88.2. 1$ $Name: not supported by cvs2svn $11 * @date $Date: 2007-01-03 0 3:36:38$10 * @version $Revision: 1.88.2.2 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2007-01-03 04:29:10 $ 12 12 * 13 13 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 38 38 #include "psHistogram.h" 39 39 40 #define MUTEX_LOCK(mutexPtr) \ 41 if (safeThreads) { \ 42 pthread_mutex_lock(mutexPtr); \ 43 } 44 45 #define MUTEX_UNLOCK(mutexPtr) \ 46 if (safeThreads) { \ 47 pthread_mutex_unlock(mutexPtr); \ 48 } 49 40 50 #define P_PS_LARGE_BLOCK_SIZE 65536 // size where under, we try to recycle 41 51 … … 262 272 psMemId psMemGetLastId(void) 263 273 { 264 if (safeThreads) { 265 pthread_mutex_lock(&memIdMutex); 266 } 274 MUTEX_LOCK(&memIdMutex); 267 275 268 276 psMemId id = memid; 269 277 270 if (safeThreads) { 271 pthread_mutex_unlock(&memIdMutex); 272 } 278 MUTEX_UNLOCK(&memIdMutex); 273 279 274 280 return id; … … 281 287 282 288 // get exclusive access to the memBlock list to avoid it changing on us while we use it. 283 // pthread_mutex_lock(&memBlockListMutex);289 // MUTEX_LOCK(&memBlockListMutex); 284 290 285 291 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) { 286 if (safeThreads) { 287 pthread_mutex_unlock(&memBlockListMutex); 288 } 292 MUTEX_UNLOCK(&memBlockListMutex); 289 293 failure = checkMemBlock(iter, __func__); 290 if (safeThreads) { 291 pthread_mutex_lock(&memBlockListMutex); 292 } 294 MUTEX_LOCK(&memBlockListMutex); 293 295 if ( failure ) { 294 296 nbad++; … … 298 300 if (abort_on_error) { 299 301 // release the lock on the memblock list 300 if (safeThreads) { 301 pthread_mutex_unlock(&memBlockListMutex); 302 } 302 MUTEX_UNLOCK(&memBlockListMutex); 303 303 psAbort(__func__, "Detected memory corruption"); 304 304 return nbad; … … 308 308 309 309 // release the lock on the memblock list 310 if (safeThreads) { 311 pthread_mutex_unlock(&memBlockListMutex); 312 } 310 MUTEX_UNLOCK(&memBlockListMutex); 311 313 312 return nbad; 314 313 } … … 353 352 } 354 353 // increment the memory id safely. 355 if (safeThreads) { 356 pthread_mutex_lock(&memBlockListMutex); 357 } 354 MUTEX_LOCK(&memBlockListMutex); 358 355 *(psMemId* ) & ptr->id = ++memid; 359 if (safeThreads) { 360 pthread_mutex_unlock(&memBlockListMutex); 361 } 356 MUTEX_UNLOCK(&memBlockListMutex); 357 362 358 ptr->file = file; 363 359 ptr->freeFunc = NULL; … … 370 366 371 367 // need exclusive access of the memory block list now... 372 if (safeThreads) { 373 pthread_mutex_lock(&memBlockListMutex); 374 } 368 MUTEX_LOCK(&memBlockListMutex); 375 369 376 370 // insert the new block to the front of the memBlock linked-list … … 381 375 lastMemBlockAllocated = ptr; 382 376 383 if (safeThreads) { 384 pthread_mutex_unlock(&memBlockListMutex); 385 } 377 MUTEX_UNLOCK(&memBlockListMutex); 386 378 387 379 // Did the user ask to be informed about this allocation? … … 417 409 // Reallocate the memory 418 410 419 if (safeThreads) { 420 pthread_mutex_lock(&memBlockListMutex); 421 } 411 MUTEX_LOCK(&memBlockListMutex); 422 412 423 413 bool isBlockLast = (ptr == lastMemBlockAllocated); // Is this the last block we allocated? … … 445 435 } 446 436 447 if (safeThreads) { 448 pthread_mutex_unlock(&memBlockListMutex); 449 } 437 MUTEX_UNLOCK(&memBlockListMutex); 450 438 451 439 // Did the user ask to be informed about this allocation? … … 492 480 psMemBlock* topBlock = lastMemBlockAllocated; 493 481 494 if (safeThreads) { 495 pthread_mutex_lock(&memBlockListMutex); 496 } 482 MUTEX_LOCK(&memBlockListMutex); 497 483 498 484 for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) { … … 513 499 } 514 500 515 if (safeThreads) { 516 pthread_mutex_unlock(&memBlockListMutex); 517 } 501 MUTEX_UNLOCK(&memBlockListMutex); 518 502 519 503 if (nleak == 0 || array == NULL) { … … 522 506 523 507 *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__); 524 if (safeThreads) { 525 pthread_mutex_lock(&memBlockListMutex); 526 } 508 MUTEX_LOCK(&memBlockListMutex); 527 509 528 510 for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) { … … 538 520 } 539 521 540 if (safeThreads) { 541 pthread_mutex_unlock(&memBlockListMutex); 542 } 522 MUTEX_UNLOCK(&memBlockListMutex); 543 523 544 524 return nleak; … … 565 545 } 566 546 567 if (safeThreads) { 568 pthread_mutex_lock(&ptr2->refCounterMutex); 569 } 547 MUTEX_LOCK(&ptr2->refCounterMutex); 570 548 refCount = ptr2->refCounter; 571 if (safeThreads) { 572 pthread_mutex_unlock(&ptr2->refCounterMutex); 573 } 549 MUTEX_UNLOCK(&ptr2->refCounterMutex); 574 550 575 551 return refCount; … … 593 569 } 594 570 595 if (safeThreads) { 596 pthread_mutex_lock(&ptr->refCounterMutex); 597 } 571 MUTEX_LOCK(&ptr->refCounterMutex); 598 572 ptr->refCounter++; 599 if (safeThreads) { 600 pthread_mutex_unlock(&ptr->refCounterMutex); 601 } 573 MUTEX_UNLOCK(&ptr->refCounterMutex); 602 574 603 575 // Did the user ask to be informed about this allocation? … … 630 602 } 631 603 632 if (safeThreads) { 633 pthread_mutex_lock(&ptr->refCounterMutex); 634 } 604 MUTEX_LOCK(&ptr->refCounterMutex); 635 605 ptr->refCounter = count; 636 if (safeThreads) { 637 pthread_mutex_unlock(&ptr->refCounterMutex); 638 } 606 MUTEX_UNLOCK(&ptr->refCounterMutex); 639 607 640 608 if (count < 1) { … … 666 634 } 667 635 668 if (safeThreads) { 669 pthread_mutex_lock(&ptr->refCounterMutex); 670 } 636 MUTEX_LOCK(&ptr->refCounterMutex); 671 637 672 638 if (ptr->refCounter > 1) { 673 639 ptr->refCounter--; // multiple references, just decrement the count. 674 if (safeThreads) { 675 pthread_mutex_unlock(&ptr->refCounterMutex); 676 } 640 MUTEX_UNLOCK(&ptr->refCounterMutex); 677 641 678 642 } else { 679 if (safeThreads) { 680 pthread_mutex_unlock(&ptr->refCounterMutex); 681 } 643 MUTEX_UNLOCK(&ptr->refCounterMutex); 682 644 683 645 if (ptr->freeFunc != NULL) { … … 685 647 } 686 648 687 if (safeThreads) { 688 pthread_mutex_lock(&memBlockListMutex); 689 } 649 MUTEX_LOCK(&memBlockListMutex); 690 650 691 651 // cut the memBlock out of the memBlock list … … 700 660 } 701 661 702 if (safeThreads) { 703 pthread_mutex_unlock(&memBlockListMutex); 704 } 662 MUTEX_UNLOCK(&memBlockListMutex); 705 663 706 664 vptr = NULL; // since we freed it, make sure we return NULL. … … 1072 1030 } 1073 1031 1074 if (safeThreads) { 1075 pthread_mutex_lock(&memBlockListMutex); 1076 } 1032 MUTEX_LOCK(&memBlockListMutex); 1077 1033 /* 1078 1034 * All memory that's currently allocated, whether persistent or not … … 1107 1063 } 1108 1064 1109 if (safeThreads) { 1110 pthread_mutex_unlock(&memBlockListMutex); 1111 } 1065 MUTEX_UNLOCK(&memBlockListMutex); 1112 1066 1113 1067 return *allocated + *persistent;
Note:
See TracChangeset
for help on using the changeset viewer.
