Changeset 6419
- Timestamp:
- Feb 9, 2006, 4:44:46 PM (20 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 4 edited
-
sys/psMemory.c (modified) (31 diffs)
-
sys/psMemory.h (modified) (3 diffs)
-
types/psList.c (modified) (12 diffs)
-
types/psList.h (modified) (2 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) -
trunk/psLib/src/sys/psMemory.h
r5216 r6419 12 12 * @ingroup MemoryManagement 13 13 * 14 * @version $Revision: 1.5 1$ $Name: not supported by cvs2svn $15 * @date $Date: 200 5-10-01 02:22:13$14 * @version $Revision: 1.52 $ $Name: not supported by cvs2svn $ 15 * @date $Date: 2006-02-10 02:44:46 $ 16 16 * 17 17 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 169 169 */ 170 170 psFreeFunc psMemGetDeallocator( 171 const psPtr ptr ///< the memory block171 const psPtr ptr ///< the memory block 172 172 ); 173 173 … … 181 181 ); 182 182 183 /** Activate or Deactivate thread safety and mutex locking in the memory management. 184 * 185 * psMemThreadSafety shall turn on thread safety in the memory management functions if 186 * safe is true, and deactivate all mutex locking in the memory management functions if 187 * safe is false. The function shall return the previous value of the thread safety. 188 * Note that the default behaviour of the library shall be for the locking to be performed. 189 * 190 * @return bool: The previous value of the thread safety. 191 */ 192 bool psMemThreadSafety( 193 bool safe ///< boolean for turning on/off thread safety 194 ); 183 195 184 196 /** Set the memory as persistent so that it is ignored when detecting memory leaks. -
trunk/psLib/src/types/psList.c
r5511 r6419 6 6 * @author Robert Daniel DeSonia, MHPCC 7 7 * 8 * @version $Revision: 1.4 5$ $Name: not supported by cvs2svn $9 * @date $Date: 200 5-11-14 22:18:34$8 * @version $Revision: 1.46 $ $Name: not supported by cvs2svn $ 9 * @date $Date: 2006-02-10 02:44:46 $ 10 10 * 11 11 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 33 33 static psBool listIteratorRemove(psListIterator* iterator); 34 34 35 //private boolean for enabling/disabling thread safety. Default = enabled. 36 static bool safeThreads = true; 37 35 38 static void listFree(psList* list) 36 39 { … … 39 42 } 40 43 41 pthread_mutex_lock(&list->p_lock); 44 if (safeThreads) { 45 pthread_mutex_lock(&list->p_lock); 46 // pthread_mutex_lock(&p_lock); 47 } 42 48 43 49 // remove the associated iterators -- any references are invalid once psList is freed. … … 64 70 } 65 71 66 pthread_mutex_unlock(&list->p_lock); 67 68 pthread_mutex_destroy(&list->p_lock); 69 72 if (safeThreads) { 73 pthread_mutex_unlock(&list->p_lock); 74 pthread_mutex_destroy(&list->p_lock); 75 // pthread_mutex_unlock(&p_lock); 76 // pthread_mutex_destroy(&p_lock); 77 } else { 78 safeThreads = true; 79 } 70 80 } 71 81 … … 100 110 int index = iterator->index; 101 111 102 pthread_mutex_lock(&list->p_lock); 103 112 if (safeThreads) { 113 pthread_mutex_lock(&list->p_lock); 114 // pthread_mutex_lock(&p_lock); 115 } 104 116 if (elem == list->head) { // head of list? 105 117 list->head = elem->next; … … 126 138 list->n--; 127 139 128 pthread_mutex_unlock(&list->p_lock) 129 ; 140 if (safeThreads) { 141 pthread_mutex_unlock(&list->p_lock); 142 // pthread_mutex_unlock(&p_lock); 143 } 130 144 131 145 // OK, delete orphaned list element and its data … … 150 164 psListIteratorAlloc(list,PS_LIST_HEAD,true); 151 165 152 pthread_mutex_init(&(list->p_lock), NULL); 166 if (safeThreads) { 167 pthread_mutex_init(&(list->p_lock), NULL); 168 // pthread_mutex_init(&p_lock, NULL); 169 } 153 170 154 171 if (data != NULL) { … … 164 181 } 165 182 166 183 bool p_psListThreadSafety(bool safe) 184 { 185 bool out = safeThreads; 186 safeThreads = safe; 187 return out; 188 } 167 189 168 190 psListIterator* psListIteratorAlloc(psList* list, long location, bool mutable) … … 326 348 psListElem* elem = psAlloc(sizeof(psListElem)); 327 349 328 pthread_mutex_lock(&list->p_lock); 350 if (safeThreads) { 351 pthread_mutex_lock(&list->p_lock); 352 // pthread_mutex_lock(&p_lock); 353 } 329 354 330 355 // set the new list element's attributes … … 362 387 } 363 388 364 pthread_mutex_unlock(&list->p_lock); 389 if (safeThreads) { 390 pthread_mutex_unlock(&list->p_lock); 391 // pthread_mutex_unlock(&p_lock); 392 } 365 393 366 394 return true; … … 399 427 psListElem* elem = psAlloc(sizeof(psListElem)); 400 428 401 pthread_mutex_lock(&list->p_lock); 429 if (safeThreads) { 430 pthread_mutex_lock(&list->p_lock); 431 // pthread_mutex_lock(&p_lock); 432 } 402 433 403 434 // set the new list element's attributes … … 435 466 } 436 467 437 pthread_mutex_unlock(&list->p_lock); 468 if (safeThreads) { 469 pthread_mutex_unlock(&list->p_lock); 470 // pthread_mutex_unlock(&p_lock); 471 } 438 472 439 473 return true; -
trunk/psLib/src/types/psList.h
r6348 r6419 7 7 * @ingroup LinkedList 8 8 * 9 * @version $Revision: 1.3 3$ $Name: not supported by cvs2svn $10 * @date $Date: 2006-02- 07 23:36:15$9 * @version $Revision: 1.34 $ $Name: not supported by cvs2svn $ 10 * @date $Date: 2006-02-10 02:44:46 $ 11 11 * 12 12 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 94 94 ; 95 95 96 /** Private function used by psMemThreadSafety to activate/deactivate thread safety. 97 * 98 * @return bool: The previous value of the thread safety. 99 */ 100 bool p_psListThreadSafety( 101 bool safe ///< current value of thread safety to set 102 ); 96 103 97 104 /** Creates a psList linked list object.
Note:
See TracChangeset
for help on using the changeset viewer.
