IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10903


Ignore:
Timestamp:
Jan 4, 2007, 12:34:06 PM (19 years ago)
Author:
jhoblitt
Message:

add HANDLE_BAD_BLOCK macro
replace all use of PS_MEM_ABORT_CORRUPT macro with HANDLE_BAD_BLOCK
remove PS_MEM_ABORT_CORRUPT macro
change badMemBlock() to accept an output parameter

Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/jch-memory/psLib/src/sys/psMemory.c

    r10902 r10903  
    99*  @author Joshua Hoblitt, University of Hawaii
    1010*
    11 *  @version $Revision: 1.88.2.12 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2007-01-04 21:55:39 $
     11*  @version $Revision: 1.88.2.13 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2007-01-04 22:34:06 $
    1313*
    1414*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    7272fprintf(stderr, "\n");
    7373
    74 #define PS_MEM_ABORT_CORRUPT(ptr) \
    75 PS_MEM_ABORT(file, "Memory corruption detected in block %lu, allocated at %s (%s:%d) by thread id %lu", \
    76              (unsigned long)ptr->id, \
    77              ptr->func, \
    78              ptr->file, \
    79              ptr->lineno,\
    80              ptr->tid);
    81 
    82 static bool badMemBlock(const psMemBlock *memBlock);
     74#define HANDLE_BAD_BLOCK(memBlock) \
     75if (badMemBlock(stderr, memBlock)) { \
     76    psMemBlockPrint(stderr, memBlock); \
     77    PS_MEM_ABORT(__func__, "Unsafe to Continue"); \
     78}
     79
     80static bool badMemBlock(FILE *output, const psMemBlock *memBlock);
    8381
    8482// pointer to the last mem block that was allocated
     
    151149 * N.b. If the block wasn't allocated by psAlloc, it will appear corrupted
    152150 */
    153 static bool badMemBlock(const psMemBlock *memBlock)
     151static bool badMemBlock(FILE *output, const psMemBlock *memBlock)
    154152{
    155153    // n.b. since this is called by psMemCheckCorruption while the memblock
     
    160158    // as they make be changed out from underneath us by new memory allocation
    161159    if (memBlock == NULL) {
    162         PS_MEM_ERROR(PS_ERR_MEMORY_CORRUPTION, true,
    163                      _("NULL memory block found."));
     160        fprintf(output, _("NULL memory block found."));
    164161        return true;
    165162    }
     
    167164    if (memBlock->refCounter == 0) {
    168165        // using an unreferenced block of memory, are you?
    169         PS_MEM_ERROR(PS_ERR_MEMORY_CORRUPTION, true,
    170                      _("Memory block %lu was freed but still being used."),
    171                      (unsigned long)memBlock->id);
     166        fprintf(output, _("Memory block was freed but still being used."));
    172167        return true;
    173168    }
    174169
    175170    if (memBlock->startblock != P_PS_MEMMAGIC || memBlock->endblock != P_PS_MEMMAGIC) {
    176         PS_MEM_ERROR(PS_ERR_MEMORY_CORRUPTION, true,
    177                      _("Memory block %lu is corrupted; buffer underflow detected."),
    178                      (unsigned long)memBlock->id);
     171        fprintf(output, _("Memory block is corrupted; buffer underflow detected."));
    179172        return true;
    180173    }
    181174    if (*(psPtr *)((int8_t *) (memBlock + 1) + memBlock->userMemorySize) != P_PS_MEMMAGIC) {
    182         PS_MEM_ERROR(PS_ERR_MEMORY_CORRUPTION, true,
    183                      _("Memory block %lu is corrupted; buffer overflow detected."),
    184                      (unsigned long)memBlock->id);
     175        fprintf(output,
     176                _("Memory block is corrupted; buffer overflow detected."));
    185177        return true;
    186178    }
     
    197189{
    198190    static psMemId incr = 10; // "p_psMemAllocID += incr"
    199     bool abort_on_error = false;
    200 
    201     if (psMemCheckCorruption(stderr, abort_on_error) > 0) {
     191
     192    if (psMemCheckCorruption(stderr, false) > 0) {
    202193        fprintf(stderr, "Detected memory corruption\n"); // somewhere to set a breakpoint
    203194    }
     
    324315int psMemCheckCorruption(FILE *output, bool abort_on_error)
    325316{
    326     psS32 nbad = 0;               // number of bad blocks
    327 
    328317    // get exclusive access to the memBlock list to avoid it changing on us
    329318    // while we use it.
    330319    MUTEX_LOCK(&memBlockListMutex);
    331320
     321    psS32 nbad = 0;               // number of bad blocks
    332322    for (psMemBlock *memBlock = lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) {
    333         if (badMemBlock(memBlock)) {
     323        if (badMemBlock(output, memBlock)) {
    334324            nbad++;
    335325
    336             fprintf(output,
    337                     "Memory corruption detected in memBlock %lu\n"
    338                     "\tSize %zd\n"
    339                     "\tPosts: %p %p %p\n"
    340                     "\tAllocated at %s (%s:%d) by thread %lu\n",
    341                     memBlock->id,
    342                     memBlock->userMemorySize,
    343                     memBlock->startblock, memBlock->endblock, (memBlock + 1 + memBlock->userMemorySize),
    344                     memBlock->func, memBlock->file, memBlock->lineno, memBlock->tid);
     326            psMemBlockPrint(output, memBlock);
    345327
    346328            if (abort_on_error) {
     
    461443    psMemBlock *memBlock = ((psMemBlock *)ptr) - 1;
    462444
    463     if (badMemBlock(memBlock)) {
    464         PS_MEM_ABORT(file, "Memory corruption detected in block %lu, allocated at %s (%s:%d) by thread id %lu",
    465                      (unsigned long)memBlock->id,
    466                      memBlock->func,
    467                      memBlock->file,
    468                      memBlock->lineno,
    469                      memBlock->tid);
    470     }
     445    HANDLE_BAD_BLOCK(memBlock);
    471446
    472447    if (size == memBlock->userMemorySize) {
     
    483458        if (memBlock == NULL) {
    484459            MUTEX_UNLOCK(&memBlockListMutex);
    485             PS_MEM_ABORT(__func__, "Failed to reallocate %zd bytes at %s (%s:%d)", size, func, file, lineno);
     460            fprintf(stderr, "Problem reallocating block\n");
     461            psMemBlockPrint(stderr,  ((psMemBlock *)ptr) - 1);
     462            PS_MEM_ABORT(__func__, "Failed to reallocate to %zd bytes at %s (%s:%d)", size, func, file, lineno);
    486463        }
    487464    }
     
    586563    psMemBlock *memBlock = ((psMemBlock *) ptr) - 1;
    587564
    588     if (badMemBlock(memBlock)) {
    589         PS_MEM_ABORT_CORRUPT(memBlock);
    590     }
     565    HANDLE_BAD_BLOCK(memBlock);
    591566
    592567    return memBlock->refCounter;
     
    604579    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    605580
    606     if (badMemBlock(memBlock)) {
    607         PS_MEM_ABORT_CORRUPT(memBlock);
    608     }
     581    HANDLE_BAD_BLOCK(memBlock);
    609582
    610583    memBlock->refCounter++;
     
    664637    psMemBlock *memBlock = ((psMemBlock *) ptr) - 1;
    665638
    666     if (badMemBlock(memBlock)) {
    667         PS_MEM_ABORT_CORRUPT(memBlock);
    668         return NULL;
    669     }
     639    HANDLE_BAD_BLOCK(memBlock);
    670640
    671641    if (memBlock->refCounter < 1) {
     
    732702    psMemBlock* memBlock = ((psMemBlock *)ptr) - 1;
    733703
    734     if (badMemBlock(memBlock)) {
    735         PS_MEM_ABORT_CORRUPT(memBlock);
    736     }
     704    HANDLE_BAD_BLOCK(memBlock);
    737705
    738706    memBlock->freeFunc = freeFunc;
     
    747715    psMemBlock* memBlock = ((psMemBlock *)ptr) - 1;
    748716
    749     if (badMemBlock(memBlock)) {
    750         PS_MEM_ABORT_CORRUPT(memBlock);
    751     }
     717    HANDLE_BAD_BLOCK(memBlock);
    752718
    753719    return memBlock->freeFunc;
     
    1025991    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    1026992
    1027     if (badMemBlock(memBlock)) {
    1028         PS_MEM_ABORT_CORRUPT(memBlock);
    1029     }
     993    HANDLE_BAD_BLOCK(memBlock);
    1030994
    1031995    return memBlock->persistent;
     
    10411005    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    10421006
    1043     if (badMemBlock(memBlock)) {
    1044         PS_MEM_ABORT_CORRUPT(memBlock);
    1045     }
     1007    HANDLE_BAD_BLOCK(memBlock);
    10461008
    10471009    memBlock->persistent = value;
     
    11101072                   "\tPrevious Block: %p Next Block: %p\n"
    11111073                   "\tFree function: %p\n"
    1112                    "\tSize: %zd Reference count: %lu\n Persistent: %s\n"
     1074                   "\tSize: %zd Reference count: %lu Persistent: %s\n"
    11131075                   "\tPosts: %p %p %p\n"
    1114                    "\tAllocated at %s (%s:%d) by thread %lu\n",
     1076                   "\tAllocated in %s at (%s:%d)\n"
     1077                   "\t\tby Thread ID %lu\n",
    11151078                   memBlock->id,
    11161079                   memBlock->previousBlock, memBlock->nextBlock,
  • trunk/psLib/test/sys/tap_psMemory.c

    r10810 r10903  
    66*  @author Robert DeSonia, MHPCC
    77*
    8 *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    9 *  @date $Date: 2006-12-18 19:18:46 $
     8*  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     9*  @date $Date: 2007-01-04 22:34:06 $
    1010*
    1111*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    6161    TPcheckLeaks();
    6262
     63    // psMemProblemCallbackSet() has been removed -JH
     64    #if 0
     65
    6366    void TPmemCorruption( void );
    6467    TPmemCorruption();
     68    #endif
    6569
    6670    void TPmultipleFree( void );
     
    225229    }
    226230
    227     psMemCheckCorruption( 1 );
     231    psMemCheckCorruption(stderr, false);
    228232
    229233    // realloc to 2x
     
    242246    ok(error==0, "Realloc preserve the contents with expanding buffer");
    243247
    244     psMemCheckCorruption( 1 );
     248    psMemCheckCorruption(stderr, false);
    245249
    246250    // realloc to 1/2 initial value.
     
    403407
    404408
     409#if 0
    405410void TPmemCorruption( void )
    406411{
     
    443448    skip_end();
    444449}
     450#endif
    445451
    446452
Note: See TracChangeset for help on using the changeset viewer.