IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 432


Ignore:
Timestamp:
Apr 15, 2004, 4:18:57 PM (22 years ago)
Author:
desonia
Message:

modifications so that psMemory compiles.

Location:
trunk/psLib/src
Files:
14 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/Makefile

    r423 r432  
    44
    55include Makefile.Globals
     6
     7.PHONY: sysUtils installSysUtils cleanSysUtils
    68
    79NAME:= "$Name: not supported by cvs2svn $ "
     
    1517INSTALLTARGETS = installSysUtils
    1618CLEANTARGETS = cleanSysUtils
     19DISTCLEANTARGETS = distcleanSysUtils
    1720
    1821all:    $(ALLTARGETS) 
     
    2932cleanSysUtils:
    3033        $(MAKE) --directory=sysUtils clean
     34
     35distcleanSysUtils:
     36        $(MAKE) --directory=sysUtils distclean
    3137
    3238
  • trunk/psLib/src/sys/psAbort.c

    r429 r432  
    99 *  @author Eric Van Alst, MHPCC
    1010 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-15 20:37:56 $
     11 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-04-16 02:18:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5757/*****************************************************************************/
    5858
    59 void psAbort(
    60     char *name,   
    61     char *fmt,
    62     ...   
    63 )
     59void psAbort(const char *name, const char *fmt,... )
    6460{
    6561    va_list    argPtr;     // variable list arguement pointer
  • trunk/psLib/src/sys/psAbort.h

    r428 r432  
    99 *  @author Eric Van Alst, MHPCC
    1010 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-15 20:37:12 $
     11 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-04-16 02:18:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4545 */
    4646void psAbort(
    47     char *name,   /**< Source of abort such as file or function detected */
    48     char *fmt,    /**< A printf style formatting statement defining msg */
     47    const char* name,                   ///< Source of abort such as file or function detected
     48    const char* fmt,                    ///< A printf style formatting statement defining msg
    4949    ...
    5050);
  • trunk/psLib/src/sys/psError.c

    r427 r432  
    1010 *  @author Eric Van Alst, MHPCC
    1111 *   
    12  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-04-15 19:04:58 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-04-16 02:18:57 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5757/*****************************************************************************/
    5858
    59 void psError(
    60    char *name,   
    61    char *fmt,
    62    ...   
    63 )
     59void psError(const char *name,const char *fmt, ...)
    6460{
    6561    va_list    argPtr;        // variable list arguement pointer
  • trunk/psLib/src/sys/psError.h

    r426 r432  
    1010 *  @author Eric Van Alst, MHPCC
    1111 *   
    12  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-04-15 19:03:41 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-04-16 02:18:57 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4545 */
    4646void psError(
    47     char *name,   /**< Source of error such as file or function detected */
    48     char *fmt,    /**< A printf style formatting statement defining msg */
     47    const char *name,                   ///< Source of error such as file or function detected
     48    const char *fmt,                    ///< A printf style formatting statement defining msg
    4949    ...
    5050);
  • trunk/psLib/src/sys/psMemory.c

    r430 r432  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-15 21:22:09 $
     10 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-04-16 02:18:57 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2020#include "psMemory.h"
    2121#include "psError.h"
     22#include "psAbort.h"
    2223
    2324#define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
    2425
    25 static int bad_memblock(const psMemBlock *m, int quiet);
     26static int checkMemBlock(const psMemBlock *m, const char* funcName);
    2627static int numMemBlock = 0;             // size of memBlocks
    2728static psMemBlock **memBlocks = NULL;
     
    5859                          const char *file, int lineno)
    5960{
    60     if (bad_memblock(ptr, 0)) {
    61         fprintf(stderr, "Memory corruption or an attempt to manipulate memory "
    62                 "not allocated with psAlloc at %s", file);
    63         if (lineno > 0) {
    64             fprintf(stderr, ":%d", lineno);
    65         }
    66         fprintf(stderr, "\n");
    67     } else if (ptr->refCounter <= 0) {
    68         psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR,
    69                 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    70                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    71     } else if (ptr->refCounter > 1) {
    72         psError(__func__,  PS_ERR_BADFREE, PS_NEW_ERROR,
    73                 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
    74                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    75     }
    76 
    77     if (lineno > 0) {
     61    checkMemBlock(ptr, __func__);
     62
     63        if (ptr->refCounter <= 0) {
     64                psError(__func__,
     65                        "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
     66                                ptr->id, ptr->file, ptr->lineno, file, lineno);
     67        } else if (ptr->refCounter > 1) {
     68                psError(__func__,
     69                        "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
     70                                ptr->id, ptr->file, ptr->lineno, file, lineno);
     71        }
     72
     73        if (lineno > 0) {
    7874        psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno);
    7975    }
     
    123119 * isn't resignalled)
    124120 */
    125 static int memAllocateCallbackDefault(const psMemBlock *ptr)
     121static long memAllocateCallbackDefault(const psMemBlock *ptr)
    126122{
    127123    static int incr = 0;  // "p_psMemAllocateID += incr"
    128124
    129     assert (ptr != NULL);  // prevent compiler warnings
    130 
    131125    return incr;
    132126}
    133127
    134 static int memFreeCallbackDefault(const psMemBlock *ptr)
     128static long memFreeCallbackDefault(const psMemBlock *ptr)
    135129{
    136130    static int incr = 0;  // "p_psMemFreeID += incr"
    137131
    138     assert (ptr != NULL);  // prevent compiler warnings
    139 
    140132    return incr;
    141133}
     
    144136 * The default callbacks, and the routines to change them
    145137 */
    146 static psMemCallback memAllocateCallback = memAllocateCallbackDefault;
    147 static psMemCallback memFreeCallback = memFreeCallbackDefault;
    148 
    149 psMemCallback psMemAllocateSetCallback(psMemCallback func)
    150 {
    151     psMemCallback old = memAllocateCallback;
     138static psMemAllocateCallback memAllocateCallback = memAllocateCallbackDefault;
     139static psMemFreeCallback memFreeCallback = memFreeCallbackDefault;
     140
     141psMemAllocateCallback psMemAllocateSetCallback(psMemAllocateCallback func)
     142{
     143    psMemFreeCallback old = memAllocateCallback;
    152144
    153145    if (func != NULL) {
     
    160152}
    161153
    162 psMemCallback psMemFreeSetCallback(psMemCallback func)
    163 {
    164     psMemCallback old = memFreeCallback;
     154psMemFreeCallback psMemFreeSetCallback(psMemFreeCallback func)
     155{
     156    psMemFreeCallback old = memFreeCallback;
    165157
    166158    if (func != NULL) {
     
    191183
    192184static int
    193 bad_memblock(const psMemBlock *m, // block to check
    194              int quiet)   // be quiet?
     185checkMemBlock(const psMemBlock *m, // block to check
     186             const char* funcName)   // be quiet?
    195187{
    196188    if (m == NULL) {
    197         if (!quiet) {
    198             psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    199                     "psMemCheckCorruption: NULL memory block\n");
    200         }
     189        psError(funcName,"Memory Corruption: NULL memory block found.");
    201190        return(1);
    202191    }
    203192
    204193    if (!ALIGNED(m)) {
    205         if (!quiet) {
    206             psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    207                     "psMemCheckCorruption: non-aligned memory block\n");
    208         }
     194        psError(funcName, "psMemCheckCorruption: non-aligned memory block");
    209195        return(1);
    210196    }
    211197
    212     if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {
    213         if (!quiet) {
    214             psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    215                     "psMemCheckCorruption: memory block %ld is corrupted\n", m->id);
    216         }
     198    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
     199        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id);
    217200        return(1);
    218201    }
     
    231214    for (int i = 1; i <= memid; i++) {
    232215        if (memBlocks[i] != NULL) {
    233             if (bad_memblock(memBlocks[i], 1)) {
     216            if (checkMemBlock(memBlocks[i], __func__)) {
    234217                nbad++;
    235218
    236                 memProblemCallback(memBlocks[i], "(psMemCheckCorruption)", 0);
     219                memProblemCallback(memBlocks[i], __func__, __LINE__);
    237220
    238221                if (abort_on_error) {
     
    263246    ptr->file = file;
    264247    *(int *)&ptr->lineno = lineno;
    265     *(void **)&ptr->magic0 = *(void **)&ptr->magic = P_PS_MEMMAGIC;
     248    ptr->startblock = P_PS_MEMMAGIC;
     249        ptr->endblock = P_PS_MEMMAGIC;
    266250
    267251    ptr->refCounter = 1;  // one user so far
     
    273257        p_psMemAllocateID += memAllocateCallback(ptr);
    274258    }
    275     if (memid >= nMemBlock) {
    276         nMemBlock = (nMemBlock == 0) ? 100000 : 2*nMemBlock;
    277         memBlocks = realloc(memBlocks, nMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
     259    if (memid >= numMemBlock) {
     260        numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock;
     261        memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
    278262    }
    279263    memBlocks[ptr->id] = ptr;
     
    311295    }
    312296
    313     if (bad_memblock(ptr, 0)) {
     297    if (checkMemBlock(ptr, "psFree") != 0) {
    314298        memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    315299    } else {
     
    369353    }
    370354
    371     assert (*arr == NULL);  // Don't generate a memory leak!
     355    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
    372356
    373357    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
     
    395379    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    396380
    397     if (bad_memblock(ptr, 0))
    398     {
    399         memProblemCallback(ptr, "(psMemGetRefCounter)", -1);
     381    if (checkMemBlock(ptr, __func__) != 0)
     382    {
     383        memProblemCallback(ptr, __func__, __LINE__);
    400384    }
    401385
     
    412396    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    413397
    414     if (bad_memblock(ptr, 0))
    415     {
    416         memProblemCallback(ptr, "(psMemIncrRefCounter)", -1);
     398    if (checkMemBlock(ptr, __func__))
     399    {
     400        memProblemCallback(ptr, __func__, __LINE__);
    417401    }
    418402
     
    431415    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    432416
    433     if (bad_memblock(ptr, 0))
    434     {
    435         memProblemCallback(ptr, "(psMemDecrRefCounter)", -1);
     417    if (checkMemBlock(ptr, __func__))
     418    {
     419        memProblemCallback(ptr, __func__, __LINE__);
    436420    }
    437421
  • trunk/psLib/src/sys/psMemory.h

    r430 r432  
    1414 *  @author Robert Lupton, Princeton University
    1515 *
    16  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-04-15 21:22:09 $
     16 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-04-16 02:18:57 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7979
    8080/// Check for memory leaks
    81 int psMemCheckLeaks(int id0,  ///< don't list blocks with id < id0
    82                     psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL
    83                     FILE *fd)  ///< print list of leaks to fd (or NULL)
    84 ;
     81int psMemCheckLeaks(
     82        int id0,                        ///< don't list blocks with id < id0
     83        psMemBlock ***arr,              ///< pointer to array of pointers to leaked blocks, or NULL
     84        FILE *fd                        ///< print list of leaks to fd (or NULL)
     85);
    8586
    8687/// Check for memory corruption
    87 int psMemCheckCorruption(int abort_on_error) ///< Abort on detecting corruption?
    88 ;
     88int psMemCheckCorruption(
     89        int abort_on_error              ///< Abort on detecting corruption?
     90);
    8991
    9092/// Return reference counter
    91 int psMemGetRefCounter(void *vptr) ///< Pointer to get refCounter for
    92 ;
     93int psMemGetRefCounter(
     94        void *vptr                      ///< Pointer to get refCounter for
     95);
    9396
    9497/// Increment reference counter and return the pointer
    95 void *psMemIncrRefCounter(void *vptr) ///< Pointer to increment refCounter, and return
    96 ;
     98void *psMemIncrRefCounter(
     99        void *vptr                      ///< Pointer to increment refCounter, and return
     100);
    97101
    98102/// Decrement reference counter and return the pointer
    99 void *psMemDecrRefCounter(void *vptr) ///< Pointer to decrement refCounter, and return
    100 ;
     103void *psMemDecrRefCounter(
     104        void *vptr                      ///< Pointer to decrement refCounter, and return
     105);
    101106
    102107/// Set callback for problems
    103 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) ///< Function to run
    104 ;
     108psMemProblemCallback psMemProblemCallbackSet(
     109        psMemProblemCallback func       ///< Function to run
     110);
    105111
    106112/// Set callback for out-of-memory
    107 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func) ///< Function to run
    108 ;
     113psMemExhaustedCallback psMemExhaustedCallbackSet(
     114        psMemExhaustedCallback func     ///< Function to run
     115);
    109116
    110117/// Set call back for when a particular memory block is allocated
    111 psMemCallback psMemAllocateCallbackSet(psMemAllocateCallback func) ///< Function to run
    112 ;
     118psMemAllocateCallback psMemAllocateCallbackSet(
     119        psMemAllocateCallback func      ///< Function to run
     120);
    113121
    114122/// Set call back for when a particular memory block is freed
    115 psMemCallback psMemFreeCallbackSet(psMemFreeCallback func) ///< Function to run
    116 ;
     123psMemFreeCallback psMemFreeCallbackSet(
     124        psMemFreeCallback func          ///< Function to run
     125);
    117126
    118127/// get next memory ID
     
    121130
    122131/// set p_psMemAllocateID to id
    123 long psMemAllocateIDSet(long id) ///< ID to set
    124 ;
     132long psMemAllocateIDSet(
     133        long id                         ///< ID to set
     134);
    125135
    126136/// set p_psMemFreeID to id
    127 long psMemFreeIDSet(long id)  ///< ID to set
    128 ;
     137long psMemFreeIDSet(
     138        long id                         ///< ID to set
     139);
    129140
    130141//@} End of public Functions
  • trunk/psLib/src/sysUtils/Makefile

    r423 r432  
    1313include ../Makefile.Globals
    1414
    15 CFLAGS = $(CFLAGS_MODULE) $(INCLUDE_GLOBAL)
    16 
    17 SRC_OBJS = psTrace.o
     15SRC_OBJS = psMemory.o psError.o psAbort.o
    1816
    1917libpsSysUtils.a: $(SRC_OBJS)
  • trunk/psLib/src/sysUtils/psAbort.c

    r429 r432  
    99 *  @author Eric Van Alst, MHPCC
    1010 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-15 20:37:56 $
     11 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-04-16 02:18:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5757/*****************************************************************************/
    5858
    59 void psAbort(
    60     char *name,   
    61     char *fmt,
    62     ...   
    63 )
     59void psAbort(const char *name, const char *fmt,... )
    6460{
    6561    va_list    argPtr;     // variable list arguement pointer
  • trunk/psLib/src/sysUtils/psAbort.h

    r428 r432  
    99 *  @author Eric Van Alst, MHPCC
    1010 *   
    11  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-04-15 20:37:12 $
     11 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-04-16 02:18:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4545 */
    4646void psAbort(
    47     char *name,   /**< Source of abort such as file or function detected */
    48     char *fmt,    /**< A printf style formatting statement defining msg */
     47    const char* name,                   ///< Source of abort such as file or function detected
     48    const char* fmt,                    ///< A printf style formatting statement defining msg
    4949    ...
    5050);
  • trunk/psLib/src/sysUtils/psError.c

    r427 r432  
    1010 *  @author Eric Van Alst, MHPCC
    1111 *   
    12  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-04-15 19:04:58 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-04-16 02:18:57 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    5757/*****************************************************************************/
    5858
    59 void psError(
    60    char *name,   
    61    char *fmt,
    62    ...   
    63 )
     59void psError(const char *name,const char *fmt, ...)
    6460{
    6561    va_list    argPtr;        // variable list arguement pointer
  • trunk/psLib/src/sysUtils/psError.h

    r426 r432  
    1010 *  @author Eric Van Alst, MHPCC
    1111 *   
    12  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2004-04-15 19:03:41 $
     12 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2004-04-16 02:18:57 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4545 */
    4646void psError(
    47     char *name,   /**< Source of error such as file or function detected */
    48     char *fmt,    /**< A printf style formatting statement defining msg */
     47    const char *name,                   ///< Source of error such as file or function detected
     48    const char *fmt,                    ///< A printf style formatting statement defining msg
    4949    ...
    5050);
  • trunk/psLib/src/sysUtils/psMemory.c

    r430 r432  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-04-15 21:22:09 $
     10 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-04-16 02:18:57 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2020#include "psMemory.h"
    2121#include "psError.h"
     22#include "psAbort.h"
    2223
    2324#define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
    2425
    25 static int bad_memblock(const psMemBlock *m, int quiet);
     26static int checkMemBlock(const psMemBlock *m, const char* funcName);
    2627static int numMemBlock = 0;             // size of memBlocks
    2728static psMemBlock **memBlocks = NULL;
     
    5859                          const char *file, int lineno)
    5960{
    60     if (bad_memblock(ptr, 0)) {
    61         fprintf(stderr, "Memory corruption or an attempt to manipulate memory "
    62                 "not allocated with psAlloc at %s", file);
    63         if (lineno > 0) {
    64             fprintf(stderr, ":%d", lineno);
    65         }
    66         fprintf(stderr, "\n");
    67     } else if (ptr->refCounter <= 0) {
    68         psError(__func__, PS_ERR_BADFREE, PS_NEW_ERROR,
    69                 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    70                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    71     } else if (ptr->refCounter > 1) {
    72         psError(__func__,  PS_ERR_BADFREE, PS_NEW_ERROR,
    73                 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
    74                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    75     }
    76 
    77     if (lineno > 0) {
     61    checkMemBlock(ptr, __func__);
     62
     63        if (ptr->refCounter <= 0) {
     64                psError(__func__,
     65                        "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
     66                                ptr->id, ptr->file, ptr->lineno, file, lineno);
     67        } else if (ptr->refCounter > 1) {
     68                psError(__func__,
     69                        "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
     70                                ptr->id, ptr->file, ptr->lineno, file, lineno);
     71        }
     72
     73        if (lineno > 0) {
    7874        psAbort(__func__, "Detected a problem in the memory system at %s:%d", file, lineno);
    7975    }
     
    123119 * isn't resignalled)
    124120 */
    125 static int memAllocateCallbackDefault(const psMemBlock *ptr)
     121static long memAllocateCallbackDefault(const psMemBlock *ptr)
    126122{
    127123    static int incr = 0;  // "p_psMemAllocateID += incr"
    128124
    129     assert (ptr != NULL);  // prevent compiler warnings
    130 
    131125    return incr;
    132126}
    133127
    134 static int memFreeCallbackDefault(const psMemBlock *ptr)
     128static long memFreeCallbackDefault(const psMemBlock *ptr)
    135129{
    136130    static int incr = 0;  // "p_psMemFreeID += incr"
    137131
    138     assert (ptr != NULL);  // prevent compiler warnings
    139 
    140132    return incr;
    141133}
     
    144136 * The default callbacks, and the routines to change them
    145137 */
    146 static psMemCallback memAllocateCallback = memAllocateCallbackDefault;
    147 static psMemCallback memFreeCallback = memFreeCallbackDefault;
    148 
    149 psMemCallback psMemAllocateSetCallback(psMemCallback func)
    150 {
    151     psMemCallback old = memAllocateCallback;
     138static psMemAllocateCallback memAllocateCallback = memAllocateCallbackDefault;
     139static psMemFreeCallback memFreeCallback = memFreeCallbackDefault;
     140
     141psMemAllocateCallback psMemAllocateSetCallback(psMemAllocateCallback func)
     142{
     143    psMemFreeCallback old = memAllocateCallback;
    152144
    153145    if (func != NULL) {
     
    160152}
    161153
    162 psMemCallback psMemFreeSetCallback(psMemCallback func)
    163 {
    164     psMemCallback old = memFreeCallback;
     154psMemFreeCallback psMemFreeSetCallback(psMemFreeCallback func)
     155{
     156    psMemFreeCallback old = memFreeCallback;
    165157
    166158    if (func != NULL) {
     
    191183
    192184static int
    193 bad_memblock(const psMemBlock *m, // block to check
    194              int quiet)   // be quiet?
     185checkMemBlock(const psMemBlock *m, // block to check
     186             const char* funcName)   // be quiet?
    195187{
    196188    if (m == NULL) {
    197         if (!quiet) {
    198             psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    199                     "psMemCheckCorruption: NULL memory block\n");
    200         }
     189        psError(funcName,"Memory Corruption: NULL memory block found.");
    201190        return(1);
    202191    }
    203192
    204193    if (!ALIGNED(m)) {
    205         if (!quiet) {
    206             psError(__func__,  PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    207                     "psMemCheckCorruption: non-aligned memory block\n");
    208         }
     194        psError(funcName, "psMemCheckCorruption: non-aligned memory block");
    209195        return(1);
    210196    }
    211197
    212     if (m->magic0 != P_PS_MEMMAGIC || m->magic != P_PS_MEMMAGIC) {
    213         if (!quiet) {
    214             psError(__func__, PS_ERR_MEMORY_CORRUPTION, PS_NEW_ERROR,
    215                     "psMemCheckCorruption: memory block %ld is corrupted\n", m->id);
    216         }
     198    if (m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC) {
     199        psError(funcName, "psMemCheckCorruption: memory block %ld is corrupted", m->id);
    217200        return(1);
    218201    }
     
    231214    for (int i = 1; i <= memid; i++) {
    232215        if (memBlocks[i] != NULL) {
    233             if (bad_memblock(memBlocks[i], 1)) {
     216            if (checkMemBlock(memBlocks[i], __func__)) {
    234217                nbad++;
    235218
    236                 memProblemCallback(memBlocks[i], "(psMemCheckCorruption)", 0);
     219                memProblemCallback(memBlocks[i], __func__, __LINE__);
    237220
    238221                if (abort_on_error) {
     
    263246    ptr->file = file;
    264247    *(int *)&ptr->lineno = lineno;
    265     *(void **)&ptr->magic0 = *(void **)&ptr->magic = P_PS_MEMMAGIC;
     248    ptr->startblock = P_PS_MEMMAGIC;
     249        ptr->endblock = P_PS_MEMMAGIC;
    266250
    267251    ptr->refCounter = 1;  // one user so far
     
    273257        p_psMemAllocateID += memAllocateCallback(ptr);
    274258    }
    275     if (memid >= nMemBlock) {
    276         nMemBlock = (nMemBlock == 0) ? 100000 : 2*nMemBlock;
    277         memBlocks = realloc(memBlocks, nMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
     259    if (memid >= numMemBlock) {
     260        numMemBlock = (numMemBlock == 0) ? 100000 : 2*numMemBlock;
     261        memBlocks = realloc(memBlocks, numMemBlock*sizeof(psMemBlock *)); // NOT psRealloc
    278262    }
    279263    memBlocks[ptr->id] = ptr;
     
    311295    }
    312296
    313     if (bad_memblock(ptr, 0)) {
     297    if (checkMemBlock(ptr, "psFree") != 0) {
    314298        memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    315299    } else {
     
    369353    }
    370354
    371     assert (*arr == NULL);  // Don't generate a memory leak!
     355    p_psFree(*arr,__FILE__, __LINE__);  // Don't generate a memory leak!
    372356
    373357    *arr = p_psAlloc(nleak*sizeof(psMemBlock), __FILE__, __LINE__);
     
    395379    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    396380
    397     if (bad_memblock(ptr, 0))
    398     {
    399         memProblemCallback(ptr, "(psMemGetRefCounter)", -1);
     381    if (checkMemBlock(ptr, __func__) != 0)
     382    {
     383        memProblemCallback(ptr, __func__, __LINE__);
    400384    }
    401385
     
    412396    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    413397
    414     if (bad_memblock(ptr, 0))
    415     {
    416         memProblemCallback(ptr, "(psMemIncrRefCounter)", -1);
     398    if (checkMemBlock(ptr, __func__))
     399    {
     400        memProblemCallback(ptr, __func__, __LINE__);
    417401    }
    418402
     
    431415    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    432416
    433     if (bad_memblock(ptr, 0))
    434     {
    435         memProblemCallback(ptr, "(psMemDecrRefCounter)", -1);
     417    if (checkMemBlock(ptr, __func__))
     418    {
     419        memProblemCallback(ptr, __func__, __LINE__);
    436420    }
    437421
  • trunk/psLib/src/sysUtils/psMemory.h

    r430 r432  
    1414 *  @author Robert Lupton, Princeton University
    1515 *
    16  *  @version $Revision: 1.1 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-04-15 21:22:09 $
     16 *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-04-16 02:18:57 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7979
    8080/// Check for memory leaks
    81 int psMemCheckLeaks(int id0,  ///< don't list blocks with id < id0
    82                     psMemBlock ***arr, ///< pointer to array of pointers to leaked blocks, or NULL
    83                     FILE *fd)  ///< print list of leaks to fd (or NULL)
    84 ;
     81int psMemCheckLeaks(
     82        int id0,                        ///< don't list blocks with id < id0
     83        psMemBlock ***arr,              ///< pointer to array of pointers to leaked blocks, or NULL
     84        FILE *fd                        ///< print list of leaks to fd (or NULL)
     85);
    8586
    8687/// Check for memory corruption
    87 int psMemCheckCorruption(int abort_on_error) ///< Abort on detecting corruption?
    88 ;
     88int psMemCheckCorruption(
     89        int abort_on_error              ///< Abort on detecting corruption?
     90);
    8991
    9092/// Return reference counter
    91 int psMemGetRefCounter(void *vptr) ///< Pointer to get refCounter for
    92 ;
     93int psMemGetRefCounter(
     94        void *vptr                      ///< Pointer to get refCounter for
     95);
    9396
    9497/// Increment reference counter and return the pointer
    95 void *psMemIncrRefCounter(void *vptr) ///< Pointer to increment refCounter, and return
    96 ;
     98void *psMemIncrRefCounter(
     99        void *vptr                      ///< Pointer to increment refCounter, and return
     100);
    97101
    98102/// Decrement reference counter and return the pointer
    99 void *psMemDecrRefCounter(void *vptr) ///< Pointer to decrement refCounter, and return
    100 ;
     103void *psMemDecrRefCounter(
     104        void *vptr                      ///< Pointer to decrement refCounter, and return
     105);
    101106
    102107/// Set callback for problems
    103 psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func) ///< Function to run
    104 ;
     108psMemProblemCallback psMemProblemCallbackSet(
     109        psMemProblemCallback func       ///< Function to run
     110);
    105111
    106112/// Set callback for out-of-memory
    107 psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func) ///< Function to run
    108 ;
     113psMemExhaustedCallback psMemExhaustedCallbackSet(
     114        psMemExhaustedCallback func     ///< Function to run
     115);
    109116
    110117/// Set call back for when a particular memory block is allocated
    111 psMemCallback psMemAllocateCallbackSet(psMemAllocateCallback func) ///< Function to run
    112 ;
     118psMemAllocateCallback psMemAllocateCallbackSet(
     119        psMemAllocateCallback func      ///< Function to run
     120);
    113121
    114122/// Set call back for when a particular memory block is freed
    115 psMemCallback psMemFreeCallbackSet(psMemFreeCallback func) ///< Function to run
    116 ;
     123psMemFreeCallback psMemFreeCallbackSet(
     124        psMemFreeCallback func          ///< Function to run
     125);
    117126
    118127/// get next memory ID
     
    121130
    122131/// set p_psMemAllocateID to id
    123 long psMemAllocateIDSet(long id) ///< ID to set
    124 ;
     132long psMemAllocateIDSet(
     133        long id                         ///< ID to set
     134);
    125135
    126136/// set p_psMemFreeID to id
    127 long psMemFreeIDSet(long id)  ///< ID to set
    128 ;
     137long psMemFreeIDSet(
     138        long id                         ///< ID to set
     139);
    129140
    130141//@} End of public Functions
Note: See TracChangeset for help on using the changeset viewer.