IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 14676


Ignore:
Timestamp:
Aug 27, 2007, 1:14:35 PM (19 years ago)
Author:
jhoblitt
Message:

add psMemIsAlloced()

Location:
trunk/psLib/src/sys
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psAssert.h

    r12596 r14676  
    2020}
    2121
    22 // Ensure this is a psLib pointer, by checking for the memblock bounds.
    23 #define PS_ASSERT_PTR(NAME, RVAL) \
     22// Ensure this is a psLib pointer
     23#define PS_ASSERT_PTR(PTR, RVAL) \
    2424{ \
    25     if (NAME == NULL) return(RVAL); \
    26     psMemBlock *mb = (psMemBlock*)(NAME) - 1; \
    27     if (mb->startblock != P_PS_MEMMAGIC || mb->endblock != P_PS_MEMMAGIC || \
    28             *(psU32 *)((char *)(mb + 1) + mb->userMemorySize) != P_PS_MEMMAGIC) { \
     25    if (PTR == NULL) return RVAL; \
     26    if (!psMemIsAlloced(PTR)) { \
    2927        psError(PS_ERR_MEMORY_CORRUPTION, false, \
    30                 "Error: Pointer %s is corrupted or not on the PS memory system.", \
    31                 #NAME); \
    32         return (RVAL); \
     28            "Error: Pointer %p is corrupted or not on the PS memory system.", \
     29            PTR); \
     30        return RVAL; \
    3331    } \
    3432}
  • trunk/psLib/src/sys/psMemory.c

    r12886 r14676  
    1010*  @author Joshua Hoblitt, University of Hawaii
    1111*
    12 *  @version $Revision: 1.96 $ $Name: not supported by cvs2svn $
    13 *  @date $Date: 2007-04-18 19:38:10 $
     12*  @version $Revision: 1.97 $ $Name: not supported by cvs2svn $
     13*  @date $Date: 2007-08-27 23:14:35 $
    1414*
    1515*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    374374                MUTEX_UNLOCK(&memBlockListMutex);
    375375                PS_MEM_ABORT(__func__, "Detected memory corruption");
    376                 return nbad;
    377376            }
    378377        }
     
    383382
    384383    return nbad;
     384}
     385
     386bool p_psMemIsAlloced(const char *file,
     387                      unsigned int lineno,
     388                      const char *func,
     389                      const void *ptr)
     390{
     391    // if ptr is a psAlloc()'d memory, find the actual address of the memBlock
     392    psMemBlock *addr = ((psMemBlock *)ptr) - 1;
     393
     394    // get exclusive access to the memBlock list to avoid it changing on us
     395    // while we use it.
     396    MUTEX_LOCK(&memBlockListMutex);
     397
     398    // loop through the linked list of memBlocks looking for a matching pointer
     399    for (psMemBlock *memBlock = lastMemBlockAllocated; memBlock != NULL; memBlock = memBlock->nextBlock) {
     400        if (memBlock == addr) {
     401            // we found the memBlock
     402            HANDLE_BAD_BLOCK(memBlock, file, lineno, func);
     403
     404            MUTEX_UNLOCK(&memBlockListMutex);
     405            return true;
     406        }
     407    }
     408
     409    // release the lock on the memblock list
     410    MUTEX_UNLOCK(&memBlockListMutex);
     411
     412    return false;
    385413}
    386414
  • trunk/psLib/src/sys/psMemory.h

    r12380 r14676  
    1515 *  @ingroup MemoryManagement
    1616 *
    17  *  @version $Revision: 1.69 $ $Name: not supported by cvs2svn $
    18  *  @date $Date: 2007-03-09 20:11:04 $
     17 *  @version $Revision: 1.70 $ $Name: not supported by cvs2svn $
     18 *  @date $Date: 2007-08-27 23:14:35 $
    1919 *
    2020 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    407407#endif // ifdef DOXYGEN
    408408
     409/** Checks to see if a pointer is to a region of memory that was allocated by psAlloc().
     410 *  @return bool
     411 *
     412 */
     413#ifdef DOXYGEN
     414bool psMemIsAlloced(
     415    const void *ptr                           ///< pointer to memory
     416);
     417#else // ifdef DOXYGEN
     418bool p_psMemIsAlloced(
     419    const char *file,
     420    unsigned int lineno,
     421    const char *func,
     422    const void *ptr
     423);
     424#ifndef SWIG
     425#define psMemIsAlloced(ptr) \
     426      p_psMemIsAlloced(__FILE__, __LINE__, __func__, ptr)
     427#endif // ifndef SWIG
     428#endif // ifdef DOXYGEN
     429
    409430
    410431/** Return reference counter
Note: See TracChangeset for help on using the changeset viewer.