IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 23, 2004, 1:00:17 PM (22 years ago)
Author:
desonia
Message:

Changed the means of deallocation of memory. ps_Alloc now registers an optional free function to handle any additional processing of a memory object.

File:
1 edited

Legend:

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

    r876 r1073  
    88 *  @author Robert Lupton, Princeton University
    99 *
    10  *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
    11  *  @date $Date: 2004-06-04 23:46:48 $
     10 *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-06-23 23:00:15 $
    1212 *
    1313 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3030static int checkMemBlock(const psMemBlock *m, const char* funcName);
    3131static psMemBlock *lastMemBlockAllocated = NULL;
    32 pthread_mutex_t  memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
    33 pthread_mutex_t  memIdMutex = PTHREAD_MUTEX_INITIALIZER;
     32static pthread_mutex_t memBlockListMutex = PTHREAD_MUTEX_INITIALIZER;
     33static pthread_mutex_t memIdMutex = PTHREAD_MUTEX_INITIALIZER;
    3434
    3535/**
     
    9696psMemoryId p_psMemFreeID = 0;   // notify user this block is freed
    9797
    98 psMemoryId psMemAllocateCallbackSetID(psMemoryId id) // set p_psMemAllocateID to id
     98psMemoryId psMemAllocateCallbackSetID(psMemoryId id)
    9999{
    100100    psMemoryId old = p_psMemAllocateID;
     
    104104}
    105105
    106 psMemoryId psMemFreeCallbackSetID(psMemoryId id)  // set p_psMemFreeID to id
     106psMemoryId psMemFreeCallbackSetID(psMemoryId id)
    107107{
    108108    psMemoryId old = p_psMemFreeID;
     
    184184#define ALIGNED(P) ((void *)((long)(P) & ~03) == (P))
    185185
    186 static int
    187 checkMemBlock(const psMemBlock *m,
    188               const char* funcName)
     186static int checkMemBlock(const psMemBlock *m, const char* funcName)
    189187{
    190188    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
     
    260258
    261259    ptr->file = file;
     260    ptr->freeFcn = NULL;
    262261    *(unsigned int*)&ptr->lineno = lineno;
    263262    ptr->startblock = P_PS_MEMMAGIC;
     
    346345    if (checkMemBlock(ptr, __func__) != 0) {
    347346        memProblemCallback(ptr, file, lineno); // we may not own this block; don't free it
    348     }
    349 
    350     psMemDecrRefCounter(vptr);          // this handles the free, if required.
     347        return;
     348    }
     349
     350    (void)psMemDecrRefCounter(vptr);   // this handles the free, if required.
    351351}
    352352
     
    354354 * Check for memory leaks. Not production quality code
    355355 */
    356 int psMemCheckLeaks(
    357     psMemoryId id0,                     // don't list blocks with id < id0
    358     psMemBlock ***arr,                  // pointer to array of pointers to leaked blocks, or NULL
    359     FILE *fd)                           // print list of leaks to fd (or NULL)
     356int psMemCheckLeaks(psMemoryId id0,psMemBlock ***arr,FILE *fd)
    360357{
    361358    int nleak = 0;
     
    365362    pthread_mutex_lock(&memBlockListMutex);
    366363
    367     for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    368     {
     364    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) {
    369365        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    370366            nleak++;
     
    382378    pthread_mutex_unlock(&memBlockListMutex);
    383379
    384     if (nleak == 0 || arr == NULL)
    385     {
     380    if (nleak == 0 || arr == NULL) {
    386381        return nleak;
    387382    }
     
    390385    pthread_mutex_lock(&memBlockListMutex);
    391386
    392     for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock)
    393     {
     387    for (psMemBlock* iter = topBlock; iter != NULL; iter=iter->nextBlock) {
    394388        if ( (psMemGetRefCounter(iter+1) > 0) && (iter->id >= id0) ) {
    395389            (*arr)[j++] = iter;
     
    408402 * Reference counting APIs
    409403 */
    410 psReferenceCount psMemGetRefCounter(void *vptr) // return refCounter
     404// return refCounter
     405psReferenceCount psMemGetRefCounter(void *vptr)
    411406{
    412407    psMemBlock *ptr;
    413408    unsigned int refCount;
    414409
    415     if (vptr == NULL)
    416     {
     410    if (vptr == NULL) {
    417411        return 0;
    418412    }
     
    420414    ptr = ((psMemBlock *)vptr) - 1;
    421415
    422     if (checkMemBlock(ptr, __func__) != 0)
    423     {
     416    if (checkMemBlock(ptr, __func__) != 0) {
    424417        memProblemCallback(ptr, __func__, __LINE__);
    425418    }
     
    431424    return refCount;
    432425}
    433 
    434 void *psMemIncrRefCounter(void *vptr) // increment and return refCounter
     426// increment and return refCounter
     427void *psMemIncrRefCounter(void *vptr)
    435428{
    436429    psMemBlock *ptr;
    437430
    438     if (vptr == NULL)
    439     {
     431    if (vptr == NULL) {
    440432        return vptr;
    441433    }
     
    443435    ptr = ((psMemBlock *)vptr) - 1;
    444436
    445     if (checkMemBlock(ptr, __func__))
    446     {
     437    if (checkMemBlock(ptr, __func__)) {
    447438        memProblemCallback(ptr, __func__, __LINE__);
    448439    }
     
    455446}
    456447
    457 void *psMemDecrRefCounter(void *vptr) // decrement and return refCounter
    458 {
    459     if (vptr == NULL)
    460     {
     448// decrement and return refCounter
     449void *psMemDecrRefCounter(void *vptr)
     450{
     451    if (vptr == NULL) {
    461452        return NULL;
    462453    }
     
    467458    pthread_mutex_lock(&ptr->refCounterMutex);
    468459
    469     if (ptr->refCounter > 1)
    470     {
     460    if (ptr->refCounter > 1) {
    471461        /// XXX - Probably should have another mutex here.
    472462        ptr->refCounter--;          // multiple references, just decrement the count.
    473463        pthread_mutex_unlock(&ptr->refCounterMutex);
    474464
    475     } else
    476     {
     465    } else {
    477466        pthread_mutex_unlock(&ptr->refCounterMutex);
    478467
     
    482471        }
    483472
     473        if (ptr->freeFcn != NULL) {
     474            ptr->freeFcn(vptr);
     475        }
     476
    484477        pthread_mutex_lock(&memBlockListMutex);
    485478
     
    507500}
    508501
    509 void p_psCustomFree(psFreeFcn fcn, void* ptr)
    510 {
    511 
    512     if (fcn == NULL) {
     502void p_psMemSetDeallocator(void* vptr, psFreeFcn freeFcn)
     503{
     504    if (vptr == NULL) {
    513505        return;
    514     } else {
    515         if (fcn == PS_FREE) {
    516             psFree(ptr);
    517         } else {
    518             fcn(ptr);
    519         }
    520     }
    521 }
     506    }
     507
     508    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
     509
     510    ptr->freeFcn = freeFcn;
     511
     512}
     513psFreeFcn p_psMemGetDeallocator(void* vptr)
     514{
     515    if (vptr == NULL) {
     516        return NULL;
     517    }
     518
     519    psMemBlock *ptr = ((psMemBlock *)vptr) - 1;
     520
     521    return ptr->freeFcn;
     522}
Note: See TracChangeset for help on using the changeset viewer.