IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 8809


Ignore:
Timestamp:
Sep 13, 2006, 11:14:48 AM (20 years ago)
Author:
jhoblitt
Message:

rollback 1.83 (accidental checkin)

File:
1 edited

Legend:

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

    r8808 r8809  
    88*  @author Robert Lupton, Princeton University
    99*
    10 *  @version $Revision: 1.83 $ $Name: not supported by cvs2svn $
    11 *  @date $Date: 2006-09-13 21:11:11 $
     10*  @version $Revision: 1.84 $ $Name: not supported by cvs2svn $
     11*  @date $Date: 2006-09-13 21:14:48 $
    1212*
    1313*  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    3838#define P_PS_MEMMAGIC (psPtr )0xdeadbeef   // Magic number in psMemBlock header
    3939
     40#define P_PS_LARGE_BLOCK_SIZE 65536        // size where under, we try to recycle
    4041
    4142static psS32 checkMemBlock(const psMemBlock* m, const char *funcName);
     
    5051static bool memory_is_persistent = false;
    5152
     53#ifdef PS_MEM_USE_RECYCLE               // Only use recycling if this is set
     54#define N_RECYCLE_BINS 14               // number of recycle bins
     55#define MAX_RECYCLE 100                 // Maximum number permitted in a recycle bin
     56static pthread_mutex_t recycleMemBlockListMutex = PTHREAD_MUTEX_INITIALIZER; // Mutex for recycle bins
     57static const psS32 recycleBinSize[N_RECYCLE_BINS] = // Size of each bin
     58    {
     59        8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, P_PS_LARGE_BLOCK_SIZE
     60    };
     61// N.B. recycleBinSize should be terminated by P_PS_LARGE_BLOCK_SIZE (simplifies search loops)
     62static psS32 recycleBinNums[N_RECYCLE_BINS] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Number in each bin
     63static psMemBlock* recycleMemBlockList[N_RECYCLE_BINS] = // Contents of the bins
     64    { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };
     65#endif // #ifdef PS_MEM_USE_RECYCLE
     66
    5267#ifdef PS_MEM_DEBUG
    5368static psMemBlock* deadBlockList;       // a place to put dead memBlocks in debug mode.
     
    5873 */
    5974static psMemId memid = 0;
     75
     76/**
     77 *  Default memExhausted callback.
     78 */
     79static psPtr memExhaustedCallbackDefault(size_t size)
     80{
     81    #if PS_MEM_USE_RECYCLE
     82    psPtr ptr = NULL;
     83    if (safeThreads) {
     84        pthread_mutex_lock(&recycleMemBlockListMutex);
     85    }
     86    // Attempt to free up everything I can find so I can alloc my ptr
     87    int bin = N_RECYCLE_BINS - 1;       // Recycle bin
     88
     89    while (bin >= 0 && ptr == NULL) {
     90        while (recycleMemBlockList[bin] != NULL && ptr == NULL) {
     91            psMemBlock *old = recycleMemBlockList[bin];
     92            recycleMemBlockList[bin] = recycleMemBlockList[bin]->nextBlock;
     93            free(old);
     94            ptr = malloc(size);
     95        }
     96        bin--;
     97    }
     98
     99    if (safeThreads) {
     100        pthread_mutex_unlock(&recycleMemBlockListMutex);
     101    }
     102    return ptr;
     103    #else  // #ifdef PS_MEM_USE_RECYCLE
     104
     105    return NULL;
     106    #endif // #ifdef PS_MEM_USE_RECYCLE
     107}
    60108
    61109/*
     
    102150                           const char *funcName)
    103151{
    104     // n.b. since this is called by psMemCheckCorruption while the memblock
    105     // list is mutex locked, we shouldn't call such things as
    106     // p_psAlloc/p_psFree here.
     152    // n.b. since this is called by psMemCheckCorruption while the memblock list is mutex locked,
     153    // we shouldn't call such things as p_psAlloc/p_psFree here.
    107154
    108155    if (m == NULL) {
     
    142189static psMemFreeCallback memFreeCallback = memFreeCallbackDefault;
    143190static psMemProblemCallback memProblemCallback = memProblemCallbackDefault;
     191static psMemExhaustedCallback memExhaustedCallback = memExhaustedCallbackDefault;
     192
     193psMemExhaustedCallback psMemExhaustedCallbackSet(psMemExhaustedCallback func)
     194{
     195    psMemExhaustedCallback old = memExhaustedCallback;
     196
     197    if (func != NULL) {
     198        memExhaustedCallback = func;
     199    } else {
     200        memExhaustedCallback = memExhaustedCallbackDefault;
     201    }
     202
     203    return old;
     204}
    144205
    145206psMemProblemCallback psMemProblemCallbackSet(psMemProblemCallback func)
     
    216277
    217278    if (safeThreads) {
    218         // LOCK: id
    219279        pthread_mutex_lock(&memIdMutex);
    220280    }
     
    223283
    224284    if (safeThreads) {
    225         // UNLOCK: id
    226285        pthread_mutex_unlock(&memIdMutex);
    227286    }
     
    232291psMemId psMemGetLastId(void)
    233292{
    234     psMemId id;
    235 
    236     if (safeThreads) {
    237         // LOCK: id
    238         pthread_mutex_lock(&memIdMutex);
    239     }
    240 
    241     id = memid;
    242 
    243     if (safeThreads) {
    244         // UNLOCK: id
    245         pthread_mutex_unlock(&memIdMutex);
    246     }
    247 
    248     return id;
     293    return memid;
    249294}
    250295
     
    252297{
    253298    psS32 nbad = 0;               // number of bad blocks
    254 
    255     // get exclusive access to the memBlock list to avoid it changing on us
    256     // while we use it.
    257     if (safeThreads) {
    258         // LOCK: block list
    259         pthread_mutex_unlock(&memBlockListMutex);
    260     }
     299    psBool failure = false;
     300
     301    // get exclusive access to the memBlock list to avoid it changing on us while we use it.
     302    //    pthread_mutex_lock(&memBlockListMutex);
    261303
    262304    for (psMemBlock* iter = lastMemBlockAllocated; iter != NULL; iter = iter->nextBlock) {
    263         if (checkMemBlock(iter, __func__)) {
     305        if (safeThreads) {
     306            pthread_mutex_unlock(&memBlockListMutex);
     307        }
     308        failure = checkMemBlock(iter, __func__);
     309        if (safeThreads) {
     310            pthread_mutex_lock(&memBlockListMutex);
     311        }
     312        if ( failure ) {
    264313            nbad++;
    265314
     
    267316
    268317            if (abort_on_error) {
     318                // release the lock on the memblock list
    269319                if (safeThreads) {
    270                     // UNLOCK: block list
    271320                    pthread_mutex_unlock(&memBlockListMutex);
    272321                }
     
    277326    }
    278327
    279     if (safeThreads) {
    280         // UNLOCK: block list
     328    // release the lock on the memblock list
     329    if (safeThreads) {
    281330        pthread_mutex_unlock(&memBlockListMutex);
    282331    }
    283 
    284332    return nbad;
    285333}
     
    379427    psMemBlock *ptr = NULL;
    380428
     429    #ifdef PS_MEM_USE_RECYCLE
     430    // Are we in one of the recycle bins?
     431    int bin = getRecycleBin(size);
     432    if (bin < N_RECYCLE_BINS) {
     433        size = recycleBinSize[bin];     // round-up size to next sized bin.
     434        ptr = recyclePop(bin);          // grab out of the recycle bin
     435    }
     436    #endif // #ifdef PS_MEM_USE_RECYCLE
     437
    381438    if (ptr == NULL) {
    382439        ptr = malloc(sizeof(psMemBlock) + size + sizeof(psPtr ));
    383440
    384441        if (ptr == NULL) {
    385             psAbort(__func__, "Failed to allocate %zd bytes at %s:%d", size, file, lineno);
     442            ptr = memExhaustedCallback(size);
     443            if (ptr == NULL) {
     444                psAbort(__func__, "Failed to allocate %zd bytes at %s:%d", size, file, lineno);
     445            }
    386446        }
    387447
     
    473533    ptr = (psMemBlock *)realloc(ptr, sizeof(psMemBlock) + size + sizeof(psPtr));
    474534    if (ptr == NULL) {
    475         psAbort(__func__, "Failed to reallocate %zd bytes at %s:%d", size, file, lineno);
     535        ptr = memExhaustedCallback(size);
     536        if (ptr == NULL) {
     537            psAbort(__func__, "Failed to reallocate %zd bytes at %s:%d", size, file, lineno);
     538        }
    476539    }
    477540
Note: See TracChangeset for help on using the changeset viewer.