IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 670


Ignore:
Timestamp:
May 13, 2004, 12:47:52 PM (22 years ago)
Author:
desonia
Message:

Major refactoring of the Vector alloc/free functions.

Location:
trunk/psLib/src
Files:
5 edited

Legend:

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

    r668 r670  
    1414 *  @author Ross Harman, MHPCC
    1515 *   
    16  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2004-05-13 20:25:00 $
     16 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2004-05-13 22:47:52 $
    1818 *
    1919 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    189189    outN = outVector->n;
    190190    inVec = inVector->vec.f;
    191     outVec = outVector->vec.i;
     191    outVec = outVector->vec.i32;
    192192
    193193    if(inN != outN) {
  • trunk/psLib/src/collections/psVector.c

    r668 r670  
    33 *  @brief Contains support for basic vector types
    44 *
    5  *  This file defines types and functions for one dimensional vectors which include: 
     5 *  This file defines types and functions for one dimensional vectors which include:
    66 *      char
    77 *      short
     
    1919 *  @author Ross Harman, MHPCC
    2020 *   
    21  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    22  *  @date $Date: 2004-05-13 20:25:00 $
     21 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     22 *  @date $Date: 2004-05-13 22:47:52 $
    2323 *
    2424 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3131#include "psError.h"
    3232#include "psVector.h"
     33#include "psLogMsg.h"
    3334
    3435/******************************************************************************/
     
    6061/*****************************************************************************/
    6162
    62 static void* p_psVectorAlloc(psVector *restrict psVec, psElemType elemType, int nalloc, int elemSize)
     63/*****************************************************************************/
     64/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     65/*****************************************************************************/
     66psVector* psVectorAlloc(psElemType elemType, unsigned int nalloc)
    6367{
    64     void *vec = NULL;
     68    psVector *psVec = NULL;
     69    int elementSize = 0;
     70
     71    // Invalid nalloc
     72    if(nalloc < 1) {
     73        psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
     74        return NULL;
     75    }
     76
     77    if (elemType == PS_TYPE_PTR) {
     78        elementSize = sizeof(void*);
     79    } else {
     80        elementSize = PSELEMTYPE_SIZEOF(elemType);
     81    }
     82
     83    // Create vector struct
     84    psVec = (psVector *)psAlloc(sizeof(psVector));
    6585
    6686    psVec->type.dimen = PS_DIMEN_VECTOR;
     
    6989    psVec->n = 0;
    7090
    71     if(nalloc != 0) {
    72         vec = psAlloc(nalloc*sizeof(elemSize));
    73     }
     91    // Create vector data array
     92    psVec->vec.v = psAlloc(nalloc*elementSize);
    7493
    75     return vec;
     94    return psVec;
    7695}
    7796
    78 static void* p_psVectorRealloc(psVector *restrict psVec, void *vec, int nalloc, int elemSize)
     97psVector *psVectorRealloc(psVector *restrict psVec, unsigned int nalloc)
    7998{
    80     // For decrease in psVector size
    81     if(nalloc < psVec->n) {
    82         psVec->n = nalloc;
    83     }
    84 
    85     psVec->nalloc = nalloc;
    86 
    87     return psRealloc(vec, nalloc*elemSize);
    88 }
    89 
    90 /*****************************************************************************/
    91 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    92 /*****************************************************************************/
    93 psVector* psVectorAlloc(psElemType elemType, int nalloc)
    94 {
    95     psVector *psVec = NULL;
    96     psElemType vecType = 0;
    97 
    98     vecType = elemType;
     99    int elementSize = 0;
     100    psElemType elemType = psVec->type.type;
    99101
    100102    // Invalid nalloc
    101     if(nalloc <= 0) {
     103    if(nalloc < 1) {
    102104        psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);
    103105        return NULL;
    104106    }
    105107
    106     // Create vector struct
    107     psVec = (psVector *)psAlloc(sizeof(psVector));
    108 
    109     // Create vector data array
    110     switch(vecType) {
    111     case PS_TYPE_CHAR:
    112         psVec->vec.c = (char *)p_psVectorAlloc(psVec, PS_TYPE_CHAR, nalloc, sizeof(char));
    113         break;
    114     case PS_TYPE_SHORT:
    115         psVec->vec.s = (short *)p_psVectorAlloc(psVec, PS_TYPE_SHORT, nalloc, sizeof(short));
    116         break;
    117     case PS_TYPE_INT:
    118         psVec->vec.i = (int *)p_psVectorAlloc(psVec, PS_TYPE_INT, nalloc, sizeof(int));
    119         break;
    120     case PS_TYPE_LONG:
    121         psVec->vec.l = (long *)p_psVectorAlloc(psVec, PS_TYPE_LONG, nalloc, sizeof(long));
    122         break;
    123     case PS_TYPE_UCHAR:
    124         psVec->vec.uc = (unsigned char *)p_psVectorAlloc(psVec, PS_TYPE_UCHAR, nalloc, sizeof(unsigned char));
    125         break;
    126     case PS_TYPE_USHORT:
    127         psVec->vec.us = (unsigned short *)p_psVectorAlloc(psVec, PS_TYPE_USHORT, nalloc, sizeof(unsigned short));
    128         break;
    129     case PS_TYPE_UINT:
    130         psVec->vec.ui = (unsigned int *)p_psVectorAlloc(psVec, PS_TYPE_UINT, nalloc, sizeof(unsigned int));
    131         break;
    132     case PS_TYPE_ULONG:
    133         psVec->vec.ul = (unsigned long *)p_psVectorAlloc(psVec, PS_TYPE_ULONG, nalloc, sizeof(unsigned long));
    134         break;
    135     case PS_TYPE_FLOAT:
    136         psVec->vec.f = (float *)p_psVectorAlloc(psVec, PS_TYPE_FLOAT, nalloc, sizeof(float));
    137         break;
    138     case PS_TYPE_DOUBLE:
    139         psVec->vec.d = (double *)p_psVectorAlloc(psVec, PS_TYPE_DOUBLE, nalloc, sizeof(double));
    140         break;
    141     case PS_TYPE_COMPLEX:
    142         psVec->vec.cf = (complex float *)p_psVectorAlloc(psVec, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
    143         break;
    144     case PS_TYPE_OTHER:
    145         break;
    146     default:
    147         psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     108    if (elemType == PS_TYPE_PTR) {
     109        elementSize = sizeof(void*);
     110    } else {
     111        elementSize = PSELEMTYPE_SIZEOF(elemType);
    148112    }
    149113
    150     return psVec;
    151 }
    152 
    153 psVector *psVectorRealloc(psVector *restrict psVec, int nalloc)
    154 {
    155     psElemType vecType = 0;
    156 
    157     vecType = psVec->type.type;
    158 
    159     // Invalid nalloc
    160     if(nalloc < 0) {
    161         psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);
    162         return NULL;
    163     }
    164114
    165115    if(psVec == NULL) {                                 // For creating new psVector
    166116        psVec = psVectorAlloc(psVec->type.type, nalloc);
    167117    } else if(psVec->nalloc != nalloc) {                // No need to realloc to same size
    168         switch (vecType) {
    169         case PS_TYPE_CHAR:
    170             psVec->vec.c = p_psVectorRealloc(psVec, psVec->vec.c, nalloc, sizeof(char));
    171             break;
    172         case PS_TYPE_SHORT:
    173             psVec->vec.s = p_psVectorRealloc(psVec, psVec->vec.s, nalloc, sizeof(short));
    174             break;
    175         case PS_TYPE_INT:
    176             psVec->vec.i = p_psVectorRealloc(psVec, psVec->vec.i, nalloc, sizeof(int));
    177             break;
    178         case PS_TYPE_LONG:
    179             psVec->vec.l = p_psVectorRealloc(psVec, psVec->vec.l, nalloc, sizeof(long));
    180             break;
    181         case PS_TYPE_UCHAR:
    182             psVec->vec.uc = p_psVectorRealloc(psVec, psVec->vec.uc, nalloc, sizeof(unsigned char));
    183             break;
    184         case PS_TYPE_USHORT:
    185             psVec->vec.us = p_psVectorRealloc(psVec, psVec->vec.us, nalloc, sizeof(unsigned short));
    186             break;
    187         case PS_TYPE_UINT:
    188             psVec->vec.ui = p_psVectorRealloc(psVec, psVec->vec.ui, nalloc, sizeof(unsigned int));
    189             break;
    190         case PS_TYPE_ULONG:
    191             psVec->vec.ul = p_psVectorRealloc(psVec, psVec->vec.ul, nalloc, sizeof(unsigned long));
    192             break;
    193         case PS_TYPE_FLOAT:
    194             psVec->vec.f = p_psVectorRealloc(psVec, psVec->vec.f, nalloc, sizeof(float));
    195             break;
    196         case PS_TYPE_DOUBLE:
    197             psVec->vec.d = p_psVectorRealloc(psVec, psVec->vec.d, nalloc, sizeof(double));
    198             break;
    199         case PS_TYPE_COMPLEX:
    200             psVec->vec.cf = p_psVectorRealloc(psVec, psVec->vec.cf, nalloc, sizeof(complex float));
    201             break;
    202         case PS_TYPE_OTHER:
    203             break;
    204         default:
    205             psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     118        psVec->vec.v = psRealloc(psVec->vec.v,nalloc*elementSize);
     119        psVec->nalloc = nalloc;
     120        if(nalloc < psVec->n) {
     121            if (elemType == PS_TYPE_PTR) {
     122                for (int i = nalloc; i < psVec->n; i++) {               // For reduction in vector size
     123                    psMemDecrRefCounter(psVec->vec.vp[i]);
     124                }
     125            }
     126            psVec->n = nalloc;
    206127        }
    207128    }
     
    212133void psVectorFree(psVector *restrict psVec)
    213134{
    214     psElemType vecType = 0;
    215 
    216     vecType = psVec->type.type;
    217 
    218135    if (psVec == NULL) {
    219136        return;
    220137    }
    221138
    222     switch (vecType) {
    223     case PS_TYPE_CHAR:
    224         psFree(psVec->vec.c);
    225         break;
    226     case PS_TYPE_SHORT:
    227         psFree(psVec->vec.s);
    228         break;
    229     case PS_TYPE_INT:
    230         psFree(psVec->vec.i);
    231         break;
    232     case PS_TYPE_LONG:
    233         psFree(psVec->vec.l);
    234         break;
    235     case PS_TYPE_UCHAR:
    236         psFree(psVec->vec.uc);
    237         break;
    238     case PS_TYPE_USHORT:
    239         psFree(psVec->vec.us);
    240         break;
    241     case PS_TYPE_UINT:
    242         psFree(psVec->vec.ui);
    243         break;
    244     case PS_TYPE_ULONG:
    245         psFree(psVec->vec.ul);
    246         break;
    247     case PS_TYPE_FLOAT:
    248         psFree(psVec->vec.f);
    249         break;
    250     case PS_TYPE_DOUBLE:
    251         psFree(psVec->vec.d);
    252         break;
    253     case PS_TYPE_COMPLEX:
    254         psFree(psVec->vec.cf);
    255         break;
    256     case PS_TYPE_OTHER:
    257         break;
    258     default:
    259         psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     139    if (psVec->type.type == PS_TYPE_PTR) {
     140        psLogMsg(__func__,PS_LOG_WARN,"psVectorFree was called with a void* vector. "
     141                 "Referenced of pointers was not freed.");
     142        psVoidPtrVectorFree(psVec,NULL);
     143    } else {
     144        psFree(psVec->vec.v);
     145        psFree(psVec);
    260146    }
    261 
    262     // Free vector struct
    263     psFree(psVec);
    264147}
    265148
    266 psVector* psVoidPtrVectorAlloc(int nalloc)
     149void psVectorElementFree(psVector *restrict psVec, void (*elemFree)(void *))
    267150{
    268     psVector *psVec = NULL;
    269     psVec = psAlloc(sizeof(psVector));
    270     psVec->vec.vp = p_psVectorAlloc(psVec, PS_TYPE_OTHER, nalloc, sizeof(void *));
    271     return psVec;
    272 }
    273 
    274 psVector *psVoidPtrVectorRealloc(psVector *restrict psVec, int nalloc)
    275 {
    276     int i = 0;
    277 
    278     for (i = nalloc; i < psVec->n; i++) {               // For reduction in vector size
    279         psMemDecrRefCounter(psVec->vec.vp[i]);
    280     }
    281 
    282     if(psVec == NULL) {                                 // For creating new psVector
    283         psVec = psVoidPtrVectorAlloc(nalloc);
    284     } else if(psVec->nalloc != nalloc) {                // No need to realloc to same size
    285         psVec->vec.vp = p_psVectorRealloc(psVec, psVec->vec.vp, nalloc, sizeof(void *));
    286     }
    287 
    288     return psVec;
    289 }
    290 
    291 void psVoidPtrVectorFree(psVector *restrict psVec, void (*elemFree)(void *))
    292 {
    293     int i = 0;
    294151
    295152    if(psVec == NULL) {
     
    297154    }
    298155
    299     for(i = 0; i < psVec->nalloc; i++) {
     156    if (psVec->type.type != PS_TYPE_PTR) {
     157        psLogMsg(__func__,PS_LOG_WARN,"psVectorElementFree only operates on void* vectors");
     158        return;
     159    }
     160
     161    for(int i = 0; i < psVec->nalloc; i++) {
    300162        if(elemFree == NULL) {
    301163            psMemDecrRefCounter(psVec->vec.vp[i]);
     
    303165            elemFree(psMemDecrRefCounter(psVec->vec.vp[i]));
    304166        }
     167        psVec->vec.vp[i] = NULL;
    305168    }
    306 
    307     // Free pointer vector
    308     psFree(psVec->vec.vp);
    309     psFree(psVec);
    310169}
    311170
  • trunk/psLib/src/collections/psVector.h

    r663 r670  
    1919 *  @author Ross Harman, MHPCC
    2020 *   
    21  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    22  *  @date $Date: 2004-05-13 19:54:26 $
     21 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     22 *  @date $Date: 2004-05-13 22:47:52 $
    2323 *
    2424 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    6060    PS_TYPE_COMPLEX_FLOAT = 0x84,     ///< Complex numbers consisting of single-precision floating point.
    6161    PS_TYPE_COMPLEX_DOUBLE = 0x88,    ///< Complex numbers consisting of double-precision floating point.
    62     PS_TYPE_OTHER,                    ///< Something else that's not supported for arithmetic.} psElemType;
     62    PS_TYPE_PTR = 0x00,               ///< Something else that's not supported for arithmetic.}
    6363} psElemType;
    6464
     
    118118        double *d;                      ///< Pointers to double precision data.
    119119        complex float *cf;              ///< Pointers to complex floating point data.
     120        void *v;                        ///< Pointers to generic void data
    120121        void **vp;                      ///< Void pointer vector.
    121122    }vec;                               ///< Union for data types.
     
    135136 */
    136137psVector *psVectorAlloc(
    137     psElemType dataType,    ///< Type of data to be held by vector.
    138     int nalloc              ///< Total number of elements to make available.
     138    psElemType dataType,                ///< Type of data to be held by vector.
     139    unsigned int nalloc                 ///< Total number of elements to make available.
    139140);
    140141
     
    164165);
    165166
    166 /** Allocate a void pointer vector.
     167
     168/** Deallocate/Dereference elements of a void pointer vector.
    167169 *
    168  * Uses psLib memory allocation functions to create a vector of void pointers as defined by the psVoidPtrVector
    169  * struct.
    170  *
    171  * @return psVector*: Pointer to psVector.
    172  *
    173  */
    174 psVector *psVoidPtrVectorAlloc(
    175     unsigned int nalloc                 ///< Number of elements to use.
    176 );
    177 
    178 /** Reallocate a void pointer vector.
    179  *
    180  * Uses psLib memory allocation functions to reallocate a vector of void pointers as defined by the
    181  * psVoidPtrVector struct. If the vector size is increased or decreased, this function does not allocate or
    182  * deallocate the contents of individual vector elements. The user must do this after calling this function.
    183  *
    184  * @return psVector*: Pointer to psVector.
    185  *
    186  */
    187 psVector *psVoidPtrVectorRealloc(
    188     psVector *restrict psVec,           ///< Void pointer vector to destroy.
    189     unsigned int nalloc                 ///< Number of elements.
    190 );
    191 
    192 /** Deallocate a void pointer vector.
    193  *
    194  * Uses psLib memory allocation functions to deallocate a vector of void pointers as defined by the
    195  * psVoidPtrVector struct. This function does not free the vector elements unless the user provides a callback
    196  * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to
    197  * delete the vector elements afterwards. The user must not delete or deallocate the vector elements prior to
    198  * calling this function, as it requires valid elements for memory reference decrementation operations.
    199  *
    200  * @return: void
     170 * Uses psLib memory allocation functions to deallocate/dereference elements of a vector of void pointers.
     171 * This function does not free the vector elements unless the user provides a elemFree function
     172 * pointer. If the elemFree function pointer is NULL, the reference cound of the elements are decremented
     173 * without being freed.  The vector psVec is not freed, and its elements will all be set to NULL.
    201174 *
    202175 */
  • trunk/psLib/src/mathtypes/psVector.c

    r668 r670  
    33 *  @brief Contains support for basic vector types
    44 *
    5  *  This file defines types and functions for one dimensional vectors which include: 
     5 *  This file defines types and functions for one dimensional vectors which include:
    66 *      char
    77 *      short
     
    1919 *  @author Ross Harman, MHPCC
    2020 *   
    21  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    22  *  @date $Date: 2004-05-13 20:25:00 $
     21 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     22 *  @date $Date: 2004-05-13 22:47:52 $
    2323 *
    2424 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    3131#include "psError.h"
    3232#include "psVector.h"
     33#include "psLogMsg.h"
    3334
    3435/******************************************************************************/
     
    6061/*****************************************************************************/
    6162
    62 static void* p_psVectorAlloc(psVector *restrict psVec, psElemType elemType, int nalloc, int elemSize)
     63/*****************************************************************************/
     64/* FUNCTION IMPLEMENTATION - PUBLIC                                          */
     65/*****************************************************************************/
     66psVector* psVectorAlloc(psElemType elemType, unsigned int nalloc)
    6367{
    64     void *vec = NULL;
     68    psVector *psVec = NULL;
     69    int elementSize = 0;
     70
     71    // Invalid nalloc
     72    if(nalloc < 1) {
     73        psError(__func__, "Invalid value for nalloc. nalloc: %d\n", nalloc);
     74        return NULL;
     75    }
     76
     77    if (elemType == PS_TYPE_PTR) {
     78        elementSize = sizeof(void*);
     79    } else {
     80        elementSize = PSELEMTYPE_SIZEOF(elemType);
     81    }
     82
     83    // Create vector struct
     84    psVec = (psVector *)psAlloc(sizeof(psVector));
    6585
    6686    psVec->type.dimen = PS_DIMEN_VECTOR;
     
    6989    psVec->n = 0;
    7090
    71     if(nalloc != 0) {
    72         vec = psAlloc(nalloc*sizeof(elemSize));
    73     }
     91    // Create vector data array
     92    psVec->vec.v = psAlloc(nalloc*elementSize);
    7493
    75     return vec;
     94    return psVec;
    7695}
    7796
    78 static void* p_psVectorRealloc(psVector *restrict psVec, void *vec, int nalloc, int elemSize)
     97psVector *psVectorRealloc(psVector *restrict psVec, unsigned int nalloc)
    7998{
    80     // For decrease in psVector size
    81     if(nalloc < psVec->n) {
    82         psVec->n = nalloc;
    83     }
    84 
    85     psVec->nalloc = nalloc;
    86 
    87     return psRealloc(vec, nalloc*elemSize);
    88 }
    89 
    90 /*****************************************************************************/
    91 /* FUNCTION IMPLEMENTATION - PUBLIC                                          */
    92 /*****************************************************************************/
    93 psVector* psVectorAlloc(psElemType elemType, int nalloc)
    94 {
    95     psVector *psVec = NULL;
    96     psElemType vecType = 0;
    97 
    98     vecType = elemType;
     99    int elementSize = 0;
     100    psElemType elemType = psVec->type.type;
    99101
    100102    // Invalid nalloc
    101     if(nalloc <= 0) {
     103    if(nalloc < 1) {
    102104        psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);
    103105        return NULL;
    104106    }
    105107
    106     // Create vector struct
    107     psVec = (psVector *)psAlloc(sizeof(psVector));
    108 
    109     // Create vector data array
    110     switch(vecType) {
    111     case PS_TYPE_CHAR:
    112         psVec->vec.c = (char *)p_psVectorAlloc(psVec, PS_TYPE_CHAR, nalloc, sizeof(char));
    113         break;
    114     case PS_TYPE_SHORT:
    115         psVec->vec.s = (short *)p_psVectorAlloc(psVec, PS_TYPE_SHORT, nalloc, sizeof(short));
    116         break;
    117     case PS_TYPE_INT:
    118         psVec->vec.i = (int *)p_psVectorAlloc(psVec, PS_TYPE_INT, nalloc, sizeof(int));
    119         break;
    120     case PS_TYPE_LONG:
    121         psVec->vec.l = (long *)p_psVectorAlloc(psVec, PS_TYPE_LONG, nalloc, sizeof(long));
    122         break;
    123     case PS_TYPE_UCHAR:
    124         psVec->vec.uc = (unsigned char *)p_psVectorAlloc(psVec, PS_TYPE_UCHAR, nalloc, sizeof(unsigned char));
    125         break;
    126     case PS_TYPE_USHORT:
    127         psVec->vec.us = (unsigned short *)p_psVectorAlloc(psVec, PS_TYPE_USHORT, nalloc, sizeof(unsigned short));
    128         break;
    129     case PS_TYPE_UINT:
    130         psVec->vec.ui = (unsigned int *)p_psVectorAlloc(psVec, PS_TYPE_UINT, nalloc, sizeof(unsigned int));
    131         break;
    132     case PS_TYPE_ULONG:
    133         psVec->vec.ul = (unsigned long *)p_psVectorAlloc(psVec, PS_TYPE_ULONG, nalloc, sizeof(unsigned long));
    134         break;
    135     case PS_TYPE_FLOAT:
    136         psVec->vec.f = (float *)p_psVectorAlloc(psVec, PS_TYPE_FLOAT, nalloc, sizeof(float));
    137         break;
    138     case PS_TYPE_DOUBLE:
    139         psVec->vec.d = (double *)p_psVectorAlloc(psVec, PS_TYPE_DOUBLE, nalloc, sizeof(double));
    140         break;
    141     case PS_TYPE_COMPLEX:
    142         psVec->vec.cf = (complex float *)p_psVectorAlloc(psVec, PS_TYPE_COMPLEX, nalloc, sizeof(complex float));
    143         break;
    144     case PS_TYPE_OTHER:
    145         break;
    146     default:
    147         psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     108    if (elemType == PS_TYPE_PTR) {
     109        elementSize = sizeof(void*);
     110    } else {
     111        elementSize = PSELEMTYPE_SIZEOF(elemType);
    148112    }
    149113
    150     return psVec;
    151 }
    152 
    153 psVector *psVectorRealloc(psVector *restrict psVec, int nalloc)
    154 {
    155     psElemType vecType = 0;
    156 
    157     vecType = psVec->type.type;
    158 
    159     // Invalid nalloc
    160     if(nalloc < 0) {
    161         psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);
    162         return NULL;
    163     }
    164114
    165115    if(psVec == NULL) {                                 // For creating new psVector
    166116        psVec = psVectorAlloc(psVec->type.type, nalloc);
    167117    } else if(psVec->nalloc != nalloc) {                // No need to realloc to same size
    168         switch (vecType) {
    169         case PS_TYPE_CHAR:
    170             psVec->vec.c = p_psVectorRealloc(psVec, psVec->vec.c, nalloc, sizeof(char));
    171             break;
    172         case PS_TYPE_SHORT:
    173             psVec->vec.s = p_psVectorRealloc(psVec, psVec->vec.s, nalloc, sizeof(short));
    174             break;
    175         case PS_TYPE_INT:
    176             psVec->vec.i = p_psVectorRealloc(psVec, psVec->vec.i, nalloc, sizeof(int));
    177             break;
    178         case PS_TYPE_LONG:
    179             psVec->vec.l = p_psVectorRealloc(psVec, psVec->vec.l, nalloc, sizeof(long));
    180             break;
    181         case PS_TYPE_UCHAR:
    182             psVec->vec.uc = p_psVectorRealloc(psVec, psVec->vec.uc, nalloc, sizeof(unsigned char));
    183             break;
    184         case PS_TYPE_USHORT:
    185             psVec->vec.us = p_psVectorRealloc(psVec, psVec->vec.us, nalloc, sizeof(unsigned short));
    186             break;
    187         case PS_TYPE_UINT:
    188             psVec->vec.ui = p_psVectorRealloc(psVec, psVec->vec.ui, nalloc, sizeof(unsigned int));
    189             break;
    190         case PS_TYPE_ULONG:
    191             psVec->vec.ul = p_psVectorRealloc(psVec, psVec->vec.ul, nalloc, sizeof(unsigned long));
    192             break;
    193         case PS_TYPE_FLOAT:
    194             psVec->vec.f = p_psVectorRealloc(psVec, psVec->vec.f, nalloc, sizeof(float));
    195             break;
    196         case PS_TYPE_DOUBLE:
    197             psVec->vec.d = p_psVectorRealloc(psVec, psVec->vec.d, nalloc, sizeof(double));
    198             break;
    199         case PS_TYPE_COMPLEX:
    200             psVec->vec.cf = p_psVectorRealloc(psVec, psVec->vec.cf, nalloc, sizeof(complex float));
    201             break;
    202         case PS_TYPE_OTHER:
    203             break;
    204         default:
    205             psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     118        psVec->vec.v = psRealloc(psVec->vec.v,nalloc*elementSize);
     119        psVec->nalloc = nalloc;
     120        if(nalloc < psVec->n) {
     121            if (elemType == PS_TYPE_PTR) {
     122                for (int i = nalloc; i < psVec->n; i++) {               // For reduction in vector size
     123                    psMemDecrRefCounter(psVec->vec.vp[i]);
     124                }
     125            }
     126            psVec->n = nalloc;
    206127        }
    207128    }
     
    212133void psVectorFree(psVector *restrict psVec)
    213134{
    214     psElemType vecType = 0;
    215 
    216     vecType = psVec->type.type;
    217 
    218135    if (psVec == NULL) {
    219136        return;
    220137    }
    221138
    222     switch (vecType) {
    223     case PS_TYPE_CHAR:
    224         psFree(psVec->vec.c);
    225         break;
    226     case PS_TYPE_SHORT:
    227         psFree(psVec->vec.s);
    228         break;
    229     case PS_TYPE_INT:
    230         psFree(psVec->vec.i);
    231         break;
    232     case PS_TYPE_LONG:
    233         psFree(psVec->vec.l);
    234         break;
    235     case PS_TYPE_UCHAR:
    236         psFree(psVec->vec.uc);
    237         break;
    238     case PS_TYPE_USHORT:
    239         psFree(psVec->vec.us);
    240         break;
    241     case PS_TYPE_UINT:
    242         psFree(psVec->vec.ui);
    243         break;
    244     case PS_TYPE_ULONG:
    245         psFree(psVec->vec.ul);
    246         break;
    247     case PS_TYPE_FLOAT:
    248         psFree(psVec->vec.f);
    249         break;
    250     case PS_TYPE_DOUBLE:
    251         psFree(psVec->vec.d);
    252         break;
    253     case PS_TYPE_COMPLEX:
    254         psFree(psVec->vec.cf);
    255         break;
    256     case PS_TYPE_OTHER:
    257         break;
    258     default:
    259         psError(__func__, " : Line %d - Invalid vector type. Type: %d\n", __LINE__, vecType);
     139    if (psVec->type.type == PS_TYPE_PTR) {
     140        psLogMsg(__func__,PS_LOG_WARN,"psVectorFree was called with a void* vector. "
     141                 "Referenced of pointers was not freed.");
     142        psVoidPtrVectorFree(psVec,NULL);
     143    } else {
     144        psFree(psVec->vec.v);
     145        psFree(psVec);
    260146    }
    261 
    262     // Free vector struct
    263     psFree(psVec);
    264147}
    265148
    266 psVector* psVoidPtrVectorAlloc(int nalloc)
     149void psVectorElementFree(psVector *restrict psVec, void (*elemFree)(void *))
    267150{
    268     psVector *psVec = NULL;
    269     psVec = psAlloc(sizeof(psVector));
    270     psVec->vec.vp = p_psVectorAlloc(psVec, PS_TYPE_OTHER, nalloc, sizeof(void *));
    271     return psVec;
    272 }
    273 
    274 psVector *psVoidPtrVectorRealloc(psVector *restrict psVec, int nalloc)
    275 {
    276     int i = 0;
    277 
    278     for (i = nalloc; i < psVec->n; i++) {               // For reduction in vector size
    279         psMemDecrRefCounter(psVec->vec.vp[i]);
    280     }
    281 
    282     if(psVec == NULL) {                                 // For creating new psVector
    283         psVec = psVoidPtrVectorAlloc(nalloc);
    284     } else if(psVec->nalloc != nalloc) {                // No need to realloc to same size
    285         psVec->vec.vp = p_psVectorRealloc(psVec, psVec->vec.vp, nalloc, sizeof(void *));
    286     }
    287 
    288     return psVec;
    289 }
    290 
    291 void psVoidPtrVectorFree(psVector *restrict psVec, void (*elemFree)(void *))
    292 {
    293     int i = 0;
    294151
    295152    if(psVec == NULL) {
     
    297154    }
    298155
    299     for(i = 0; i < psVec->nalloc; i++) {
     156    if (psVec->type.type != PS_TYPE_PTR) {
     157        psLogMsg(__func__,PS_LOG_WARN,"psVectorElementFree only operates on void* vectors");
     158        return;
     159    }
     160
     161    for(int i = 0; i < psVec->nalloc; i++) {
    300162        if(elemFree == NULL) {
    301163            psMemDecrRefCounter(psVec->vec.vp[i]);
     
    303165            elemFree(psMemDecrRefCounter(psVec->vec.vp[i]));
    304166        }
     167        psVec->vec.vp[i] = NULL;
    305168    }
    306 
    307     // Free pointer vector
    308     psFree(psVec->vec.vp);
    309     psFree(psVec);
    310169}
    311170
  • trunk/psLib/src/mathtypes/psVector.h

    r663 r670  
    1919 *  @author Ross Harman, MHPCC
    2020 *   
    21  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    22  *  @date $Date: 2004-05-13 19:54:26 $
     21 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     22 *  @date $Date: 2004-05-13 22:47:52 $
    2323 *
    2424 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    6060    PS_TYPE_COMPLEX_FLOAT = 0x84,     ///< Complex numbers consisting of single-precision floating point.
    6161    PS_TYPE_COMPLEX_DOUBLE = 0x88,    ///< Complex numbers consisting of double-precision floating point.
    62     PS_TYPE_OTHER,                    ///< Something else that's not supported for arithmetic.} psElemType;
     62    PS_TYPE_PTR = 0x00,               ///< Something else that's not supported for arithmetic.}
    6363} psElemType;
    6464
     
    118118        double *d;                      ///< Pointers to double precision data.
    119119        complex float *cf;              ///< Pointers to complex floating point data.
     120        void *v;                        ///< Pointers to generic void data
    120121        void **vp;                      ///< Void pointer vector.
    121122    }vec;                               ///< Union for data types.
     
    135136 */
    136137psVector *psVectorAlloc(
    137     psElemType dataType,    ///< Type of data to be held by vector.
    138     int nalloc              ///< Total number of elements to make available.
     138    psElemType dataType,                ///< Type of data to be held by vector.
     139    unsigned int nalloc                 ///< Total number of elements to make available.
    139140);
    140141
     
    164165);
    165166
    166 /** Allocate a void pointer vector.
     167
     168/** Deallocate/Dereference elements of a void pointer vector.
    167169 *
    168  * Uses psLib memory allocation functions to create a vector of void pointers as defined by the psVoidPtrVector
    169  * struct.
    170  *
    171  * @return psVector*: Pointer to psVector.
    172  *
    173  */
    174 psVector *psVoidPtrVectorAlloc(
    175     unsigned int nalloc                 ///< Number of elements to use.
    176 );
    177 
    178 /** Reallocate a void pointer vector.
    179  *
    180  * Uses psLib memory allocation functions to reallocate a vector of void pointers as defined by the
    181  * psVoidPtrVector struct. If the vector size is increased or decreased, this function does not allocate or
    182  * deallocate the contents of individual vector elements. The user must do this after calling this function.
    183  *
    184  * @return psVector*: Pointer to psVector.
    185  *
    186  */
    187 psVector *psVoidPtrVectorRealloc(
    188     psVector *restrict psVec,           ///< Void pointer vector to destroy.
    189     unsigned int nalloc                 ///< Number of elements.
    190 );
    191 
    192 /** Deallocate a void pointer vector.
    193  *
    194  * Uses psLib memory allocation functions to deallocate a vector of void pointers as defined by the
    195  * psVoidPtrVector struct. This function does not free the vector elements unless the user provides a callback
    196  * function. NULL may be passed as an alternative to supplying a callback, but the user must remember to
    197  * delete the vector elements afterwards. The user must not delete or deallocate the vector elements prior to
    198  * calling this function, as it requires valid elements for memory reference decrementation operations.
    199  *
    200  * @return: void
     170 * Uses psLib memory allocation functions to deallocate/dereference elements of a vector of void pointers.
     171 * This function does not free the vector elements unless the user provides a elemFree function
     172 * pointer. If the elemFree function pointer is NULL, the reference cound of the elements are decremented
     173 * without being freed.  The vector psVec is not freed, and its elements will all be set to NULL.
    201174 *
    202175 */
Note: See TracChangeset for help on using the changeset viewer.