IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 4, 2004, 1:37:39 PM (22 years ago)
Author:
desonia
Message:

found the server astyle upgrade was faulty, so the format was reset.

File:
1 edited

Legend:

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

    r1360 r1385  
    88*  @author Robert Lupton, Princeton University
    99*
    10 *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2004-07-31 02:28:10 $
     10*  @version $Revision: 1.31 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2004-08-04 23:37:39 $
    1212*
    1313*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4747        NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL
    4848    };
    49    
     49
    5050#ifdef PS_MEM_DEBUG
    5151static psMemBlock* deadBlockList;    // a place to put dead memBlocks in debug mode.
     
    6262{
    6363    void * ptr = NULL;
    64    
     64
    6565    pthread_mutex_lock( &recycleMemBlockListMutex );
    6666    int level = recycleBins - 1;
    6767    while ( level >= 0 && ptr == NULL ) {
    68             while ( recycleMemBlockList[ level ] != NULL && ptr == NULL ) {
    69                     psMemBlock * old = recycleMemBlockList[ level ];
    70                     recycleMemBlockList[ level ] = recycleMemBlockList[ level ] ->nextBlock;
    71                     free( old );
    72                     ptr = malloc( size );
    73                 }
    74             level--;
    75         }
     68        while ( recycleMemBlockList[ level ] != NULL && ptr == NULL ) {
     69            psMemBlock * old = recycleMemBlockList[ level ];
     70            recycleMemBlockList[ level ] = recycleMemBlockList[ level ] ->nextBlock;
     71            free( old );
     72            ptr = malloc( size );
     73        }
     74        level--;
     75    }
    7676    pthread_mutex_unlock( &recycleMemBlockListMutex );
    77    
     77
    7878    return ptr;
    7979}
     
    8484{
    8585    psMemExhaustedCallback old = memExhaustedCallback;
    86    
     86
    8787    if ( func != NULL ) {
    88             memExhaustedCallback = func;
    89         } else {
    90             memExhaustedCallback = memExhaustedCallbackDefault;
    91         }
    92        
     88        memExhaustedCallback = func;
     89    } else {
     90        memExhaustedCallback = memExhaustedCallbackDefault;
     91    }
     92
    9393    return old;
    9494}
     
    9898{
    9999    if ( ptr->refCounter < 1 ) {
    100             psError( __func__,
    101                      "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
    102                      ptr->id, ptr->file, ptr->lineno, file, lineno );
    103         }
    104        
     100        psError( __func__,
     101                 "Block %ld allocated at %s:%d freed more than once at %s:%d\n",
     102                 ptr->id, ptr->file, ptr->lineno, file, lineno );
     103    }
     104
    105105    if ( lineno > 0 ) {
    106             psAbort( __func__, "Detected a problem in the memory system at %s:%d", file, lineno );
    107         }
     106        psAbort( __func__, "Detected a problem in the memory system at %s:%d", file, lineno );
     107    }
    108108}
    109109static psMemProblemCallback memProblemCallback = memProblemCallbackDefault;
     
    112112{
    113113    psMemProblemCallback old = memProblemCallback;
    114    
     114
    115115    if ( func != NULL ) {
    116             memProblemCallback = func;
    117         } else {
    118             memProblemCallback = memProblemCallbackDefault;
    119         }
    120        
     116        memProblemCallback = func;
     117    } else {
     118        memProblemCallback = memProblemCallbackDefault;
     119    }
     120
    121121    return old;
    122122}
     
    133133    psMemoryId old = p_psMemAllocateID;
    134134    p_psMemAllocateID = id;
    135    
     135
    136136    return old;
    137137}
     
    141141    psMemoryId old = p_psMemFreeID;
    142142    p_psMemFreeID = id;
    143    
     143
    144144    return old;
    145145}
     
    154154{
    155155    static psMemoryId incr = 0;  // "p_psMemAllocateID += incr"
    156    
     156
    157157    return incr;
    158158}
     
    161161{
    162162    static psMemoryId incr = 0;  // "p_psMemFreeID += incr"
    163    
     163
    164164    return incr;
    165165}
     
    174174{
    175175    psMemFreeCallback old = memAllocateCallback;
    176    
     176
    177177    if ( func != NULL ) {
    178             memAllocateCallback = func;
    179         } else {
    180             memAllocateCallback = memAllocateCallbackDefault;
    181         }
    182        
     178        memAllocateCallback = func;
     179    } else {
     180        memAllocateCallback = memAllocateCallbackDefault;
     181    }
     182
    183183    return old;
    184184}
     
    187187{
    188188    psMemFreeCallback old = memFreeCallback;
    189    
     189
    190190    if ( func != NULL ) {
    191             memFreeCallback = func;
    192         } else {
    193             memFreeCallback = memFreeCallbackDefault;
    194         }
    195        
     191        memFreeCallback = func;
     192    } else {
     193        memFreeCallback = memFreeCallbackDefault;
     194    }
     195
    196196    return old;
    197197}
     
    206206    id = memid + 1;
    207207    pthread_mutex_unlock( &memIdMutex );
    208    
     208
    209209    return id;
    210210}
     
    220220    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
    221221    // we shouldn't call such things as p_psAlloc/p_psFree here.
    222    
     222
    223223    if ( m == NULL ) {
    224             psError( funcName, "Memory Corruption: NULL memory block found." );
    225             return 1;
    226         }
    227        
     224        psError( funcName, "Memory Corruption: NULL memory block found." );
     225        return 1;
     226    }
     227
    228228    if ( m->refCounter == 0 ) {
    229             // using an unreferenced block of memory, are you?
    230             psError( __func__, "Memory Corruption: memory block %ld was freed but still used.",
    231                      m->id );
    232             return 1;
    233         }
    234        
     229        // using an unreferenced block of memory, are you?
     230        psError( __func__, "Memory Corruption: memory block %ld was freed but still used.",
     231                 m->id );
     232        return 1;
     233    }
     234
    235235    if ( m->startblock != P_PS_MEMMAGIC || m->endblock != P_PS_MEMMAGIC ) {
    236             psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)",
    237                      m->id );
    238             return 1;
    239         }
     236        psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer underflow)",
     237                 m->id );
     238        return 1;
     239    }
    240240    if ( *( void** ) ( ( int8_t* ) ( m + 1 ) + m->userMemorySize ) != P_PS_MEMMAGIC ) {
    241             psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)",
    242                      m->id );
    243             return 1;
    244         }
    245        
     241        psError( funcName, "Memory Corruption: memory block %ld is corrupted (buffer overflow)",
     242                 m->id );
     243        return 1;
     244    }
     245
    246246    return 0;
    247247}
     
    250250{
    251251    int nbad = 0;                       // number of bad blocks
    252    
     252
    253253    // get exclusive access to the memBlock list to avoid it changing on us while we use it.
    254254    pthread_mutex_lock( &memBlockListMutex );
    255    
     255
    256256    for ( psMemBlock * iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock ) {
    257             if ( checkMemBlock( iter, __func__ ) ) {
    258                     nbad++;
    259                    
    260                     memProblemCallback( iter, __func__, __LINE__ );
    261                    
    262                     if ( abort_on_error ) {
    263                             // release the lock on the memblock list
    264                             pthread_mutex_unlock( &memBlockListMutex );
    265                             psAbort( __func__, "Detected memory corruption" );
    266                             return nbad;
    267                         }
    268                 }
    269         }
    270        
     257        if ( checkMemBlock( iter, __func__ ) ) {
     258            nbad++;
     259
     260            memProblemCallback( iter, __func__, __LINE__ );
     261
     262            if ( abort_on_error ) {
     263                // release the lock on the memblock list
     264                pthread_mutex_unlock( &memBlockListMutex );
     265                psAbort( __func__, "Detected memory corruption" );
     266                return nbad;
     267            }
     268        }
     269    }
     270
    271271    // release the lock on the memblock list
    272272    pthread_mutex_unlock( &memBlockListMutex );
     
    278278
    279279    psMemBlock * ptr = NULL;
    280    
     280
    281281    // memory is of the size I want to bother recycling?
    282282    if ( size < P_PS_LARGE_BLOCK_SIZE ) {
    283             // find the bin we need.
    284             int level = 0;
    285             while ( size > recycleBinSize[ level ] ) {
    286                     level++;
     283        // find the bin we need.
     284        int level = 0;
     285        while ( size > recycleBinSize[ level ] ) {
     286            level++;
     287        }
     288        // Are we in one of the bins
     289        if ( level < recycleBins ) {
     290
     291            size = recycleBinSize[ level ];  // round-up size to next sized bin.
     292
     293            pthread_mutex_lock( &recycleMemBlockListMutex );
     294
     295            if ( recycleMemBlockList[ level ] != NULL ) {
     296                ptr = recycleMemBlockList[ level ];
     297                recycleMemBlockList[ level ] = ptr->nextBlock;
     298                if ( recycleMemBlockList[ level ] != NULL ) {
     299                    recycleMemBlockList[ level ] ->previousBlock = NULL;
    287300                }
    288             // Are we in one of the bins
    289             if ( level < recycleBins ) {
    290            
    291                     size = recycleBinSize[ level ];  // round-up size to next sized bin.
    292                    
    293                     pthread_mutex_lock( &recycleMemBlockListMutex );
    294                    
    295                     if ( recycleMemBlockList[ level ] != NULL ) {
    296                             ptr = recycleMemBlockList[ level ];
    297                             recycleMemBlockList[ level ] = ptr->nextBlock;
    298                             if ( recycleMemBlockList[ level ] != NULL ) {
    299                                     recycleMemBlockList[ level ] ->previousBlock = NULL;
    300                                 }
    301                             size = ptr->userMemorySize;
    302                         }
    303                        
    304                     pthread_mutex_unlock( &recycleMemBlockListMutex );
    305                 }
    306         }
    307        
     301                size = ptr->userMemorySize;
     302            }
     303
     304            pthread_mutex_unlock( &recycleMemBlockListMutex );
     305        }
     306    }
     307
    308308    if ( ptr == NULL ) {
    309             ptr = malloc( sizeof( psMemBlock ) + size + sizeof( void* ) );
    310            
     309        ptr = malloc( sizeof( psMemBlock ) + size + sizeof( void* ) );
     310
     311        if ( ptr == NULL ) {
     312            ptr = memExhaustedCallback( size );
    311313            if ( ptr == NULL ) {
    312                     ptr = memExhaustedCallback( size );
    313                     if ( ptr == NULL ) {
    314                             psAbort( __func__, "Failed to allocate %u bytes at %s:%d",
    315                                      size, file, lineno );
    316                         }
    317                 }
    318                
    319             ptr->startblock = P_PS_MEMMAGIC;
    320             ptr->endblock = P_PS_MEMMAGIC;
    321             ptr->userMemorySize = size;
    322             pthread_mutex_init( &ptr->refCounterMutex, NULL );
    323         }
    324        
     314                psAbort( __func__, "Failed to allocate %u bytes at %s:%d",
     315                         size, file, lineno );
     316            }
     317        }
     318
     319        ptr->startblock = P_PS_MEMMAGIC;
     320        ptr->endblock = P_PS_MEMMAGIC;
     321        ptr->userMemorySize = size;
     322        pthread_mutex_init( &ptr->refCounterMutex, NULL );
     323    }
     324
    325325    // increment the memory id safely.
    326326    pthread_mutex_lock( &memBlockListMutex );
    327327    *( psMemoryId* ) & ptr->id = ++memid;
    328328    pthread_mutex_unlock( &memBlockListMutex );
    329    
     329
    330330    ptr->file = file;
    331331    ptr->freeFcn = NULL;
     
    333333    *( void** ) ( ( int8_t* ) ( ptr + 1 ) + size ) = P_PS_MEMMAGIC;
    334334    ptr->previousBlock = NULL;
    335    
     335
    336336    ptr->refCounter = 1;                // one user so far
    337    
     337
    338338    // need exclusive access of the memory block list now...
    339339    pthread_mutex_lock( &memBlockListMutex );
    340    
     340
    341341    // insert the new block to the front of the memBlock linked-list
    342342    ptr->nextBlock = lastMemBlockAllocated;
    343343    if ( ptr->nextBlock != NULL ) {
    344             ptr->nextBlock->previousBlock = ptr;
    345         }
     344        ptr->nextBlock->previousBlock = ptr;
     345    }
    346346    lastMemBlockAllocated = ptr;
    347    
     347
    348348    pthread_mutex_unlock( &memBlockListMutex );
    349    
     349
    350350    //  Did the user ask to be informed about this allocation?
    351351    if ( ptr->id == p_psMemAllocateID ) {
    352             p_psMemAllocateID += memAllocateCallback( ptr );
    353         }
    354        
     352        p_psMemAllocateID += memAllocateCallback( ptr );
     353    }
     354
    355355    // And return the user the memory that they allocated
    356356    return ptr + 1;   // user memory
     
    360360{
    361361    if ( vptr == NULL ) {
    362             return p_psAlloc( size, file, lineno );
    363         } else {
    364             psMemBlock *ptr = ( ( psMemBlock * ) vptr ) - 1;
    365             bool isBlockLast = false;
    366            
    367             if ( checkMemBlock( ptr, __func__ ) != 0 ) {
    368                     memProblemCallback( ptr, file, lineno );
    369                     psAbort( file, "Realloc detected a memory corruption (id %ld @ %s:%d).",
    370                              ptr->id, ptr->file, ptr->lineno );
    371                 }
    372                
    373             pthread_mutex_lock( &memBlockListMutex );
    374            
    375             isBlockLast = ( ptr == lastMemBlockAllocated );
    376            
    377             ptr = ( psMemBlock* ) realloc( ptr, sizeof( psMemBlock ) + size + sizeof( void* ) );
    378            
    379             if ( ptr == NULL ) {
    380                     psAbort( __func__, "Failed to reallocate %ld bytes at %s:%d",
    381                              size, file, lineno );
    382                 }
    383                
    384             ptr->userMemorySize = size;
    385             *( void** ) ( ( int8_t* ) ( ptr + 1 ) + size ) = P_PS_MEMMAGIC;
    386            
    387             if ( isBlockLast ) {
    388                     lastMemBlockAllocated = ptr;
    389                 }
    390                
    391             // the block location may have changed, so fix the linked list addresses.
    392             if ( ptr->nextBlock != NULL ) {
    393                     ptr->nextBlock->previousBlock = ptr;
    394                 }
    395             if ( ptr->previousBlock != NULL ) {
    396                     ptr->previousBlock->nextBlock = ptr;
    397                 }
    398                
    399             pthread_mutex_unlock( &memBlockListMutex );
    400            
    401             //  Did the user ask to be informed about this allocation?
    402             if ( ptr->id == p_psMemAllocateID ) {
    403                     p_psMemAllocateID += memAllocateCallback( ptr );
    404                 }
    405                
    406             return ptr + 1;   // usr memory
    407         }
     362        return p_psAlloc( size, file, lineno );
     363    } else {
     364        psMemBlock *ptr = ( ( psMemBlock * ) vptr ) - 1;
     365        bool isBlockLast = false;
     366
     367        if ( checkMemBlock( ptr, __func__ ) != 0 ) {
     368            memProblemCallback( ptr, file, lineno );
     369            psAbort( file, "Realloc detected a memory corruption (id %ld @ %s:%d).",
     370                     ptr->id, ptr->file, ptr->lineno );
     371        }
     372
     373        pthread_mutex_lock( &memBlockListMutex );
     374
     375        isBlockLast = ( ptr == lastMemBlockAllocated );
     376
     377        ptr = ( psMemBlock* ) realloc( ptr, sizeof( psMemBlock ) + size + sizeof( void* ) );
     378
     379        if ( ptr == NULL ) {
     380            psAbort( __func__, "Failed to reallocate %ld bytes at %s:%d",
     381                     size, file, lineno );
     382        }
     383
     384        ptr->userMemorySize = size;
     385        *( void** ) ( ( int8_t* ) ( ptr + 1 ) + size ) = P_PS_MEMMAGIC;
     386
     387        if ( isBlockLast ) {
     388            lastMemBlockAllocated = ptr;
     389        }
     390
     391        // the block location may have changed, so fix the linked list addresses.
     392        if ( ptr->nextBlock != NULL ) {
     393            ptr->nextBlock->previousBlock = ptr;
     394        }
     395        if ( ptr->previousBlock != NULL ) {
     396            ptr->previousBlock->nextBlock = ptr;
     397        }
     398
     399        pthread_mutex_unlock( &memBlockListMutex );
     400
     401        //  Did the user ask to be informed about this allocation?
     402        if ( ptr->id == p_psMemAllocateID ) {
     403            p_psMemAllocateID += memAllocateCallback( ptr );
     404        }
     405
     406        return ptr + 1;   // usr memory
     407    }
    408408}
    409409
     
    421421    int j = 0;
    422422    psMemBlock* topBlock = lastMemBlockAllocated;
    423    
     423
    424424    pthread_mutex_lock( &memBlockListMutex );
    425    
     425
    426426    for ( psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock ) {
    427             if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 ) ) {
    428                     nleak++;
    429                    
    430                     if ( fd != NULL ) {
    431                             if ( nleak == 1 ) {
    432                                     fprintf( fd, "   %20s:line ID\n", "file" );
    433                                 }
    434                                
    435                             fprintf( fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id );
    436                         }
     427        if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 ) ) {
     428            nleak++;
     429
     430            if ( fd != NULL ) {
     431                if ( nleak == 1 ) {
     432                    fprintf( fd, "   %20s:line ID\n", "file" );
    437433                }
    438         }
    439        
     434
     435                fprintf( fd, "   %20s:%-4d %ld\n", iter->file, iter->lineno, iter->id );
     436            }
     437        }
     438    }
     439
    440440    pthread_mutex_unlock( &memBlockListMutex );
    441    
     441
    442442    if ( nleak == 0 || arr == NULL ) {
    443             return nleak;
    444         }
    445        
     443        return nleak;
     444    }
     445
    446446    *arr = p_psAlloc( nleak * sizeof( psMemBlock ), __FILE__, __LINE__ );
    447447    pthread_mutex_lock( &memBlockListMutex );
    448    
     448
    449449    for ( psMemBlock * iter = topBlock; iter != NULL; iter = iter->nextBlock ) {
    450             if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 ) ) {
    451                     ( *arr ) [ j++ ] = iter;
    452                     if ( j == nleak ) { // found them all
    453                             break;
    454                         }
    455                 }
    456         }
    457        
     450        if ( ( psMemGetRefCounter( iter + 1 ) > 0 ) && ( iter->id >= id0 ) ) {
     451            ( *arr ) [ j++ ] = iter;
     452            if ( j == nleak ) { // found them all
     453                break;
     454            }
     455        }
     456    }
     457
    458458    pthread_mutex_unlock( &memBlockListMutex );
    459    
     459
    460460    return nleak;
    461461}
     
    463463/*
    464464 * Reference counting APIs
    465  */ 
     465 */
    466466// return refCounter
    467467psReferenceCount psMemGetRefCounter( void *vptr )
     
    469469    psMemBlock * ptr;
    470470    unsigned int refCount;
    471    
     471
    472472    if ( vptr == NULL ) {
    473             return 0;
    474         }
    475        
     473        return 0;
     474    }
     475
    476476    ptr = ( ( psMemBlock * ) vptr ) - 1;
    477    
     477
    478478    if ( checkMemBlock( ptr, __func__ ) != 0 ) {
    479             memProblemCallback( ptr, __func__, __LINE__ );
    480         }
    481        
     479        memProblemCallback( ptr, __func__, __LINE__ );
     480    }
     481
    482482    pthread_mutex_lock( &ptr->refCounterMutex );
    483483    refCount = ptr->refCounter;
    484484    pthread_mutex_unlock( &ptr->refCounterMutex );
    485    
     485
    486486    return refCount;
    487487}
     
    490490{
    491491    psMemBlock * ptr;
    492    
     492
    493493    if ( vptr == NULL ) {
    494             return vptr;
    495         }
    496        
     494        return vptr;
     495    }
     496
    497497    ptr = ( ( psMemBlock * ) vptr ) - 1;
    498    
     498
    499499    if ( checkMemBlock( ptr, __func__ ) ) {
    500             memProblemCallback( ptr, file, lineno );
    501         }
    502        
     500        memProblemCallback( ptr, file, lineno );
     501    }
     502
    503503    pthread_mutex_lock( &ptr->refCounterMutex );
    504504    ptr->refCounter++;
    505505    pthread_mutex_unlock( &ptr->refCounterMutex );
    506    
     506
    507507    return vptr;
    508508}
     
    512512{
    513513    if ( vptr == NULL ) {
    514             return NULL;
    515         }
    516        
     514        return NULL;
     515    }
     516
    517517    psMemBlock *ptr = ( ( psMemBlock * ) vptr ) - 1;
    518    
     518
    519519    if ( checkMemBlock( ptr, __func__ ) != 0 ) {
    520             memProblemCallback( ptr, file, lineno );
    521             return NULL;
    522         }
    523        
     520        memProblemCallback( ptr, file, lineno );
     521        return NULL;
     522    }
     523
    524524    pthread_mutex_lock( &ptr->refCounterMutex );
    525    
     525
    526526    if ( ptr->refCounter > 1 ) {
    527             /// XXX - Probably should have another mutex here.
    528             ptr->refCounter--;          // multiple references, just decrement the count.
    529             pthread_mutex_unlock( &ptr->refCounterMutex );
    530            
     527        /// XXX - Probably should have another mutex here.
     528        ptr->refCounter--;          // multiple references, just decrement the count.
     529        pthread_mutex_unlock( &ptr->refCounterMutex );
     530
     531    } else {
     532        pthread_mutex_unlock( &ptr->refCounterMutex );
     533
     534        // Did the user ask to be informed about this deallocation?
     535        if ( ptr->id == p_psMemFreeID ) {
     536            p_psMemFreeID += memFreeCallback( ptr );
     537        }
     538
     539        if ( ptr->freeFcn != NULL ) {
     540            ptr->freeFcn( vptr );
     541        }
     542
     543        pthread_mutex_lock( &memBlockListMutex );
     544
     545        // cut the memBlock out of the memBlock list
     546        if ( ptr->nextBlock != NULL ) {
     547            ptr->nextBlock->previousBlock = ptr->previousBlock;
     548        }
     549        if ( ptr->previousBlock != NULL ) {
     550            ptr->previousBlock->nextBlock = ptr->nextBlock;
     551        }
     552        if ( lastMemBlockAllocated == ptr ) {
     553            lastMemBlockAllocated = ptr->nextBlock;
     554        }
     555
     556        pthread_mutex_unlock( &memBlockListMutex );
     557
     558
     559        // do we need to recycle?
     560        if ( ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE ) {
     561
     562            int level = 1;
     563            while ( ptr->userMemorySize >= recycleBinSize[ level ] ) {
     564                level++;
     565            }
     566            level--;
     567
     568            ptr->refCounter = 0;
     569            ptr->previousBlock = NULL;
     570
     571            pthread_mutex_lock( &recycleMemBlockListMutex );
     572            ptr->nextBlock = recycleMemBlockList[ level ];
     573            if ( recycleMemBlockList[ level ] != NULL ) {
     574                recycleMemBlockList[ level ] ->previousBlock = ptr;
     575            }
     576            recycleMemBlockList[ level ] = ptr;
     577            pthread_mutex_unlock( &recycleMemBlockListMutex );
     578
    531579        } else {
    532             pthread_mutex_unlock( &ptr->refCounterMutex );
    533            
    534             // Did the user ask to be informed about this deallocation?
    535             if ( ptr->id == p_psMemFreeID ) {
    536                     p_psMemFreeID += memFreeCallback( ptr );
    537                 }
    538                
    539             if ( ptr->freeFcn != NULL ) {
    540                     ptr->freeFcn( vptr );
    541                 }
    542                
    543             pthread_mutex_lock( &memBlockListMutex );
    544            
    545             // cut the memBlock out of the memBlock list
    546             if ( ptr->nextBlock != NULL ) {
    547                     ptr->nextBlock->previousBlock = ptr->previousBlock;
    548                 }
    549             if ( ptr->previousBlock != NULL ) {
    550                     ptr->previousBlock->nextBlock = ptr->nextBlock;
    551                 }
    552             if ( lastMemBlockAllocated == ptr ) {
    553                     lastMemBlockAllocated = ptr->nextBlock;
    554                 }
    555                
    556             pthread_mutex_unlock( &memBlockListMutex );
    557            
    558            
    559             // do we need to recycle?
    560             if ( ptr->userMemorySize < P_PS_LARGE_BLOCK_SIZE ) {
    561            
    562                     int level = 1;
    563                     while ( ptr->userMemorySize >= recycleBinSize[ level ] ) {
    564                             level++;
    565                         }
    566                     level--;
    567                    
    568                     ptr->refCounter = 0;
    569                     ptr->previousBlock = NULL;
    570                    
    571                     pthread_mutex_lock( &recycleMemBlockListMutex );
    572                     ptr->nextBlock = recycleMemBlockList[ level ];
    573                     if ( recycleMemBlockList[ level ] != NULL ) {
    574                             recycleMemBlockList[ level ] ->previousBlock = ptr;
    575                         }
    576                     recycleMemBlockList[ level ] = ptr;
    577                     pthread_mutex_unlock( &recycleMemBlockListMutex );
    578                    
    579                 } else {
    580                     // memory is larger than I want to recycle.
    581                     #ifdef PS_MEM_DEBUG
    582                     ( void ) p_psRealloc( vptr, 0, file, lineno );
    583                     ptr->previousBlock = NULL;
    584                     ptr->nextBlock = deadBlockList;
    585                     if ( deadBlockList != NULL ) {
    586                             deadBlockList->previous = ptr;
    587                         }
    588                     deadBlockList = ptr;
    589                     #else
    590                    
    591                     pthread_mutex_destroy( &ptr->refCounterMutex );
    592                     free( ptr );
    593                     #endif
    594                    
    595                 }
    596                
    597             vptr = NULL;    // since we freed it, make sure we return NULL.
    598         }
    599        
     580            // memory is larger than I want to recycle.
     581            #ifdef PS_MEM_DEBUG
     582            ( void ) p_psRealloc( vptr, 0, file, lineno );
     583            ptr->previousBlock = NULL;
     584            ptr->nextBlock = deadBlockList;
     585            if ( deadBlockList != NULL ) {
     586                deadBlockList->previous = ptr;
     587            }
     588            deadBlockList = ptr;
     589            #else
     590
     591            pthread_mutex_destroy( &ptr->refCounterMutex );
     592            free( ptr );
     593            #endif
     594
     595        }
     596
     597        vptr = NULL;    // since we freed it, make sure we return NULL.
     598    }
     599
    600600    return vptr;
    601601}
     
    604604{
    605605    if ( vptr == NULL ) {
    606             return ;
    607         }
    608        
     606        return ;
     607    }
     608
    609609    psMemBlock *ptr = ( ( psMemBlock * ) vptr ) - 1;
    610    
     610
    611611    if ( checkMemBlock( ptr, __func__ ) != 0 ) {
    612             memProblemCallback( ptr, __func__, __LINE__ );
    613         }
    614        
     612        memProblemCallback( ptr, __func__, __LINE__ );
     613    }
     614
    615615    ptr->freeFcn = freeFcn;
    616    
     616
    617617}
    618618psFreeFcn p_psMemGetDeallocator( void* vptr )
    619619{
    620620    if ( vptr == NULL ) {
    621             return NULL;
    622         }
    623        
     621        return NULL;
     622    }
     623
    624624    psMemBlock *ptr = ( ( psMemBlock * ) vptr ) - 1;
    625    
     625
    626626    if ( checkMemBlock( ptr, __func__ ) != 0 ) {
    627             memProblemCallback( ptr, __func__, __LINE__ );
    628         }
    629        
     627        memProblemCallback( ptr, __func__, __LINE__ );
     628    }
     629
    630630    return ptr->freeFcn;
    631631}
Note: See TracChangeset for help on using the changeset viewer.