Changeset 6419 for trunk/psLib/src/sys/psMemory.c
- Timestamp:
- Feb 9, 2006, 4:44:46 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psMemory.c (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psMemory.c
r6314 r6419 8 8 * @author Robert Lupton, Princeton University 9 9 * 10 * @version $Revision: 1.6 7$ $Name: not supported by cvs2svn $11 * @date $Date: 2006-02- 03 00:11:59$10 * @version $Revision: 1.68 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2006-02-10 02:44:46 $ 12 12 * 13 13 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 58 58 static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; 59 59 60 //private boolean for enabling/disabling thread safety. Default = enabled. 61 static bool safeThreads = true; 62 60 63 static psS32 recycleBins = 13; 61 64 static psS32 recycleBinSize[14] = { … … 83 86 { 84 87 psPtr ptr = NULL; 85 86 pthread_mutex_lock(&recycleMemBlockListMutex); 88 if (safeThreads) { 89 pthread_mutex_lock(&recycleMemBlockListMutex); 90 } 87 91 psS32 level = recycleBins - 1; 88 92 … … 97 101 level--; 98 102 } 99 pthread_mutex_unlock(&recycleMemBlockListMutex); 103 104 if (safeThreads) { 105 pthread_mutex_unlock(&recycleMemBlockListMutex); 106 } 100 107 101 108 return ptr; … … 271 278 psMemId id; 272 279 273 pthread_mutex_lock(&memIdMutex); 280 if (safeThreads) { 281 pthread_mutex_lock(&memIdMutex); 282 } 283 274 284 id = memid + 1; 275 pthread_mutex_unlock(&memIdMutex); 285 286 if (safeThreads) { 287 pthread_mutex_unlock(&memIdMutex); 288 } 276 289 277 290 return id; … … 287 300 288 301 for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) { 289 pthread_mutex_unlock(&memBlockListMutex); 302 if (safeThreads) { 303 pthread_mutex_unlock(&memBlockListMutex); 304 } 290 305 failure = checkMemBlock(iter, __func__); 291 pthread_mutex_lock(&memBlockListMutex); 306 if (safeThreads) { 307 pthread_mutex_lock(&memBlockListMutex); 308 } 292 309 if ( failure ) { 293 310 nbad++; … … 297 314 if (abort_on_error) { 298 315 // release the lock on the memblock list 299 pthread_mutex_unlock(&memBlockListMutex); 316 if (safeThreads) { 317 pthread_mutex_unlock(&memBlockListMutex); 318 } 300 319 psAbort(__func__, "Detected memory corruption"); 301 320 return nbad; … … 305 324 306 325 // release the lock on the memblock list 307 pthread_mutex_unlock(&memBlockListMutex); 326 if (safeThreads) { 327 pthread_mutex_unlock(&memBlockListMutex); 328 } 308 329 return nbad; 309 330 } … … 331 352 size = recycleBinSize[level]; // round-up size to next sized bin. 332 353 333 pthread_mutex_lock(&recycleMemBlockListMutex); 354 if (safeThreads) { 355 pthread_mutex_lock(&recycleMemBlockListMutex); 356 } 334 357 335 358 if (recycleMemBlockList[level] != NULL) { … … 342 365 } 343 366 344 pthread_mutex_unlock(&recycleMemBlockListMutex); 367 if (safeThreads) { 368 pthread_mutex_unlock(&recycleMemBlockListMutex); 369 } 345 370 } 346 371 } … … 359 384 *(psPtr*)&ptr->endblock = P_PS_MEMMAGIC; 360 385 ptr->userMemorySize = size; 361 pthread_mutex_init(&ptr->refCounterMutex, NULL); 386 if (safeThreads) { 387 pthread_mutex_init(&ptr->refCounterMutex, NULL); 388 } 362 389 } 363 390 // increment the memory id safely. 364 pthread_mutex_lock(&memBlockListMutex); 391 if (safeThreads) { 392 pthread_mutex_lock(&memBlockListMutex); 393 } 365 394 *(psMemId* ) & ptr->id = ++memid; 366 pthread_mutex_unlock(&memBlockListMutex); 367 395 if (safeThreads) { 396 pthread_mutex_unlock(&memBlockListMutex); 397 } 368 398 ptr->file = file; 369 399 ptr->freeFunc = NULL; … … 376 406 377 407 // need exclusive access of the memory block list now... 378 pthread_mutex_lock(&memBlockListMutex); 408 if (safeThreads) { 409 pthread_mutex_lock(&memBlockListMutex); 410 } 379 411 380 412 // insert the new block to the front of the memBlock linked-list … … 385 417 lastMemBlockAllocated = ptr; 386 418 387 pthread_mutex_unlock(&memBlockListMutex); 419 if (safeThreads) { 420 pthread_mutex_unlock(&memBlockListMutex); 421 } 388 422 389 423 // Did the user ask to be informed about this allocation? … … 414 448 } 415 449 416 pthread_mutex_lock(&memBlockListMutex); 450 if (safeThreads) { 451 pthread_mutex_lock(&memBlockListMutex); 452 } 417 453 418 454 isBlockLast = (ptr == lastMemBlockAllocated); … … 441 477 } 442 478 443 pthread_mutex_unlock(&memBlockListMutex); 479 if (safeThreads) { 480 pthread_mutex_unlock(&memBlockListMutex); 481 } 444 482 445 483 // Did the user ask to be informed about this allocation? … … 487 525 psMemBlock* topBlock = lastMemBlockAllocated; 488 526 489 pthread_mutex_lock(&memBlockListMutex); 527 if (safeThreads) { 528 pthread_mutex_lock(&memBlockListMutex); 529 } 490 530 491 531 for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) { … … 506 546 } 507 547 508 pthread_mutex_unlock(&memBlockListMutex); 548 if (safeThreads) { 549 pthread_mutex_unlock(&memBlockListMutex); 550 } 509 551 510 552 if (nleak == 0 || array == NULL) { … … 513 555 514 556 *array = p_psAlloc(nleak * sizeof(psMemBlock), __FILE__, __LINE__); 515 pthread_mutex_lock(&memBlockListMutex); 557 if (safeThreads) { 558 pthread_mutex_lock(&memBlockListMutex); 559 } 516 560 517 561 for (psMemBlock* iter = topBlock; iter != NULL; iter = iter->nextBlock) { … … 527 571 } 528 572 529 pthread_mutex_unlock(&memBlockListMutex); 573 if (safeThreads) { 574 pthread_mutex_unlock(&memBlockListMutex); 575 } 530 576 531 577 return nleak; … … 552 598 } 553 599 554 pthread_mutex_lock(&ptr2->refCounterMutex); 600 if (safeThreads) { 601 pthread_mutex_lock(&ptr2->refCounterMutex); 602 } 555 603 refCount = ptr2->refCounter; 556 pthread_mutex_unlock(&ptr2->refCounterMutex); 604 if (safeThreads) { 605 pthread_mutex_unlock(&ptr2->refCounterMutex); 606 } 557 607 558 608 return refCount; … … 576 626 } 577 627 578 pthread_mutex_lock(&ptr->refCounterMutex); 628 if (safeThreads) { 629 pthread_mutex_lock(&ptr->refCounterMutex); 630 } 579 631 ptr->refCounter++; 580 pthread_mutex_unlock(&ptr->refCounterMutex); 632 if (safeThreads) { 633 pthread_mutex_unlock(&ptr->refCounterMutex); 634 } 581 635 582 636 // Did the user ask to be informed about this allocation? … … 588 642 } 589 643 590 psPtr p_psMemSetRefCounter( 591 psPtr vptr, 592 psReferenceCount count, 593 const char *file, 594 psS32 lineno) 644 psPtr p_psMemSetRefCounter(psPtr vptr, 645 psReferenceCount count, 646 const char *file, 647 psS32 lineno) 595 648 { 596 649 psMemBlock* ptr; … … 610 663 } 611 664 612 pthread_mutex_lock(&ptr->refCounterMutex); 665 if (safeThreads) { 666 pthread_mutex_lock(&ptr->refCounterMutex); 667 } 613 668 ptr->refCounter = count; 614 pthread_mutex_unlock(&ptr->refCounterMutex); 669 if (safeThreads) { 670 pthread_mutex_unlock(&ptr->refCounterMutex); 671 } 615 672 616 673 if (count < 1) { … … 642 699 } 643 700 644 pthread_mutex_lock(&ptr->refCounterMutex); 701 if (safeThreads) { 702 pthread_mutex_lock(&ptr->refCounterMutex); 703 } 645 704 646 705 if (ptr->refCounter > 1) { 647 706 ptr->refCounter--; // multiple references, just decrement the count. 648 pthread_mutex_unlock(&ptr->refCounterMutex); 707 if (safeThreads) { 708 pthread_mutex_unlock(&ptr->refCounterMutex); 709 } 649 710 650 711 } else { 651 pthread_mutex_unlock(&ptr->refCounterMutex); 712 if (safeThreads) { 713 pthread_mutex_unlock(&ptr->refCounterMutex); 714 } 652 715 653 716 if (ptr->freeFunc != NULL) { … … 655 718 } 656 719 657 pthread_mutex_lock(&memBlockListMutex); 720 if (safeThreads) { 721 pthread_mutex_lock(&memBlockListMutex); 722 } 658 723 659 724 // cut the memBlock out of the memBlock list … … 668 733 } 669 734 670 pthread_mutex_unlock(&memBlockListMutex); 735 if (safeThreads) { 736 pthread_mutex_unlock(&memBlockListMutex); 737 } 671 738 672 739 // do we need to recycle? … … 683 750 ptr->previousBlock = NULL; 684 751 685 pthread_mutex_lock(&recycleMemBlockListMutex); 752 if (safeThreads) { 753 pthread_mutex_lock(&recycleMemBlockListMutex); 754 } 686 755 ptr->nextBlock = recycleMemBlockList[level]; 687 756 if (recycleMemBlockList[level] != NULL) { … … 689 758 } 690 759 recycleMemBlockList[level] = ptr; 691 pthread_mutex_unlock(&recycleMemBlockListMutex); 760 if (safeThreads) { 761 pthread_mutex_unlock(&recycleMemBlockListMutex); 762 } 692 763 693 764 } else { … … 703 774 #else // #ifdef PS_MEM_DEBUG 704 775 705 pthread_mutex_destroy(&ptr->refCounterMutex); 776 if (safeThreads) { 777 pthread_mutex_destroy(&ptr->refCounterMutex); 778 } 706 779 free(ptr); 707 780 #endif // #else - #ifdef PS_MEM_DEBUG … … 731 804 732 805 } 806 733 807 psFreeFunc psMemGetDeallocator(const psPtr ptr) 734 808 { … … 998 1072 } 999 1073 1074 bool psMemThreadSafety(bool safe) 1075 { 1076 bool out = safeThreads; 1077 safeThreads = safe; 1078 if ( out != p_psListThreadSafety(safe) ) { 1079 psWarning("Thread safety setting in psMemory didn't match that of psList.\n"); 1080 } 1081 return out; 1082 } 1000 1083 1001 1084 bool p_psMemGetPersistent(psPtr vptr)
Note:
See TracChangeset
for help on using the changeset viewer.
