Changeset 670
- Timestamp:
- May 13, 2004, 12:47:52 PM (22 years ago)
- Location:
- trunk/psLib/src
- Files:
-
- 5 edited
-
collections/psSort.c (modified) (2 diffs)
-
collections/psVector.c (modified) (8 diffs)
-
collections/psVector.h (modified) (5 diffs)
-
mathtypes/psVector.c (modified) (8 diffs)
-
mathtypes/psVector.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/collections/psSort.c
r668 r670 14 14 * @author Ross Harman, MHPCC 15 15 * 16 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $17 * @date $Date: 2004-05-13 2 0:25:00$16 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 17 * @date $Date: 2004-05-13 22:47:52 $ 18 18 * 19 19 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 189 189 outN = outVector->n; 190 190 inVec = inVector->vec.f; 191 outVec = outVector->vec.i ;191 outVec = outVector->vec.i32; 192 192 193 193 if(inN != outN) { -
trunk/psLib/src/collections/psVector.c
r668 r670 3 3 * @brief Contains support for basic vector types 4 4 * 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: 6 6 * char 7 7 * short … … 19 19 * @author Ross Harman, MHPCC 20 20 * 21 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $22 * @date $Date: 2004-05-13 2 0:25:00$21 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 22 * @date $Date: 2004-05-13 22:47:52 $ 23 23 * 24 24 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 31 31 #include "psError.h" 32 32 #include "psVector.h" 33 #include "psLogMsg.h" 33 34 34 35 /******************************************************************************/ … … 60 61 /*****************************************************************************/ 61 62 62 static void* p_psVectorAlloc(psVector *restrict psVec, psElemType elemType, int nalloc, int elemSize) 63 /*****************************************************************************/ 64 /* FUNCTION IMPLEMENTATION - PUBLIC */ 65 /*****************************************************************************/ 66 psVector* psVectorAlloc(psElemType elemType, unsigned int nalloc) 63 67 { 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)); 65 85 66 86 psVec->type.dimen = PS_DIMEN_VECTOR; … … 69 89 psVec->n = 0; 70 90 71 if(nalloc != 0) { 72 vec = psAlloc(nalloc*sizeof(elemSize)); 73 } 91 // Create vector data array 92 psVec->vec.v = psAlloc(nalloc*elementSize); 74 93 75 return vec;94 return psVec; 76 95 } 77 96 78 static void* p_psVectorRealloc(psVector *restrict psVec, void *vec, int nalloc, int elemSize)97 psVector *psVectorRealloc(psVector *restrict psVec, unsigned int nalloc) 79 98 { 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; 99 101 100 102 // Invalid nalloc 101 if(nalloc < = 0) {103 if(nalloc < 1) { 102 104 psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc); 103 105 return NULL; 104 106 } 105 107 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); 148 112 } 149 113 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 nalloc160 if(nalloc < 0) {161 psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);162 return NULL;163 }164 114 165 115 if(psVec == NULL) { // For creating new psVector 166 116 psVec = psVectorAlloc(psVec->type.type, nalloc); 167 117 } 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; 206 127 } 207 128 } … … 212 133 void psVectorFree(psVector *restrict psVec) 213 134 { 214 psElemType vecType = 0;215 216 vecType = psVec->type.type;217 218 135 if (psVec == NULL) { 219 136 return; 220 137 } 221 138 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); 260 146 } 261 262 // Free vector struct263 psFree(psVec);264 147 } 265 148 266 psVector* psVoidPtrVectorAlloc(int nalloc)149 void psVectorElementFree(psVector *restrict psVec, void (*elemFree)(void *)) 267 150 { 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 size279 psMemDecrRefCounter(psVec->vec.vp[i]);280 }281 282 if(psVec == NULL) { // For creating new psVector283 psVec = psVoidPtrVectorAlloc(nalloc);284 } else if(psVec->nalloc != nalloc) { // No need to realloc to same size285 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;294 151 295 152 if(psVec == NULL) { … … 297 154 } 298 155 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++) { 300 162 if(elemFree == NULL) { 301 163 psMemDecrRefCounter(psVec->vec.vp[i]); … … 303 165 elemFree(psMemDecrRefCounter(psVec->vec.vp[i])); 304 166 } 167 psVec->vec.vp[i] = NULL; 305 168 } 306 307 // Free pointer vector308 psFree(psVec->vec.vp);309 psFree(psVec);310 169 } 311 170 -
trunk/psLib/src/collections/psVector.h
r663 r670 19 19 * @author Ross Harman, MHPCC 20 20 * 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 $ 23 23 * 24 24 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 60 60 PS_TYPE_COMPLEX_FLOAT = 0x84, ///< Complex numbers consisting of single-precision floating point. 61 61 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.} 63 63 } psElemType; 64 64 … … 118 118 double *d; ///< Pointers to double precision data. 119 119 complex float *cf; ///< Pointers to complex floating point data. 120 void *v; ///< Pointers to generic void data 120 121 void **vp; ///< Void pointer vector. 121 122 }vec; ///< Union for data types. … … 135 136 */ 136 137 psVector *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. 139 140 ); 140 141 … … 164 165 ); 165 166 166 /** Allocate a void pointer vector. 167 168 /** Deallocate/Dereference elements of a void pointer vector. 167 169 * 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. 201 174 * 202 175 */ -
trunk/psLib/src/mathtypes/psVector.c
r668 r670 3 3 * @brief Contains support for basic vector types 4 4 * 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: 6 6 * char 7 7 * short … … 19 19 * @author Ross Harman, MHPCC 20 20 * 21 * @version $Revision: 1. 2$ $Name: not supported by cvs2svn $22 * @date $Date: 2004-05-13 2 0:25:00$21 * @version $Revision: 1.3 $ $Name: not supported by cvs2svn $ 22 * @date $Date: 2004-05-13 22:47:52 $ 23 23 * 24 24 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 31 31 #include "psError.h" 32 32 #include "psVector.h" 33 #include "psLogMsg.h" 33 34 34 35 /******************************************************************************/ … … 60 61 /*****************************************************************************/ 61 62 62 static void* p_psVectorAlloc(psVector *restrict psVec, psElemType elemType, int nalloc, int elemSize) 63 /*****************************************************************************/ 64 /* FUNCTION IMPLEMENTATION - PUBLIC */ 65 /*****************************************************************************/ 66 psVector* psVectorAlloc(psElemType elemType, unsigned int nalloc) 63 67 { 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)); 65 85 66 86 psVec->type.dimen = PS_DIMEN_VECTOR; … … 69 89 psVec->n = 0; 70 90 71 if(nalloc != 0) { 72 vec = psAlloc(nalloc*sizeof(elemSize)); 73 } 91 // Create vector data array 92 psVec->vec.v = psAlloc(nalloc*elementSize); 74 93 75 return vec;94 return psVec; 76 95 } 77 96 78 static void* p_psVectorRealloc(psVector *restrict psVec, void *vec, int nalloc, int elemSize)97 psVector *psVectorRealloc(psVector *restrict psVec, unsigned int nalloc) 79 98 { 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; 99 101 100 102 // Invalid nalloc 101 if(nalloc < = 0) {103 if(nalloc < 1) { 102 104 psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc); 103 105 return NULL; 104 106 } 105 107 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); 148 112 } 149 113 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 nalloc160 if(nalloc < 0) {161 psError(__func__, " : Line %d - Invalid value for nalloc. nalloc: %d\n", __LINE__, nalloc);162 return NULL;163 }164 114 165 115 if(psVec == NULL) { // For creating new psVector 166 116 psVec = psVectorAlloc(psVec->type.type, nalloc); 167 117 } 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; 206 127 } 207 128 } … … 212 133 void psVectorFree(psVector *restrict psVec) 213 134 { 214 psElemType vecType = 0;215 216 vecType = psVec->type.type;217 218 135 if (psVec == NULL) { 219 136 return; 220 137 } 221 138 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); 260 146 } 261 262 // Free vector struct263 psFree(psVec);264 147 } 265 148 266 psVector* psVoidPtrVectorAlloc(int nalloc)149 void psVectorElementFree(psVector *restrict psVec, void (*elemFree)(void *)) 267 150 { 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 size279 psMemDecrRefCounter(psVec->vec.vp[i]);280 }281 282 if(psVec == NULL) { // For creating new psVector283 psVec = psVoidPtrVectorAlloc(nalloc);284 } else if(psVec->nalloc != nalloc) { // No need to realloc to same size285 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;294 151 295 152 if(psVec == NULL) { … … 297 154 } 298 155 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++) { 300 162 if(elemFree == NULL) { 301 163 psMemDecrRefCounter(psVec->vec.vp[i]); … … 303 165 elemFree(psMemDecrRefCounter(psVec->vec.vp[i])); 304 166 } 167 psVec->vec.vp[i] = NULL; 305 168 } 306 307 // Free pointer vector308 psFree(psVec->vec.vp);309 psFree(psVec);310 169 } 311 170 -
trunk/psLib/src/mathtypes/psVector.h
r663 r670 19 19 * @author Ross Harman, MHPCC 20 20 * 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 $ 23 23 * 24 24 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 60 60 PS_TYPE_COMPLEX_FLOAT = 0x84, ///< Complex numbers consisting of single-precision floating point. 61 61 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.} 63 63 } psElemType; 64 64 … … 118 118 double *d; ///< Pointers to double precision data. 119 119 complex float *cf; ///< Pointers to complex floating point data. 120 void *v; ///< Pointers to generic void data 120 121 void **vp; ///< Void pointer vector. 121 122 }vec; ///< Union for data types. … … 135 136 */ 136 137 psVector *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. 139 140 ); 140 141 … … 164 165 ); 165 166 166 /** Allocate a void pointer vector. 167 168 /** Deallocate/Dereference elements of a void pointer vector. 167 169 * 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. 201 174 * 202 175 */
Note:
See TracChangeset
for help on using the changeset viewer.
