IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 10906


Ignore:
Timestamp:
Jan 4, 2007, 3:15:33 PM (19 years ago)
Author:
jhoblitt
Message:

change badMemBlock() to accept file, lineno, & func
rename psMemGetRefCounter() -> p_psMemGetRefCounter()
rename psMemGetDeallocator() -> p_psMemGetDeallocator()
rename psMemSetDeallocator() -> p_psMemSetDeallocator()
add psMemGetRefCounter, psMemGetDeallocator, & psMemSetDeallocator wrapper macros
rename some function params to be more consistent with each other
remove psMemProblemCallbackSet() prototype
add file, lineno, & func to most public functions
remove redundant double free check from p_psMemDecrRefCounter

Location:
branches/jch-memory/psLib/src/sys
Files:
2 edited

Legend:

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

    r10903 r10906  
    99*  @author Joshua Hoblitt, University of Hawaii
    1010*
    11 *  @version $Revision: 1.88.2.13 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2007-01-04 22:34:06 $
     11*  @version $Revision: 1.88.2.14 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2007-01-05 01:15:33 $
    1313*
    1414*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    7272fprintf(stderr, "\n");
    7373
    74 #define HANDLE_BAD_BLOCK(memBlock) \
    75 if (badMemBlock(stderr, memBlock)) { \
     74#define HANDLE_BAD_BLOCK(memBlock, file, lineo, func) \
     75if (badMemBlock(stderr, memBlock, file, lineo, func)) { \
    7676    psMemBlockPrint(stderr, memBlock); \
    7777    PS_MEM_ABORT(__func__, "Unsafe to Continue"); \
    7878}
    7979
    80 static bool badMemBlock(FILE *output, const psMemBlock *memBlock);
     80static bool badMemBlock(FILE *output, const psMemBlock *memBlock, const char *file, unsigned int lineo, const char *func);
    8181
    8282// pointer to the last mem block that was allocated
     
    149149 * N.b. If the block wasn't allocated by psAlloc, it will appear corrupted
    150150 */
    151 static bool badMemBlock(FILE *output, const psMemBlock *memBlock)
     151static bool badMemBlock(FILE *output, const psMemBlock *memBlock, const char *file, unsigned int lineno, const char *func)
    152152{
    153153    // n.b. since this is called by psMemCheckCorruption while the memblock
     
    158158    // as they make be changed out from underneath us by new memory allocation
    159159    if (memBlock == NULL) {
    160         fprintf(output, _("NULL memory block found."));
     160        fprintf(output, _("NULL memory block. Caught in %s at (%s:%d.)."), func, file, lineno);
    161161        return true;
    162162    }
     
    164164    if (memBlock->refCounter == 0) {
    165165        // using an unreferenced block of memory, are you?
    166         fprintf(output, _("Memory block was freed but still being used."));
     166        fprintf(output, _("Memory block was freed but still being used.  Caught in %s at (%s:%d)."), func, file, lineno);
    167167        return true;
    168168    }
    169169
    170170    if (memBlock->startblock != P_PS_MEMMAGIC || memBlock->endblock != P_PS_MEMMAGIC) {
    171         fprintf(output, _("Memory block is corrupted; buffer underflow detected."));
     171        fprintf(output, _("Memory block is corrupted; buffer underflow detected.Caught in %s at (%s:%d)."), func, file, lineno);
    172172        return true;
    173173    }
    174174    if (*(psPtr *)((int8_t *) (memBlock + 1) + memBlock->userMemorySize) != P_PS_MEMMAGIC) {
    175175        fprintf(output,
    176                 _("Memory block is corrupted; buffer overflow detected."));
     176                _("Memory block is corrupted; buffer overflow detected. Caught in %s at (%s:%d)."), func, file, lineno);
    177177        return true;
    178178    }
     
    321321    psS32 nbad = 0;               // number of bad blocks
    322322    for (psMemBlock *memBlock = lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) {
    323         if (badMemBlock(output, memBlock)) {
     323        if (badMemBlock(output, memBlock, __FILE__, __LINE__, __func__)) {
    324324            nbad++;
    325325
     
    443443    psMemBlock *memBlock = ((psMemBlock *)ptr) - 1;
    444444
    445     HANDLE_BAD_BLOCK(memBlock);
     445    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    446446
    447447    if (size == memBlock->userMemorySize) {
     
    555555
    556556// return refCounter
    557 psReferenceCount psMemGetRefCounter(const psPtr ptr)
     557psReferenceCount p_psMemGetRefCounter(const psPtr ptr,
     558                                      const char *file,
     559                                      unsigned int lineno,
     560                                      const char *func)
    558561{
    559562    if (ptr == NULL) {
     
    563566    psMemBlock *memBlock = ((psMemBlock *) ptr) - 1;
    564567
    565     HANDLE_BAD_BLOCK(memBlock);
     568    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    566569
    567570    return memBlock->refCounter;
     
    571574psPtr p_psMemIncrRefCounter(const psPtr ptr,
    572575                            const char *file,
    573                             psS32 lineno)
     576                            unsigned int lineno,
     577                            const char *func)
    574578{
    575579    if (ptr == NULL) {
     
    579583    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    580584
    581     HANDLE_BAD_BLOCK(memBlock);
     585    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    582586
    583587    memBlock->refCounter++;
     
    629633psPtr p_psMemDecrRefCounter(psPtr ptr,
    630634                            const char *file,
    631                             psS32 lineno)
     635                            unsigned int lineno,
     636                            const char *func)
    632637{
    633638    if (ptr == NULL) {
     
    637642    psMemBlock *memBlock = ((psMemBlock *) ptr) - 1;
    638643
    639     HANDLE_BAD_BLOCK(memBlock);
    640 
    641     if (memBlock->refCounter < 1) {
    642         PS_MEM_ABORT(__func__,_("Block %lu, allocated at %s (%s:%d), freed multiple times at %s:%d."),
    643                      (unsigned long)memBlock->id,
    644                      memBlock->func,
    645                      memBlock->file,
    646                      memBlock->lineno,
    647                      file,
    648                      lineno);
    649     }
     644    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    650645
    651646    // if we have multiple references, just decrement the count and return.
     
    693688}
    694689
    695 void psMemSetDeallocator(psPtr ptr,
    696                          psFreeFunc freeFunc)
     690void p_psMemSetDeallocator(psPtr ptr,
     691                           psFreeFunc freeFunc,
     692                           const char *file,
     693                           unsigned int lineno,
     694                           const char *func)
    697695{
    698696    if (ptr == NULL) {
     
    702700    psMemBlock* memBlock = ((psMemBlock *)ptr) - 1;
    703701
    704     HANDLE_BAD_BLOCK(memBlock);
     702    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    705703
    706704    memBlock->freeFunc = freeFunc;
    707705}
    708706
    709 psFreeFunc psMemGetDeallocator(const psPtr ptr)
     707psFreeFunc p_psMemGetDeallocator(const psPtr ptr,
     708                                 const char *file,
     709                                 unsigned int lineno,
     710                                 const char *func)
    710711{
    711712    if (ptr == NULL) {
     
    715716    psMemBlock* memBlock = ((psMemBlock *)ptr) - 1;
    716717
    717     HANDLE_BAD_BLOCK(memBlock);
     718    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    718719
    719720    return memBlock->freeFunc;
     
    983984}
    984985
    985 bool p_psMemGetPersistent(psPtr ptr)
     986bool p_psMemGetPersistent(psPtr ptr,
     987                          const char *file,
     988                          unsigned int lineno,
     989                          const char *func)
    986990{
    987991    if (ptr == NULL) {
     
    991995    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    992996
    993     HANDLE_BAD_BLOCK(memBlock);
     997    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    994998
    995999    return memBlock->persistent;
     
    9971001
    9981002void p_psMemSetPersistent(psPtr ptr,
    999                           bool value)
     1003                          bool value,
     1004                          const char *file,
     1005                          unsigned int lineno,
     1006                          const char *func)
    10001007{
    10011008    if (ptr == NULL) {
     
    10051012    psMemBlock* memBlock = ((psMemBlock *) ptr) - 1;
    10061013
    1007     HANDLE_BAD_BLOCK(memBlock);
     1014    HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
    10081015
    10091016    memBlock->persistent = value;
  • branches/jch-memory/psLib/src/sys/psMemory.h

    r10902 r10906  
    1212 *  @ingroup MemoryManagement
    1313 *
    14  *  @version $Revision: 1.61.2.5 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2007-01-04 21:55:39 $
     14 *  @version $Revision: 1.61.2.6 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2007-01-05 01:15:33 $
    1616 *
    1717 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    109109typedef void (*psMemProblemCallback) (
    110110    psMemBlock* ptr,                   ///< the pointer to the problematic memory block.
    111     const char *filename,                    ///< the file in which the problem originated
     111    const char *file,                  ///< the file in which the problem originated
    112112    unsigned int lineno                ///< the line number in which the problem originated
    113113);
     
    139139psPtr p_psAlloc(
    140140    size_t size,                       ///< Size required
    141     const char *filename,              ///< File of caller
     141    const char *file,                  ///< File of caller
    142142    unsigned int lineno,               ///< Line number of caller
    143143    const char *func                   ///< Function name of caller
     
    152152#endif // ! DOXYGEN
    153153
     154
    154155/** Set the deallocator routine
    155156 *
     
    159160 *
    160161 */
     162#ifdef DOXYGEN
     163
    161164void psMemSetDeallocator(
    162165    psPtr ptr,                         ///< the memory block to operate on
    163166    psFreeFunc freeFunc                ///< the function to be executed at deallocation
    164167);
     168
     169#else // ifdef DOXYGEN
     170
     171void p_psMemSetDeallocator(
     172    psPtr ptr,                          ///< the memory block to operate on
     173    psFreeFunc freeFunc,                ///< the function to be executed at deallocation
     174    const char *file,                   ///< File of caller
     175    unsigned int lineno,                ///< Line number of caller
     176    const char *func                    ///< Function name of caller
     177);
     178
     179#ifndef SWIG
     180#define psMemSetDeallocator(ptr, freeFunc) \
     181p_psMemSetDeallocator(ptr, freeFunc, __FILE__, __LINE__, __func__);
     182#endif // ! SWIG
     183
     184#endif // ifdef DOXYGEN
     185
    165186
    166187/** Get the deallocator routine
     
    173194 *  @return psFreeFunc    the routine to be called at deallocation.
    174195 */
     196#ifdef DOXYGEN
     197
    175198psFreeFunc psMemGetDeallocator(
    176     const psPtr ptr                    ///< the memory block
    177 );
     199    const psPtr ptr                     ///< the memory block
     200);
     201
     202#else // ifdef DOXYGEN
     203
     204psFreeFunc p_psMemGetDeallocator(
     205    const psPtr ptr,                    ///< the memory block
     206    const char *file,                   ///< File of caller
     207    unsigned int lineno,                ///< Line number of caller
     208    const char *func                    ///< Function name of caller
     209);
     210
     211#ifndef SWIG
     212#define psMemGetDeallocator(ptr) \
     213p_psMemGetDeallocator(ptr, __FILE__, __LINE__, __func__)
     214#endif // ! SWIG
     215
     216#endif // ifdef DOXYGEN
     217
    178218
    179219/** Checks the deallocator to see if the pointer matches the desired datatype.
     
    207247bool psMemGetThreadSafety(void);
    208248
     249
    209250/** Set the memory as persistent so that it is ignored when detecting memory leaks.
    210251 *
     
    217258 *
    218259 */
     260#ifdef DOXYGEN
     261
     262void psMemSetPersistent(
     263    psPtr ptr,                         ///< the memory block to operate on
     264    bool value,                        ///< true if memory is persistent, otherwise false
     265);
     266
     267#else // #ifdef DOXYGEN
     268
    219269void p_psMemSetPersistent(
    220270    psPtr ptr,                         ///< the memory block to operate on
    221     bool value                         ///< true if memory is persistent, otherwise false
    222 );
     271    bool value,                        ///< true if memory is persistent, otherwise false
     272    const char *file,                  ///< File of caller
     273    unsigned int lineno,               ///< Line number of caller
     274    const char *func                   ///< Function name of caller
     275);
     276
     277#ifndef SWIG
     278#define psMemSetPersistent(ptr, value) \
     279p_psMemSetPersistent(ptr, value, __FILE__, __LINE__, __func__);
     280#endif // ! SWIG
     281
     282#endif // DOXYGEN
     283
    223284
    224285/** Set whether allocated memory is persistent
     
    229290 */
    230291bool p_psMemAllocatePersistent(bool is_persistent); ///< Should all memory allocated be persistent?
     292
    231293
    232294/** Get the memory's persistent flag.
     
    239301 *  @return bool    true if memory is marked persistent, otherwise false.
    240302 */
     303#ifdef DOXYGEN
     304
     305bool psMemGetPersistent(
     306    psPtr ptr,                         ///< the memory block to check.
     307);
     308#else // ifdef DOXYGEN
     309
    241310bool p_psMemGetPersistent(
    242     psPtr ptr                          ///< the memory block to check.
    243 );
     311    psPtr ptr,                         ///< the memory block to check.
     312    const char *file,                  ///< File of caller
     313    unsigned int lineno,               ///< Line number of caller
     314    const char *func                   ///< Function name of caller
     315);
     316
     317#ifndef SWIG
     318#define psMemGetPersistent(ptr) \
     319p_MemGetPersistent(ptr, __FILE__, __LINE__, __func__);
     320#endif // ! SWIG
     321
     322#endif // DOXYGEN
    244323
    245324
     
    260339    psPtr ptr,                         ///< Pointer to re-allocate
    261340    size_t size,                       ///< Size required
    262     const char *filename,              ///< File of caller
     341    const char *file,                  ///< File of caller
    263342    unsigned int lineno,               ///< Line number of caller
    264343    const char *func                   ///< Function name of caller
     
    273352#endif // ! DOXYGEN
    274353
     354
    275355/** Free memory.  This operates much like free().
    276356 *
     
    285365/// Free memory.  psFree sends file and line number to p_psFree.
    286366#ifndef SWIG
    287 //#define psFree(ptr) { p_psMemDecrRefCounter((psPtr)ptr, __FILE__, __LINE__); *(void**)&(ptr) = NULL; }
    288 #define psFree(ptr) { p_psMemDecrRefCounter((psPtr)ptr, __FILE__, __LINE__); }
     367#define            psFree(ptr) \
     368p_psMemDecrRefCounter((psPtr *)ptr, __FILE__, __LINE__, __func__);
    289369#endif // ! SWIG
    290370
    291371#endif // ! DOXYGEN
     372
    292373
    293374/** Check for memory leaks.  This scans for allocated memory buffers not freed with an ID not less than id0.
     
    330411 *  @ingroup memRefCount
    331412 */
     413#ifdef DOXYGEN
     414
    332415psReferenceCount psMemGetRefCounter(
    333416    const psPtr ptr                    ///< Pointer to get refCounter for
    334417);
    335418
     419#else // ifdef DOXYGEN
     420
     421psReferenceCount p_psMemGetRefCounter(
     422    const psPtr ptr,                   ///< Pointer to get refCounter for
     423    const char *file,                  ///< File of call
     424    unsigned int lineno,               ///< Line number of call
     425    const char *func                   ///< Function name of caller
     426);
     427
     428#ifndef SWIG
     429#define psMemGetRefCounter(ptr) \
     430p_psMemGetRefCounter(ptr, __FILE__, __LINE__, __func__)
     431#endif // !SWIG
     432
     433#endif // !DOXYGEN
     434
     435
    336436/** Increment reference counter and return the pointer
    337437 *
     
    341441 */
    342442#ifdef DOXYGEN
     443
    343444psPtr psMemIncrRefCounter(
    344445    const psPtr ptr                    ///< Pointer to increment refCounter, and return
    345446);
    346 #else
     447#else // ifdef DOXYGEN
     448
    347449psPtr p_psMemIncrRefCounter(
    348     const psPtr vptr,                  ///< Pointer to increment refCounter, and return
     450    const psPtr ptr,                  ///< Pointer to increment refCounter, and return
    349451    const char *file,                  ///< File of call
    350     psS32 lineno                       ///< Line number of call
    351 );
    352 
    353 #ifndef SWIG
    354 #define psMemIncrRefCounter(vptr) p_psMemIncrRefCounter(vptr, __FILE__, __LINE__)
     452    unsigned int lineno,               ///< Line number of call
     453    const char *func                   ///< Function name of caller
     454);
     455
     456#ifndef SWIG
     457#define psMemIncrRefCounter(ptr) \
     458p_psMemIncrRefCounter(ptr, __FILE__, __LINE__, __func__)
    355459#endif // !SWIG
    356460
    357461#endif // !DOXYGEN
     462
    358463
    359464/** Decrement reference counter and return the pointer
     
    365470 */
    366471#ifdef DOXYGEN
     472
    367473psPtr psMemDecrRefCounter(
    368474    psPtr ptr                         ///< Pointer to decrement refCounter, and return
    369475);
     476
    370477#else // DOXYGEN
     478
    371479psPtr p_psMemDecrRefCounter(
    372     psPtr vptr,                        ///< Pointer to decrement refCounter, and return
     480    psPtr ptr,                        ///< Pointer to decrement refCounter, and return
    373481    const char *file,                  ///< File of call
    374     psS32 lineno                       ///< Line number of call
    375 );
    376 
    377 #ifndef SWIG
    378 #define psMemDecrRefCounter(vptr) p_psMemDecrRefCounter(vptr, __FILE__, __LINE__)
     482    unsigned int lineno,               ///< Line number of call
     483    const char *func                   ///< Function name of caller
     484);
     485
     486#ifndef SWIG
     487#define psMemDecrRefCounter(ptr) \
     488p_psMemDecrRefCounter(ptr, __FILE__, __LINE__, __func__)
    379489#endif // !SWIG
    380490
     
    408518#endif // !DOXYGEN
    409519#endif // psMemSetRefCounter
    410 
    411 /** Set callback for problems.
    412  *
    413  *  At various occasions, the memory manager can check the state of the memory
    414  *  stack. If any of these checks discover that the memory stack is corrupted,
    415  *  the psMemProblemCallback is called.
    416  
    417  *  @ingroup memCallback
    418  *
    419  *  @return psMemProblemCallback       old psMemProblemCallback function
    420  */
    421 psMemProblemCallback psMemProblemCallbackSet(
    422     psMemProblemCallback func          ///< Function to run at memory problem detection
    423 );
    424520
    425521/** Set callback for out-of-memory.
Note: See TracChangeset for help on using the changeset viewer.