IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 28, 2004, 3:08:46 PM (22 years ago)
Author:
desonia
Message:

added psVectorRecycle function, which tries to recycle a vector (same concept as psImageRecycle).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/collections/psVector.c

    r689 r811  
    1919 *  @author Ross Harman, MHPCC
    2020 *   
    21  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    22  *  @date $Date: 2004-05-14 21:05:52 $
     21 *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
     22 *  @date $Date: 2004-05-29 01:08:46 $
    2323 *
    2424 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    9191}
    9292
    93 psVector *psVectorRealloc(psVector *restrict psVec, unsigned int nalloc)
     93psVector *psVectorRealloc(psVector *restrict in, unsigned int nalloc)
    9494{
    9595    int elementSize = 0;
    96     psElemType elemType = psVec->type.type;
     96    psElemType elemType;
    9797
    9898    // Invalid nalloc
     
    102102    }
    103103
    104     elementSize = PSELEMTYPE_SIZEOF(elemType);
    105 
    106     if(psVec == NULL) {                                     // For creating new psVector
    107         psVec = psVectorAlloc(psVec->type.type, nalloc);
    108     } else if(psVec->nalloc != nalloc) {                    // No need to realloc to same size
    109         if(nalloc < psVec->n) {
     104    if(in == NULL) {                                     // For creating new psVector
     105        return NULL;
     106    } else if(in->nalloc != nalloc) {                    // No need to realloc to same size
     107        elemType = in->type.type;
     108        elementSize = PSELEMTYPE_SIZEOF(elemType);
     109        if(nalloc < in->n) {
    110110            if (elemType == PS_TYPE_PTR) {
    111                 for (int i = nalloc; i < psVec->n; i++) {   // For reduction in vector size
    112                     psMemDecrRefCounter(psVec->vec.vp[i]);
     111                for (int i = nalloc; i < in->n; i++) {   // For reduction in vector size
     112                    psMemDecrRefCounter(in->vec.vp[i]);
    113113                }
    114114            }
    115             psVec->n = nalloc;
     115            in->n = nalloc;
    116116        }
    117117
    118118        // Realloc after decrementation to avoid accessing freed array elements
    119         psVec->vec.v = psRealloc(psVec->vec.v,nalloc*elementSize);
    120         psVec->nalloc = nalloc;
     119        in->vec.v = psRealloc(in->vec.v,nalloc*elementSize);
     120        in->nalloc = nalloc;
    121121    }
    122122
    123     return psVec;
     123    return in;
     124}
     125
     126psVector *psVectorRecycle(psVector *restrict in, psElemType type, unsigned int nalloc)
     127{
     128    psElemType elemType;
     129
     130    if (in == NULL) {
     131        return psVectorAlloc(type,nalloc);
     132    }
     133
     134    elemType = in->type.type;
     135
     136    if (in->nalloc == nalloc && elemType == type) {
     137        // it is proper size/type already
     138        return in;
     139    }
     140
     141    // Invalid nalloc
     142    if(nalloc < 1) {
     143        psError(__func__, "Invalid value for nalloc (%d)\n", nalloc);
     144        psVectorFree(in);
     145        return NULL;
     146    }
     147
     148
     149    // if vector of pointers, dereference old values.
     150    if (elemType == PS_TYPE_PTR) {
     151        for (int i = 0; i < in->n; i++) {   // For reduction in vector size
     152            psMemDecrRefCounter(in->vec.vp[i]);
     153            in->vec.vp[i] = NULL;
     154        }
     155    }
     156
     157    in->vec.v = psRealloc(in->vec.v,nalloc*PSELEMTYPE_SIZEOF(elemType));
     158
     159    in->n = 0;
     160    in->type.type = elemType;
     161    in->nalloc = nalloc;
     162
     163    return in;
    124164}
    125165
Note: See TracChangeset for help on using the changeset viewer.