IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 1204


Ignore:
Timestamp:
Jul 8, 2004, 4:45:42 PM (22 years ago)
Author:
desonia
Message:

added a PS_MEM_DEBUG preprocessor directive that allows the keeping of all
psMemBlocks as to detect multiple frees.

Location:
trunk/psLib
Files:
5 edited

Legend:

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

    r1081 r1204  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-06-24 03:12:19 $
     10 *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-07-09 02:45:42 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    190190    if (m == NULL) {
    191191        psError(funcName,"Memory Corruption: NULL memory block found.");
    192         return(1);
     192        return 1;
     193    }
     194
     195    if (m->refCounter == 0) {
     196        // using an unreferenced block of memory, are you?
     197        psError(__func__,"Memory Corruption: memory block %ld was freed but still used.",
     198                m->id);
     199        return 1;
    193200    }
    194201
    195202    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
    196         psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer underflow)",
     203        psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)",
    197204                m->id);
    198         return(1);
     205        return 1;
    199206    }
    200207    if (*(void**)((int8_t*)(m+1)+m->userMemorySize) != P_PS_MEMMAGIC) {
    201         psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer overflow)",
     208        psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)",
    202209                m->id);
    203         return(1);
    204     }
    205 
    206     return(0);
     210        return 1;
     211    }
     212
     213    return 0;
    207214}
    208215
     
    292299        bool isBlockLast = false;
    293300
     301        if (checkMemBlock(ptr, __func__) != 0) {
     302            memProblemCallback(ptr, file, lineno);
     303            psAbort(file,"Realloc detected a memory corruption (id %ld @ %s:%d).",
     304                    ptr->id,ptr->file,ptr->lineno);
     305        }
     306
    294307        pthread_mutex_lock(&memBlockListMutex);
    295308
     
    331344void p_psFree(void *vptr, const char *file, int lineno)
    332345{
    333     if (vptr == NULL) {
    334         return;
    335     }
    336 
    337     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    338 
    339     if (checkMemBlock(ptr, __func__) != 0) {
    340         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    341         return;
    342     }
    343 
    344     (void)psMemDecrRefCounter(vptr);   // this handles the free, if required.
    345 }
    346 
    347 /*
    348  * Check for memory leaks. Not production quality code
     346    (void)p_psMemDecrRefCounter(vptr,file,lineno);   // this handles the free, if required.
     347}
     348
     349/*
     350 * Check for memory leaks.
    349351 */
    350352int psMemCheckLeaks(psMemoryId id0,psMemBlock ***arr,FILE *fd)
     
    419421}
    420422// increment and return refCounter
    421 void *psMemIncrRefCounter(void *vptr)
     423void* p_psMemIncrRefCounter(void *vptr, const char *file, int lineno)
    422424{
    423425    psMemBlock *ptr;
     
    430432
    431433    if (checkMemBlock(ptr, __func__)) {
    432         memProblemCallback(ptr, __func__, __LINE__);
     434        memProblemCallback(ptr, file,lineno);
    433435    }
    434436
     
    441443
    442444// decrement and return refCounter
    443 void *psMemDecrRefCounter(void *vptr)
     445void* p_psMemDecrRefCounter(void *vptr,const char *file, int lineno)
    444446{
    445447    if (vptr == NULL) {
     
    449451    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    450452
     453    if (checkMemBlock(ptr, __func__) != 0) {
     454        memProblemCallback(ptr, file, lineno);
     455        return NULL;
     456    }
    451457
    452458    pthread_mutex_lock(&ptr->refCounterMutex);
     
    469475        }
    470476
     477        #ifdef PS_MEM_DEBUG
     478        // for debug mode, we should keep the memBlock around and mark as not referenced.
     479        ptr->refCounter = 0;
     480        (void)p_psRealloc(vptr,0,file,lineno);
     481        #else
     482
    471483        pthread_mutex_lock(&memBlockListMutex);
    472484
     
    487499
    488500        free(ptr);
     501        #endif
    489502
    490503        vptr = NULL;    // since we freed it, make sure we return NULL.
     
    502515    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    503516
     517    if (checkMemBlock(ptr, __func__) != 0) {
     518        memProblemCallback(ptr, __func__, __LINE__);
     519    }
     520
    504521    ptr->freeFcn = freeFcn;
    505522
     
    513530    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    514531
     532    if (checkMemBlock(ptr, __func__) != 0) {
     533        memProblemCallback(ptr, __func__, __LINE__);
     534    }
     535
    515536    return ptr->freeFcn;
    516537}
  • trunk/psLib/src/sys/psMemory.h

    r1073 r1204  
    1414 *  @ingroup MemoryManagement
    1515 *
    16  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-06-23 23:00:15 $
     16 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-07-09 02:45:42 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    223223 *  @ingroup memRefCount
    224224 */
    225 void *psMemIncrRefCounter(
    226     void *vptr                      ///< Pointer to increment refCounter, and return
    227 );
     225#ifdef DOXYGEN
     226void* psMemIncrRefCounter(
     227    void *vptr                         ///< Pointer to increment refCounter, and return
     228);
     229#else
     230void* p_psMemIncrRefCounter(
     231    void *vptr,                        ///< Pointer to increment refCounter, and return
     232    const char *file,                  ///< File of call
     233    int lineno                         ///< Line number of call
     234);
     235#define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__)
     236#endif
    228237
    229238/** Decrement reference counter and return the pointer
     
    231240 *  @ingroup memRefCount
    232241 */
    233 void *psMemDecrRefCounter(
    234     void *vptr                      ///< Pointer to decrement refCounter, and return
    235 );
     242#ifdef DOXYGEN
     243void* psMemDecrRefCounter(
     244    void *vptr                         ///< Pointer to decrement refCounter, and return
     245);
     246#else
     247void* p_psMemDecrRefCounter(
     248    void *vptr,                        ///< Pointer to decrement refCounter, and return
     249    const char *file,                  ///< File of call
     250    int lineno                         ///< Line number of call
     251);
     252#define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__)
     253#endif
    236254
    237255/** Set callback for problems
  • trunk/psLib/src/sysUtils/psMemory.c

    r1081 r1204  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.26 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-06-24 03:12:19 $
     10 *  @version $Revision: 1.27 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-07-09 02:45:42 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    190190    if (m == NULL) {
    191191        psError(funcName,"Memory Corruption: NULL memory block found.");
    192         return(1);
     192        return 1;
     193    }
     194
     195    if (m->refCounter == 0) {
     196        // using an unreferenced block of memory, are you?
     197        psError(__func__,"Memory Corruption: memory block %ld was freed but still used.",
     198                m->id);
     199        return 1;
    193200    }
    194201
    195202    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
    196         psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer underflow)",
     203        psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)",
    197204                m->id);
    198         return(1);
     205        return 1;
    199206    }
    200207    if (*(void**)((int8_t*)(m+1)+m->userMemorySize) != P_PS_MEMMAGIC) {
    201         psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted (buffer overflow)",
     208        psError(funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)",
    202209                m->id);
    203         return(1);
    204     }
    205 
    206     return(0);
     210        return 1;
     211    }
     212
     213    return 0;
    207214}
    208215
     
    292299        bool isBlockLast = false;
    293300
     301        if (checkMemBlock(ptr, __func__) != 0) {
     302            memProblemCallback(ptr, file, lineno);
     303            psAbort(file,"Realloc detected a memory corruption (id %ld @ %s:%d).",
     304                    ptr->id,ptr->file,ptr->lineno);
     305        }
     306
    294307        pthread_mutex_lock(&memBlockListMutex);
    295308
     
    331344void p_psFree(void *vptr, const char *file, int lineno)
    332345{
    333     if (vptr == NULL) {
    334         return;
    335     }
    336 
    337     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    338 
    339     if (checkMemBlock(ptr, __func__) != 0) {
    340         memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    341         return;
    342     }
    343 
    344     (void)psMemDecrRefCounter(vptr);   // this handles the free, if required.
    345 }
    346 
    347 /*
    348  * Check for memory leaks. Not production quality code
     346    (void)p_psMemDecrRefCounter(vptr,file,lineno);   // this handles the free, if required.
     347}
     348
     349/*
     350 * Check for memory leaks.
    349351 */
    350352int psMemCheckLeaks(psMemoryId id0,psMemBlock ***arr,FILE *fd)
     
    419421}
    420422// increment and return refCounter
    421 void *psMemIncrRefCounter(void *vptr)
     423void* p_psMemIncrRefCounter(void *vptr, const char *file, int lineno)
    422424{
    423425    psMemBlock *ptr;
     
    430432
    431433    if (checkMemBlock(ptr, __func__)) {
    432         memProblemCallback(ptr, __func__, __LINE__);
     434        memProblemCallback(ptr, file,lineno);
    433435    }
    434436
     
    441443
    442444// decrement and return refCounter
    443 void *psMemDecrRefCounter(void *vptr)
     445void* p_psMemDecrRefCounter(void *vptr,const char *file, int lineno)
    444446{
    445447    if (vptr == NULL) {
     
    449451    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    450452
     453    if (checkMemBlock(ptr, __func__) != 0) {
     454        memProblemCallback(ptr, file, lineno);
     455        return NULL;
     456    }
    451457
    452458    pthread_mutex_lock(&ptr->refCounterMutex);
     
    469475        }
    470476
     477        #ifdef PS_MEM_DEBUG
     478        // for debug mode, we should keep the memBlock around and mark as not referenced.
     479        ptr->refCounter = 0;
     480        (void)p_psRealloc(vptr,0,file,lineno);
     481        #else
     482
    471483        pthread_mutex_lock(&memBlockListMutex);
    472484
     
    487499
    488500        free(ptr);
     501        #endif
    489502
    490503        vptr = NULL;    // since we freed it, make sure we return NULL.
     
    502515    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    503516
     517    if (checkMemBlock(ptr, __func__) != 0) {
     518        memProblemCallback(ptr, __func__, __LINE__);
     519    }
     520
    504521    ptr->freeFcn = freeFcn;
    505522
     
    513530    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    514531
     532    if (checkMemBlock(ptr, __func__) != 0) {
     533        memProblemCallback(ptr, __func__, __LINE__);
     534    }
     535
    515536    return ptr->freeFcn;
    516537}
  • trunk/psLib/src/sysUtils/psMemory.h

    r1073 r1204  
    1414 *  @ingroup MemoryManagement
    1515 *
    16  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-06-23 23:00:15 $
     16 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-07-09 02:45:42 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    223223 *  @ingroup memRefCount
    224224 */
    225 void *psMemIncrRefCounter(
    226     void *vptr                      ///< Pointer to increment refCounter, and return
    227 );
     225#ifdef DOXYGEN
     226void* psMemIncrRefCounter(
     227    void *vptr                         ///< Pointer to increment refCounter, and return
     228);
     229#else
     230void* p_psMemIncrRefCounter(
     231    void *vptr,                        ///< Pointer to increment refCounter, and return
     232    const char *file,                  ///< File of call
     233    int lineno                         ///< Line number of call
     234);
     235#define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__)
     236#endif
    228237
    229238/** Decrement reference counter and return the pointer
     
    231240 *  @ingroup memRefCount
    232241 */
    233 void *psMemDecrRefCounter(
    234     void *vptr                      ///< Pointer to decrement refCounter, and return
    235 );
     242#ifdef DOXYGEN
     243void* psMemDecrRefCounter(
     244    void *vptr                         ///< Pointer to decrement refCounter, and return
     245);
     246#else
     247void* p_psMemDecrRefCounter(
     248    void *vptr,                        ///< Pointer to decrement refCounter, and return
     249    const char *file,                  ///< File of call
     250    int lineno                         ///< Line number of call
     251);
     252#define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__)
     253#endif
    236254
    237255/** Set callback for problems
  • trunk/psLib/test/sysUtils/verified/tst_psMemory.stderr

    r1198 r1204  
    9191 <DATE> <TIME> |<HOST>|I|TPmemCorruption|psMemCheckCorruption shall detect memory corruptions
    9292 <DATE> <TIME> |<HOST>|I|TPmemCorruption|psMemCheckCorruption should output an error message and memProblemCallback callback should be called.
    93  <DATE> <TIME> |<HOST>|E|psMemCheckCorru|psMemCheckCorruption: memory block 1 is corrupted (buffer underflow)
    94  <DATE> <TIME> |<HOST>|I|memProblemCallb|memory callback called for id 1 (psMemCheckCorruption:220).
     93 <DATE> <TIME> |<HOST>|E|psMemCheckCorru|Memory Corruption: memory block 1 is corrupted (buffer underflow)
     94 <DATE> <TIME> |<HOST>|I|memProblemCallb|memory callback called for id 1 (psMemCheckCorruption:227).
    9595
    9696---> TESTPOINT PASSED (psMemory{psMemCorruption} | tst_psMemory.c)
Note: See TracChangeset for help on using the changeset viewer.