IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 19, 2004, 10:19:22 AM (22 years ago)
Author:
desonia
Message:

ran astyle on the files.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psMemory.c

    r451 r452  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-19 20:14:04 $
     10 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-04-19 20:19:22 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4949
    5050    if (func != NULL) {
    51             memExhaustedCallback = func;
    52         } else {
    53             memExhaustedCallback = memExhaustedCallbackDefault;
    54         }
     51        memExhaustedCallback = func;
     52    } else {
     53        memExhaustedCallback = memExhaustedCallbackDefault;
     54    }
    5555
    5656    return old;
     
    5858
    5959static void memProblemCallbackDefault(const psMemBlock *ptr,
    60                           const char *file, int lineno)
     60                                      const char *file, int lineno)
    6161{
    6262    checkMemBlock(ptr, __func__);
    6363
    64         if (ptr->refCounter <= 0) {
    65                 psError(__func__,
    66                         "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    67                                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    68         } else if (ptr->refCounter > 1) {
    69                 psError(__func__,
    70                         "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
    71                                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    72         }
    73 
    74         if (lineno > 0) {
     64    if (ptr->refCounter <= 0) {
     65        psError(__func__,
     66                "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
     67                ptr->id, ptr->file, ptr->lineno, file, lineno);
     68    } else if (ptr->refCounter > 1) {
     69        psError(__func__,
     70                "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
     71                ptr->id, ptr->file, ptr->lineno, file, lineno);
     72    }
     73
     74    if (lineno > 0) {
    7575        psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno);
    7676    }
     
    8383
    8484    if (func != NULL) {
    85             memProblemCallback = func;
    86         } else {
    87             memProblemCallback = memProblemCallbackDefault;
    88         }
     85        memProblemCallback = func;
     86    } else {
     87        memProblemCallback = memProblemCallbackDefault;
     88    }
    8989
    9090    return old;
     
    145145
    146146    if (func != NULL) {
    147             memAllocateCallback =  func;
    148         } else {
    149             memAllocateCallback = memAllocateCallbackDefault;
    150         }
     147        memAllocateCallback =  func;
     148    } else {
     149        memAllocateCallback = memAllocateCallbackDefault;
     150    }
    151151
    152152    return old;
     
    158158
    159159    if (func != NULL) {
    160             memFreeCallback = func;
    161         } else {
    162             memFreeCallback = memFreeCallbackDefault;
    163         }
     160        memFreeCallback = func;
     161    } else {
     162        memFreeCallback = memFreeCallbackDefault;
     163    }
    164164
    165165    return old;
     
    185185static int
    186186checkMemBlock(const psMemBlock *m,
    187              const char* funcName)
     187              const char* funcName)
    188188{
    189189    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
     
    212212    int nbad = 0;                       // number of bad blocks
    213213
    214         // get exclusive access to the memBlock list to avoid it changing on us while we use it.
    215         pthread_mutex_lock(&memBlockListMutex);
    216 
    217     for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock)
    218 {
     214    // get exclusive access to the memBlock list to avoid it changing on us while we use it.
     215    pthread_mutex_lock(&memBlockListMutex);
     216
     217    for (psMemBlock* iter=lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
    219218        if (checkMemBlock(iter, __func__)) {
    220                         nbad++;
    221 
    222                         memProblemCallback(iter, __func__, __LINE__);
    223 
    224                         if (abort_on_error) {
    225                                 // release the lock on the memblock list
    226                 pthread_mutex_unlock(&memBlockListMutex);
    227                                 psAbort(__func__, "Detected memory corruption");
    228                                 return nbad;
    229                         }
    230         }
    231     }
    232 
    233         // release the lock on the memblock list
    234         pthread_mutex_unlock(&memBlockListMutex);
     219            nbad++;
     220
     221            memProblemCallback(iter, __func__, __LINE__);
     222
     223            if (abort_on_error) {
     224                // release the lock on the memblock list
     225                pthread_mutex_unlock(&memBlockListMutex);
     226                psAbort(__func__, "Detected memory corruption");
     227                return nbad;
     228            }
     229        }
     230    }
     231
     232    // release the lock on the memblock list
     233    pthread_mutex_unlock(&memBlockListMutex);
    235234    return nbad;
    236235}
     
    254253    *(unsigned int*)&ptr->lineno = lineno;
    255254    ptr->startblock = P_PS_MEMMAGIC;
    256         ptr->endblock = P_PS_MEMMAGIC;
     255    ptr->endblock = P_PS_MEMMAGIC;
    257256    ptr->refCounter = 1;                // one user so far
    258257
    259         // need exclusive access of the memory block list now...
    260         pthread_mutex_lock(&memBlockListMutex);
    261 
    262         // insert the new block to the front of the memBlock linked-list
    263         ptr->previousBlock = NULL;
    264         ptr->nextBlock = lastMemBlockAllocated;
    265         if (ptr->nextBlock != NULL) {
    266             ptr->nextBlock->previousBlock = ptr;
    267         }
    268         lastMemBlockAllocated = ptr;
    269 
    270         pthread_mutex_unlock(&memBlockListMutex);
     258    // need exclusive access of the memory block list now...
     259    pthread_mutex_lock(&memBlockListMutex);
     260
     261    // insert the new block to the front of the memBlock linked-list
     262    ptr->previousBlock = NULL;
     263    ptr->nextBlock = lastMemBlockAllocated;
     264    if (ptr->nextBlock != NULL) {
     265        ptr->nextBlock->previousBlock = ptr;
     266    }
     267    lastMemBlockAllocated = ptr;
     268
     269    pthread_mutex_unlock(&memBlockListMutex);
    271270
    272271    //  Did the user ask to be informed about this allocation?
     
    275274    }
    276275
    277         // And return the user the memory that they allocated
     276    // And return the user the memory that they allocated
    278277    return ptr + 1;   // user memory
    279278}
     
    286285        psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    287286
    288         pthread_mutex_lock(&memBlockListMutex);
    289 
    290                 ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
    291 
    292                 // the block location may have changed, so fix the linked list addresses.
    293                 if (ptr->nextBlock != NULL) {
    294                     ptr->nextBlock->previousBlock = ptr;
    295                 }
    296                 if (ptr->previousBlock != NULL) {
    297                     ptr->previousBlock->nextBlock = ptr;
    298                 }
    299 
    300             pthread_mutex_unlock(&memBlockListMutex);
    301 
    302                 return ptr + 1;   // usr memory
     287        pthread_mutex_lock(&memBlockListMutex);
     288
     289        ptr = (psMemBlock*)realloc(ptr, sizeof(psMemBlock) + size);
     290
     291        // the block location may have changed, so fix the linked list addresses.
     292        if (ptr->nextBlock != NULL) {
     293            ptr->nextBlock->previousBlock = ptr;
     294        }
     295        if (ptr->previousBlock != NULL) {
     296            ptr->previousBlock->nextBlock = ptr;
     297        }
     298
     299        pthread_mutex_unlock(&memBlockListMutex);
     300
     301        return ptr + 1;   // usr memory
    303302    }
    304303}
     
    312311    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    313312
    314         // Did the user ask to be informed about this deallocation?
     313    // Did the user ask to be informed about this deallocation?
    315314    if (ptr->id == p_psMemFreeID) {
    316315        p_psMemFreeID += memFreeCallback(ptr);
     
    321320    } else {
    322321        if (ptr->refCounter > 1) {
    323                     psError(__func__,"The buffer being freed is referenced elsewhere. "
    324                                 "Buffer's reference count was decremented instead.");
     322            psError(__func__,"The buffer being freed is referenced elsewhere. "
     323                    "Buffer's reference count was decremented instead.");
    325324            ptr->refCounter--;
    326325            memProblemCallback(ptr, file, lineno);
    327326        } else {
    328327
    329                         // cut the memBlock out of the memBlock list
    330                 pthread_mutex_lock(&memBlockListMutex);
    331                         if (ptr->nextBlock != NULL) {
    332                             ptr->nextBlock->previousBlock = ptr->previousBlock;
    333                         }
    334                         if (ptr->previousBlock != NULL) {
    335                             ptr->previousBlock->nextBlock = ptr->nextBlock;
    336                         }
    337                         if (lastMemBlockAllocated == ptr) {
    338                             lastMemBlockAllocated = ptr->nextBlock;
    339                         }
    340 
    341                 pthread_mutex_unlock(&memBlockListMutex);
    342 
    343                     free(ptr);
    344                 }
     328            // cut the memBlock out of the memBlock list
     329            pthread_mutex_lock(&memBlockListMutex);
     330            if (ptr->nextBlock != NULL) {
     331                ptr->nextBlock->previousBlock = ptr->previousBlock;
     332            }
     333            if (ptr->previousBlock != NULL) {
     334                ptr->previousBlock->nextBlock = ptr->nextBlock;
     335            }
     336            if (lastMemBlockAllocated == ptr) {
     337                lastMemBlockAllocated = ptr->nextBlock;
     338            }
     339
     340            pthread_mutex_unlock(&memBlockListMutex);
     341
     342            free(ptr);
     343        }
    345344    }
    346345}
     
    356355{
    357356    int nleak = 0;
    358         int j = 0;
    359 
    360         pthread_mutex_lock(&memBlockListMutex);
    361 
    362     for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
     357    int j = 0;
     358
     359    pthread_mutex_lock(&memBlockListMutex);
     360
     361    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock)
     362    {
    363363        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
    364                         nleak++;
    365 
    366                         if (fd != NULL) {
    367                                 if (nleak == 1) {
    368                                         fprintf(fd, "   %20s:line ID\n", "file");
    369                                 }
    370 
    371                                 fprintf(fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id);
    372                         }
    373                 }
    374     }
    375 
    376         pthread_mutex_unlock(&memBlockListMutex);
    377 
    378         if (nleak == 0 || arr == NULL) {
     364            nleak++;
     365
     366            if (fd != NULL) {
     367                if (nleak == 1) {
     368                    fprintf(fd, "   %20s:line ID\n", "file");
     369                }
     370
     371                fprintf(fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id);
     372            }
     373        }
     374    }
     375
     376    pthread_mutex_unlock(&memBlockListMutex);
     377
     378    if (nleak == 0 || arr == NULL)
     379    {
    379380        return nleak;
    380381    }
     
    384385    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
    385386
    386         pthread_mutex_lock(&memBlockListMutex);
    387 
    388     for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock) {
     387    pthread_mutex_lock(&memBlockListMutex);
     388
     389    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter=iter->nextBlock)
     390    {
    389391        if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
    390                         (*arr)[j++] = iter;
    391                         if (j == nleak) { // found them all
    392                                 break;
    393                         }
     392            (*arr)[j++] = iter;
     393            if (j == nleak) { // found them all
     394                break;
     395            }
    394396        }
    395397    }
     
    418420void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
    419421{
    420     if (vptr == NULL) {
     422    if (vptr == NULL)
     423    {
    421424        return vptr;
    422425    }
     
    424427    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    425428
    426     if (checkMemBlock(ptr, __func__)) {
     429    if (checkMemBlock(ptr, __func__))
     430    {
    427431        memProblemCallback(ptr, __func__, __LINE__);
    428432    }
     
    435439void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
    436440{
    437     if (vptr == NULL) {
     441    if (vptr == NULL)
     442    {
    438443        return vptr;
    439444    }
     
    441446    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    442447
    443     if (checkMemBlock(ptr, __func__)) {
     448    if (checkMemBlock(ptr, __func__))
     449    {
    444450        memProblemCallback(ptr, __func__, __LINE__);
    445451    }
Note: See TracChangeset for help on using the changeset viewer.