- Timestamp:
- Mar 5, 2012, 5:19:48 PM (14 years ago)
- Location:
- branches/meh_branches/ppstack_test
- Files:
-
- 2 edited
-
. (modified) (1 prop)
-
psLib/src/sys/psMemory.c (modified) (18 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/meh_branches/ppstack_test
- Property svn:mergeinfo changed
-
branches/meh_branches/ppstack_test/psLib/src/sys/psMemory.c
r31660 r33415 56 56 57 57 # define USE_SPINLOCK 0 58 # define USE_HARDLOCK 058 # define USE_HARDLOCK 1 59 59 60 60 # if (USE_SPINLOCK) … … 100 100 PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); \ 101 101 } 102 103 static bool checkingForCorruption = false; 102 104 103 105 static bool isBadMemBlock(FILE *output, const psMemBlock *memBlock, const char *file, unsigned int lineo, const char *func); … … 337 339 /**** Unique ID for allocated blocks and associated accessor functions 338 340 */ 339 static psMemId memid = 0;341 static volatile psMemId memid = 0; 340 342 341 343 /* Return memory ID counter for next block to be allocated … … 384 386 // pointer to the last mem block that was allocated. 385 387 // This is the root of the entire memory list 386 static psMemBlock *lastMemBlockAllocated = NULL;388 static volatile psMemBlock *lastMemBlockAllocated = NULL; 387 389 388 390 // set lock on lastMemBlockAllocated … … 515 517 continue; 516 518 } 517 if (memBlock->nextBlock && memBlock->nextBlock->inFlight) { 518 // nope, we need to try again 519 MUTEX_UNLOCK(&memBlockListMutex); 520 usleep (BLOCK_SLEEP); 521 retryLock ++; 522 continue; 523 } 524 if (memBlock->previousBlock && memBlock->previousBlock->inFlight) { 525 // nope, we need to try again 526 MUTEX_UNLOCK(&memBlockListMutex); 527 usleep (BLOCK_SLEEP); 528 retryLock ++; 529 continue; 530 } 519 if (memBlock->nextBlock && memBlock->nextBlock->inFlight) { 520 // we reply on the value of 'inFlight'. we should crash if this block is corrupted 521 if (memBlock->nextBlock->startblock != P_PS_MEMMAGIC) { 522 PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); 523 } 524 if (memBlock->nextBlock->endblock != P_PS_MEMMAGIC) { 525 PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); 526 } 527 // nope, we need to try again 528 MUTEX_UNLOCK(&memBlockListMutex); 529 usleep (BLOCK_SLEEP); 530 retryLock ++; 531 continue; 532 } 533 if (memBlock->previousBlock && memBlock->previousBlock->inFlight) { 534 // we reply on the value of 'inFlight'. we should crash if this block is corrupted 535 if (memBlock->previousBlock->startblock != P_PS_MEMMAGIC) { 536 PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); 537 } 538 if (memBlock->previousBlock->endblock != P_PS_MEMMAGIC) { 539 PS_MEM_ABORT(__func__, "Unsafe to Continue\n"); 540 } 541 // nope, we need to try again 542 MUTEX_UNLOCK(&memBlockListMutex); 543 usleep (BLOCK_SLEEP); 544 retryLock ++; 545 continue; 546 } 531 547 532 548 // the markers are ours! … … 641 657 memBlock->refCounter = 1; // one user so far 642 658 643 psMemBlock *last0 = lastMemBlockAllocated; 644 int flight0 = (lastMemBlockAllocated) ? lastMemBlockAllocated->inFlight : 10; 659 // XXX these lines are test lines that potentially access invalid memory 660 // XXX psMemBlock *last0 = lastMemBlockAllocated; 661 // XXX int flight0 = (lastMemBlockAllocated) ? lastMemBlockAllocated->inFlight : 10; 645 662 646 663 // need exclusive access of the memory block list now... … … 649 666 BLOCKLAST_LOCK (); 650 667 651 psMemBlock *last1 = lastMemBlockAllocated;652 int flight1 = (lastMemBlockAllocated) ? lastMemBlockAllocated->inFlight : 10;653 654 if (false) {655 fprintf (stderr, "last 0 : %lld, last 1 : %lld, flight0: %d, flight1: %d\n", (long long int) last0, (long long int) last1, (int) flight0, (int) flight1);656 }668 // XXX psMemBlock *last1 = lastMemBlockAllocated; 669 // XXX int flight1 = (lastMemBlockAllocated) ? lastMemBlockAllocated->inFlight : 10; 670 671 // XXX if (false) { 672 // XXX fprintf (stderr, "last 0 : %lld, last 1 : %lld, flight0: %d, flight1: %d\n", (long long int) last0, (long long int) last1, (int) flight0, (int) flight1); 673 // XXX } 657 674 658 675 // increment the memory id only after we've grabbed the memBlockListMutex this value is … … 665 682 lastMemBlockAllocated->previousBlock = memBlock; 666 683 } 667 memBlock->nextBlock = lastMemBlockAllocated;684 memBlock->nextBlock = (psMemBlock *) lastMemBlockAllocated; 668 685 lastMemBlockAllocated = memBlock; 669 686 … … 731 748 } 732 749 fprintf(output, _("\n\tMemory block is corrupted; buffer underflow detected.\n")); 750 if (!checkingForCorruption) { 751 p_psMemCheckCorruption(__FILE__, __LINE__, __func__, output, false); 752 } 733 753 bad = true; 734 754 } … … 740 760 } 741 761 fprintf(output, _("\n\tMemory block is corrupted; buffer overflow detected.\n")); 762 if (!checkingForCorruption) { 763 p_psMemCheckCorruption(__FILE__, __LINE__, __func__, output, false); 764 } 742 765 bad = true; 743 766 } … … 889 912 psMemBlock ***array, 890 913 FILE * fd, 891 bool persistence) 914 bool persistence, 915 int maxDisplayedLeaksCount ///< List at most maxDisplayedLeaksCount (-1 for all) 916 ) 892 917 { 893 918 psS32 nleak = 0; 894 919 psS32 j = 0; 895 psMemBlock *topBlock = lastMemBlockAllocated;920 psMemBlock *topBlock = (psMemBlock *) lastMemBlockAllocated; 896 921 897 922 // XXX move this elsewhere? 898 fprintf (stderr, "set %d locks, cleared %d locks, retry on %d locks (memID %ld)\n", setLock, clearLock, retryLock, memid); 923 if (fd != NULL) { 924 # if (USE_HARDLOCK) 925 # else 926 fprintf (fd, "set %d locks, cleared %d locks, retry on %d locks (memID %ld)\n", setLock, clearLock, retryLock, memid); 927 # endif 928 } 899 929 900 930 // make sure that the memblock list is free of corruption before we crawl … … 915 945 for (memBlock = topBlock; memBlock->nextBlock != NULL; memBlock = memBlock->nextBlock) { } 916 946 947 int maxToDisplay = maxDisplayedLeaksCount; 948 psMemBlock *memBlockBackup = memBlock; 949 if (maxToDisplay == -1 ) { 950 for (; memBlock != NULL; memBlock = memBlock->previousBlock) { 951 if ( (memBlock->refCounter > 0) && 952 ( (persistence) || (!persistence && !memBlock->persistent) ) && 953 (memBlock->id >= id0)) { 954 nleak++; 955 } 956 } 957 maxToDisplay=nleak; 958 } 959 if (fd != NULL) { 960 fprintf(fd, "Number of leaks to display: %d\n", maxToDisplay); 961 } 962 memBlock = memBlockBackup; 963 964 nleak=0; 917 965 // iterate through the block list starting with the oldest block 918 966 for (; memBlock != NULL; memBlock = memBlock->previousBlock) { … … 924 972 925 973 // only print a max of 500 leaks (make this an argument) 926 if ( (nleak < 500) && (fd != NULL)) {974 if ( (nleak <= maxToDisplay) && (fd != NULL) ) { 927 975 if (nleak == 1) { 928 976 fprintf(fd, "# func at (file:line) ID: X Ref: X\n"); … … 1168 1216 #endif 1169 1217 1218 # if (PS_TRACE_ON) 1219 // before freeing a particular block of memory, fill the entire block with FF to poison it 1220 // this should trigger any bad reactions early. this may be costly, so on do it for 1221 // unoptimzed builds 1222 size_t totalSize = sizeof(psMemBlock) + memBlock->userMemorySize + sizeof(void *); 1223 memset ((void *) memBlock, 0xff, totalSize); 1224 # endif 1225 1170 1226 free(memBlock); 1171 1227 … … 1256 1312 MUTEX_LOCK(&memBlockListMutex); 1257 1313 1314 // this function calls 'isBadMemBlock', which may in turn call this function : avoid nested calls or we will be stuck forever 1315 checkingForCorruption = true; 1316 1258 1317 psS32 nbad = 0; // number of bad blocks 1259 for (psMemBlock *memBlock = lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) {1318 for (psMemBlock *memBlock = (psMemBlock *) lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) { 1260 1319 if (isBadMemBlock(output, memBlock, __FILE__, __LINE__, __func__)) { 1261 1320 nbad++; … … 1269 1328 } 1270 1329 1330 checkingForCorruption = true; 1331 1271 1332 // release the lock on the memblock list 1272 1333 MUTEX_UNLOCK(&memBlockListMutex); … … 1288 1349 1289 1350 // loop through the linked list of memBlocks looking for a matching pointer 1290 for (psMemBlock *memBlock = lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) {1351 for (psMemBlock *memBlock = (psMemBlock *) lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) { 1291 1352 if (memBlock == addr) { 1292 1353 // we found the memBlock … … 1339 1400 size_t alloc = 0, persist = 0; 1340 1401 size_t nalloc = 0, npersist = 0; 1341 for (psMemBlock* ptr = lastMemBlockAllocated; ptr != NULL; ptr = ptr->nextBlock) {1402 for (psMemBlock* ptr = (psMemBlock *) lastMemBlockAllocated; ptr != NULL; ptr = ptr->nextBlock) { 1342 1403 assert (ptr->refCounter > 0); 1343 1404
Note:
See TracChangeset
for help on using the changeset viewer.
