IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

modifications so that psMemory compiles.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.