IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 830


Ignore:
Timestamp:
Jun 2, 2004, 10:03:22 AM (22 years ago)
Author:
desonia
Message:

Modified psFree to just decrement the ref. counter if it is > 1.

Location:
trunk/psLib/src
Files:
4 edited

Legend:

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

    r803 r830  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-05-28 03:17:39 $
     10 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-06-02 20:03:22 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1616#define PS_ALLOW_MALLOC                 // we're allowed to call malloc()
    1717
    18 #include <pthread.h>                   // we need a mutex to make this stuff thread safe.
    1918#include <stdlib.h>
    2019#include <stdio.h>
     
    2524#include "psError.h"
    2625#include "psAbort.h"
     26#include "psLogMsg.h"
    2727
    2828#define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
     
    6767        psError(__func__,
    6868                "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    69                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    70     } else if (ptr->refCounter > 1) {
    71         psError(__func__,
    72                 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
    7369                ptr->id, ptr->file, ptr->lineno, file, lineno);
    7470    }
     
    268264    ptr->startblock = P_PS_MEMMAGIC;
    269265    ptr->endblock = P_PS_MEMMAGIC;
    270     ptr->refCounter = 1;                // one user so far
    271266    ptr->userMemorySize = size;
    272267    *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
    273268    ptr->previousBlock = NULL;
     269
     270    pthread_mutex_init(&ptr->refCounterMutex, NULL);
     271    ptr->refCounter = 1;                // one user so far
    274272
    275273    // need exclusive access of the memory block list now...
     
    347345    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    348346
    349     // Did the user ask to be informed about this deallocation?
    350     if (ptr->id == p_psMemFreeID) {
    351         p_psMemFreeID += memFreeCallback(ptr);
    352     }
    353 
    354     if (checkMemBlock(ptr, "psFree") != 0) {
     347    if (checkMemBlock(ptr, __func__) != 0) {
    355348        memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    356     } else {
    357         if (ptr->refCounter > 1) {
    358             psError(__func__,"The buffer being freed is referenced elsewhere. "
    359                     "Buffer's reference count was decremented instead.");
    360             ptr->refCounter--;
    361             memProblemCallback(ptr, file, lineno);
    362         } else {
    363 
    364             // cut the memBlock out of the memBlock list
    365             pthread_mutex_lock(&memBlockListMutex);
    366             if (ptr->nextBlock != NULL) {
    367                 ptr->nextBlock->previousBlock = ptr->previousBlock;
    368             }
    369             if (ptr->previousBlock != NULL) {
    370                 ptr->previousBlock->nextBlock = ptr->nextBlock;
    371             }
    372             if (lastMemBlockAllocated == ptr) {
    373                 lastMemBlockAllocated = ptr->nextBlock;
    374             }
    375 
    376             pthread_mutex_unlock(&memBlockListMutex);
    377 
    378             free(ptr);
    379         }
    380     }
     349    }
     350
     351    psMemDecrRefCounter(vptr);          // this handles the free, if required.
    381352}
    382353
     
    397368    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    398369    {
    399         if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
     370        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    400371            nleak++;
    401372
     
    422393    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    423394    {
    424         if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
     395        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    425396            (*arr)[j++] = iter;
    426397            if (j == nleak) { // found them all
     
    440411psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
    441412{
     413    psMemBlock *ptr;
     414    unsigned int refCount;
     415
     416    if (vptr == NULL)
     417    {
     418        return 0;
     419    }
     420
     421    ptr = ((psMemBlock *)vptr) - 1;
     422
     423    if (checkMemBlock(ptr, __func__) != 0)
     424    {
     425        memProblemCallback(ptr, __func__, __LINE__);
     426    }
     427
     428    pthread_mutex_lock(&ptr->refCounterMutex);
     429    refCount = ptr->refCounter;
     430    pthread_mutex_unlock(&ptr->refCounterMutex);
     431
     432    return refCount;
     433}
     434
     435void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
     436{
     437    psMemBlock *ptr;
     438
     439    if (vptr == NULL)
     440    {
     441        return vptr;
     442    }
     443
     444    ptr = ((psMemBlock *)vptr) - 1;
     445
     446    if (checkMemBlock(ptr, __func__))
     447    {
     448        memProblemCallback(ptr, __func__, __LINE__);
     449    }
     450
     451    pthread_mutex_lock(&ptr->refCounterMutex);
     452    ptr->refCounter++;
     453    pthread_mutex_unlock(&ptr->refCounterMutex);
     454
     455    return vptr;
     456}
     457
     458void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
     459{
     460    if (vptr == NULL)
     461    {
     462        return NULL;
     463    }
     464
    442465    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    443466
    444     if (checkMemBlock(ptr, __func__) != 0)
    445     {
    446         memProblemCallback(ptr, __func__, __LINE__);
    447     }
    448 
    449     return ptr->refCounter;
    450 }
    451 
    452 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
    453 {
    454     if (vptr == NULL)
    455     {
    456         return vptr;
    457     }
    458 
    459     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    460 
    461     if (checkMemBlock(ptr, __func__))
    462     {
    463         memProblemCallback(ptr, __func__, __LINE__);
    464     }
    465 
    466     ptr->refCounter++;
     467
     468    pthread_mutex_lock(&ptr->refCounterMutex);
     469
     470    if (ptr->refCounter > 1)
     471    {
     472        /// XXX - Probably should have another mutex here.
     473        ptr->refCounter--;          // multiple references, just decrement the count.
     474        pthread_mutex_unlock(&ptr->refCounterMutex);
     475
     476    } else
     477    {
     478        pthread_mutex_unlock(&ptr->refCounterMutex);
     479
     480        // Did the user ask to be informed about this deallocation?
     481        if (ptr->id == p_psMemFreeID) {
     482            p_psMemFreeID += memFreeCallback(ptr);
     483        }
     484
     485        pthread_mutex_lock(&memBlockListMutex);
     486
     487        // cut the memBlock out of the memBlock list
     488        if (ptr->nextBlock != NULL) {
     489            ptr->nextBlock->previousBlock = ptr->previousBlock;
     490        }
     491        if (ptr->previousBlock != NULL) {
     492            ptr->previousBlock->nextBlock = ptr->nextBlock;
     493        }
     494        if (lastMemBlockAllocated == ptr) {
     495            lastMemBlockAllocated = ptr->nextBlock;
     496        }
     497
     498        pthread_mutex_unlock(&memBlockListMutex);
     499
     500        free(ptr);
     501
     502        vptr = NULL;    // since we freed it, make sure we return NULL.
     503    }
    467504
    468505    return vptr;
    469506}
    470507
    471 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
    472 {
    473     if (vptr == NULL)
    474     {
    475         return vptr;
    476     }
    477 
    478     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    479 
    480     if (checkMemBlock(ptr, __func__))
    481     {
    482         memProblemCallback(ptr, __func__, __LINE__);
    483     }
    484 
    485     ptr->refCounter--;
    486 
    487     return vptr;
    488 }
  • trunk/psLib/src/sys/psMemory.h

    r803 r830  
    1212 *  @author Robert Lupton, Princeton University
    1313 *
    14  *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-05-28 03:17:39 $
     14 *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2004-06-02 20:03:22 $
    1616 *
    1717 *  @ingroup SystemGroup System Utilities
     
    2121
    2222#include <stdio.h>                      // needed for FILE
    23 
     23#include <pthread.h>                   // we need a mutex to make this stuff thread safe.
    2424
    2525/** @addtogroup MemoryManagement Memory Management Utilities
     
    6464    const char* file;                   ///< set from __FILE__ in e.g. p_psAlloc
    6565    const int lineno;                   ///< set from __LINE__ in e.g. p_psAlloc
    66     // TODO: refCounter should be mutexed?
     66    pthread_mutex_t   refCounterMutex;  ///< mutex to ensure exclusive access to reference counter
    6767    psReferenceCount refCounter;        ///< how many times pointer is referenced
    6868    const void* endblock;               ///< initialised to p_psMEMMAGIC
  • trunk/psLib/src/sysUtils/psMemory.c

    r803 r830  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-05-28 03:17:39 $
     10 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-06-02 20:03:22 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1616#define PS_ALLOW_MALLOC                 // we're allowed to call malloc()
    1717
    18 #include <pthread.h>                   // we need a mutex to make this stuff thread safe.
    1918#include <stdlib.h>
    2019#include <stdio.h>
     
    2524#include "psError.h"
    2625#include "psAbort.h"
     26#include "psLogMsg.h"
    2727
    2828#define P_PS_MEMMAGIC (void *)0xdeadbeef // Magic number in psMemBlock header
     
    6767        psError(__func__,
    6868                "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    69                 ptr->id, ptr->file, ptr->lineno, file, lineno);
    70     } else if (ptr->refCounter > 1) {
    71         psError(__func__,
    72                 "Block %ld allocated at %s:%d freed while still referenced at %s:%d\n",
    7369                ptr->id, ptr->file, ptr->lineno, file, lineno);
    7470    }
     
    268264    ptr->startblock = P_PS_MEMMAGIC;
    269265    ptr->endblock = P_PS_MEMMAGIC;
    270     ptr->refCounter = 1;                // one user so far
    271266    ptr->userMemorySize = size;
    272267    *(void**)((int8_t*)(ptr+1)+size) = P_PS_MEMMAGIC;
    273268    ptr->previousBlock = NULL;
     269
     270    pthread_mutex_init(&ptr->refCounterMutex, NULL);
     271    ptr->refCounter = 1;                // one user so far
    274272
    275273    // need exclusive access of the memory block list now...
     
    347345    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    348346
    349     // Did the user ask to be informed about this deallocation?
    350     if (ptr->id == p_psMemFreeID) {
    351         p_psMemFreeID += memFreeCallback(ptr);
    352     }
    353 
    354     if (checkMemBlock(ptr, "psFree") != 0) {
     347    if (checkMemBlock(ptr, __func__) != 0) {
    355348        memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    356     } else {
    357         if (ptr->refCounter > 1) {
    358             psError(__func__,"The buffer being freed is referenced elsewhere. "
    359                     "Buffer's reference count was decremented instead.");
    360             ptr->refCounter--;
    361             memProblemCallback(ptr, file, lineno);
    362         } else {
    363 
    364             // cut the memBlock out of the memBlock list
    365             pthread_mutex_lock(&memBlockListMutex);
    366             if (ptr->nextBlock != NULL) {
    367                 ptr->nextBlock->previousBlock = ptr->previousBlock;
    368             }
    369             if (ptr->previousBlock != NULL) {
    370                 ptr->previousBlock->nextBlock = ptr->nextBlock;
    371             }
    372             if (lastMemBlockAllocated == ptr) {
    373                 lastMemBlockAllocated = ptr->nextBlock;
    374             }
    375 
    376             pthread_mutex_unlock(&memBlockListMutex);
    377 
    378             free(ptr);
    379         }
    380     }
     349    }
     350
     351    psMemDecrRefCounter(vptr);          // this handles the free, if required.
    381352}
    382353
     
    397368    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    398369    {
    399         if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
     370        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    400371            nleak++;
    401372
     
    422393    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    423394    {
    424         if ( (iter->refCounter > 0) && (iter->id >= id0) ) {
     395        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    425396            (*arr)[j++] = iter;
    426397            if (j == nleak) { // found them all
     
    440411psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
    441412{
     413    psMemBlock *ptr;
     414    unsigned int refCount;
     415
     416    if (vptr == NULL)
     417    {
     418        return 0;
     419    }
     420
     421    ptr = ((psMemBlock *)vptr) - 1;
     422
     423    if (checkMemBlock(ptr, __func__) != 0)
     424    {
     425        memProblemCallback(ptr, __func__, __LINE__);
     426    }
     427
     428    pthread_mutex_lock(&ptr->refCounterMutex);
     429    refCount = ptr->refCounter;
     430    pthread_mutex_unlock(&ptr->refCounterMutex);
     431
     432    return refCount;
     433}
     434
     435void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
     436{
     437    psMemBlock *ptr;
     438
     439    if (vptr == NULL)
     440    {
     441        return vptr;
     442    }
     443
     444    ptr = ((psMemBlock *)vptr) - 1;
     445
     446    if (checkMemBlock(ptr, __func__))
     447    {
     448        memProblemCallback(ptr, __func__, __LINE__);
     449    }
     450
     451    pthread_mutex_lock(&ptr->refCounterMutex);
     452    ptr->refCounter++;
     453    pthread_mutex_unlock(&ptr->refCounterMutex);
     454
     455    return vptr;
     456}
     457
     458void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
     459{
     460    if (vptr == NULL)
     461    {
     462        return NULL;
     463    }
     464
    442465    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    443466
    444     if (checkMemBlock(ptr, __func__) != 0)
    445     {
    446         memProblemCallback(ptr, __func__, __LINE__);
    447     }
    448 
    449     return ptr->refCounter;
    450 }
    451 
    452 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
    453 {
    454     if (vptr == NULL)
    455     {
    456         return vptr;
    457     }
    458 
    459     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    460 
    461     if (checkMemBlock(ptr, __func__))
    462     {
    463         memProblemCallback(ptr, __func__, __LINE__);
    464     }
    465 
    466     ptr->refCounter++;
     467
     468    pthread_mutex_lock(&ptr->refCounterMutex);
     469
     470    if (ptr->refCounter > 1)
     471    {
     472        /// XXX - Probably should have another mutex here.
     473        ptr->refCounter--;          // multiple references, just decrement the count.
     474        pthread_mutex_unlock(&ptr->refCounterMutex);
     475
     476    } else
     477    {
     478        pthread_mutex_unlock(&ptr->refCounterMutex);
     479
     480        // Did the user ask to be informed about this deallocation?
     481        if (ptr->id == p_psMemFreeID) {
     482            p_psMemFreeID += memFreeCallback(ptr);
     483        }
     484
     485        pthread_mutex_lock(&memBlockListMutex);
     486
     487        // cut the memBlock out of the memBlock list
     488        if (ptr->nextBlock != NULL) {
     489            ptr->nextBlock->previousBlock = ptr->previousBlock;
     490        }
     491        if (ptr->previousBlock != NULL) {
     492            ptr->previousBlock->nextBlock = ptr->nextBlock;
     493        }
     494        if (lastMemBlockAllocated == ptr) {
     495            lastMemBlockAllocated = ptr->nextBlock;
     496        }
     497
     498        pthread_mutex_unlock(&memBlockListMutex);
     499
     500        free(ptr);
     501
     502        vptr = NULL;    // since we freed it, make sure we return NULL.
     503    }
    467504
    468505    return vptr;
    469506}
    470507
    471 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
    472 {
    473     if (vptr == NULL)
    474     {
    475         return vptr;
    476     }
    477 
    478     psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
    479 
    480     if (checkMemBlock(ptr, __func__))
    481     {
    482         memProblemCallback(ptr, __func__, __LINE__);
    483     }
    484 
    485     ptr->refCounter--;
    486 
    487     return vptr;
    488 }
  • trunk/psLib/src/sysUtils/psMemory.h

    r803 r830  
    1212 *  @author Robert Lupton, Princeton University
    1313 *
    14  *  @version $Revision: 1.14 $ $Name: not supported by cvs2svn $
    15  *  @date $Date: 2004-05-28 03:17:39 $
     14 *  @version $Revision: 1.15 $ $Name: not supported by cvs2svn $
     15 *  @date $Date: 2004-06-02 20:03:22 $
    1616 *
    1717 *  @ingroup SystemGroup System Utilities
     
    2121
    2222#include <stdio.h>                      // needed for FILE
    23 
     23#include <pthread.h>                   // we need a mutex to make this stuff thread safe.
    2424
    2525/** @addtogroup MemoryManagement Memory Management Utilities
     
    6464    const char* file;                   ///< set from __FILE__ in e.g. p_psAlloc
    6565    const int lineno;                   ///< set from __LINE__ in e.g. p_psAlloc
    66     // TODO: refCounter should be mutexed?
     66    pthread_mutex_t   refCounterMutex;  ///< mutex to ensure exclusive access to reference counter
    6767    psReferenceCount refCounter;        ///< how many times pointer is referenced
    6868    const void* endblock;               ///< initialised to p_psMEMMAGIC
Note: See TracChangeset for help on using the changeset viewer.