Changeset 12446


Ignore:
Timestamp:
06/18/12 13:09:02 (13 years ago)
Author:
utke
Message:

implement a templated version to replace xrealloc - as part of this also add the C equivalent of the C++ management routines and use cpp directives to filter ... as part of that since the memset isn't a proper equivalent of a NewInit (with a specific non-null value) I changed the NewInit to NewZeroInit to have a proper wrapper for calloc as used before.

Location:
issm/trunk-jpl/src/c
Files:
13 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp

    r12434 r12446  
    3939               
    4040                if(rows*cols){
    41                         matrix=xNewInit<double>(rows*cols,0.0);
     41                        matrix=xNewZeroInit<double>(rows*cols);
    4242
    4343                        /*Now, get ir,jc and pr: */
     
    6464                /*Create serial matrix: */
    6565                if(rows*cols){
    66                         matrix=xNewInit<double>(rows*cols,0.0);
     66                        matrix=xNewZeroInit<double>(rows*cols);
    6767
    6868                        for(i=0;i<rows;i++){
     
    8181                /*Create serial matrix: */
    8282                if(rows*cols){
    83                         matrix=xNewInit<double>(rows*cols,0.0);
     83                        matrix=xNewZeroInit<double>(rows*cols);
    8484
    8585                        for(i=0;i<rows;i++){
     
    9898                /*Create serial matrix: */
    9999                if(rows*cols){
    100                         matrix=xNewInit<double>(rows*cols,0.0);
     100                        matrix=xNewZeroInit<double>(rows*cols);
    101101
    102102                        for(i=0;i<rows;i++){
  • issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp

    r12434 r12446  
    5151                nz=(int)((double)nnz/(double)rows);
    5252
    53                 matrix=xNewInit<double>(rows*cols,0.0);
     53                matrix=xNewZeroInit<double>(rows*cols);
    5454
    5555                /*Now, get ir,jc and pr: */
     
    7474               
    7575                /*Create serial matrix: */
    76                 matrix=xNewInit<double>(numel,0.0);
     76                matrix=xNewZeroInit<double>(numel);
    7777
    7878                dims=xNew<int>(ndims);
     
    131131                nz=(int)((double)nnz/(double)rows);
    132132
    133                 matrix=xNewInit<bool>(rows*cols,false);
     133                matrix=xNewZeroInit<bool>(rows*cols);
    134134
    135135                /*Now, get ir,jc and pm: */
  • issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp

    r12434 r12446  
    5151
    5252                if(rows){
    53                         vector=xNewInit<double>(rows,0.0);
     53                        vector=xNewZeroInit<double>(rows);
    5454
    5555                        /*Now, get ir,jc and pr: */
  • issm/trunk-jpl/src/c/modules/KMLMeshWritex/KMLMeshWritex.cpp

    r12442 r12446  
    120120                _printf_(true,"Creating the node connectivity table.\n");
    121121                nncon=mxepg+1;
    122                 nodecon=xNewInit<int>(mncon*nncon,0);
     122                nodecon=xNewZeroInit<int>(mncon*nncon);
    123123                ncfree=true;
    124124
     
    150150                else if (mdata == mncon) {
    151151                        _printf_(true,"Averaging nodal data to element data.\n");
    152                         edata=xNewInit<double>(melem*ndata,0);
     152                        edata=xNewZeroInit<double>(melem*ndata);
    153153                        edfree=true;
    154154
  • issm/trunk-jpl/src/c/modules/Krigingx/pKrigingx.cpp

    r12442 r12446  
    3737
    3838        /*Allocate output*/
    39         predictions =xNewInit<double>(n_interp,0.0);
    40         error       =xNewInit<double>(n_interp,0.0);
     39        predictions =xNewZeroInit<double>(n_interp);
     40        error       =xNewZeroInit<double>(n_interp);
    4141
    4242        /*Get output*/
  • issm/trunk-jpl/src/c/objects/DakotaPlugin.cpp

    r12354 r12446  
    8585
    8686        /*Initialize responses: */
    87         responses=xNewInit<IssmDouble>(numFns,0.0);
     87        responses=xNewZeroInit<IssmDouble>(numFns);
    8888
    8989        /*run core solution: */
  • issm/trunk-jpl/src/c/objects/Numerics/ElementMatrix.cpp

    r12441 r12446  
    101101        /*Gset and values*/
    102102        this->gglobaldoflist=xNew<int>(this->nrows);
    103         this->values=xNewInit<double>(this->nrows*this->ncols,0.0);
     103        this->values=xNewZeroInit<double>(this->nrows*this->ncols);
    104104        for(i=0;i<Ke1->nrows;i++){
    105105                for(j=0;j<Ke1->ncols;j++){
     
    205205
    206206        /*fill values with 0: */
    207         this->values=xNewInit<double>(this->nrows*this->ncols,0.0);
     207        this->values=xNewZeroInit<double>(this->nrows*this->ncols);
    208208
    209209        /*g list*/
  • issm/trunk-jpl/src/c/objects/Numerics/ElementVector.cpp

    r12441 r12446  
    7575        /*Gset and values*/
    7676        this->gglobaldoflist=xNew<int>(this->nrows);
    77         this->values=xNewInit<double>(this->nrows,0.0);
     77        this->values=xNewZeroInit<double>(this->nrows);
    7878        for(i=0;i<pe1->nrows;i++){
    7979                this->values[i] += pe1->values[i];
     
    138138
    139139        /*fill values with 0: */
    140         this->values=xNewInit<double>(this->nrows,0.0);
     140        this->values=xNewZeroInit<double>(this->nrows);
    141141       
    142142        /*g list*/
  • issm/trunk-jpl/src/c/shared/Alloc/xNewDelete.h

    r12353 r12446  
    88#include <cassert>
    99
     10
     11// memory management of types
     12// T with non-trivial constructors require
     13// C++ style memory management
     14#define USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     15// but for speed on may alternatively use C memory managment
     16// but can do so safely only for T that are at most
     17// plain old data structures (POD)
     18#ifndef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     19#include <cstdlib>
     20#endif
     21
    1022template <class T>
    1123T* xNew(unsigned int size) {
     24#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
    1225  T* aT_p=new T[size];
    1326  assert(aT_p);
    1427  return aT_p;
    15 };
     28#else
     29  T* aT_p=(T*)malloc(size*sizeof(T));
     30  assert(aT_p);
     31  return aT_p;
     32#endif 
     33}
    1634
    1735template <class T>
    18 T* xNewInit(unsigned int size, const T initVal) {
    19   T* aT_p=new T[size];
     36T* xNewZeroInit(unsigned int size) {
     37#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     38  T* aT_p=xNew<T>(size);
     39  for (unsigned int i=0; i<size;++i)
     40    aT_p[i]=(T)0;
     41  return aT_p;
     42#else
     43  T* aT_p=(T*)calloc(size,sizeof(T));
    2044  assert(aT_p);
    21   for (int i=0; i<size;++i)
    22     aT_p[i]=initVal;
    2345  return aT_p;
    24 };
     46#endif
     47}
    2548
    2649template <class T>
    2750void xDelete(T*& aT_p) {
    2851  if (aT_p)
     52#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
    2953    delete []aT_p;
     54#else
     55    free((void*)aT_p);
     56#endif
    3057  aT_p=0;
    31 };
     58}
     59
     60template <class T>
     61T* xReNew(T* old, unsigned int size) {
     62#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     63  assert(old);
     64  T* aT_p=xNew<T>(size);
     65  for (unsigned int i=0; i<size;++i)
     66    aT_p[i]=old[i];
     67  xDelete<T>(old);
     68  return aT_p;
     69#else
     70  T* aT_p=(T*)realloc((void*)old,size*sizeof(T));
     71  assert(aT_p);
     72  return aT_p;
     73#endif
     74}
    3275
    3376#endif
  • issm/trunk-jpl/src/c/shared/TriMesh/SplitMeshForRifts.cpp

    r12440 r12446  
    5252
    5353        /*Go through all nodes of the rift segments, and start splitting the mesh: */
    54         flags=xNewInit<int>(nods,0); //to make sure we don't split the same nodes twice!
     54        flags=xNewZeroInit<int>(nods); //to make sure we don't split the same nodes twice!
    5555        for (i=0;i<nriftsegs;i++){
    5656                for (j=0;j<2;j++){
  • issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp

    r12440 r12446  
    6666                                else{
    6767                                        /*Reallocate another max_number_elements slots in the GridElements: */
    68                                         GridElementsRealloc=(int*)xrealloc(GridElements,(current_size+max_number_elements)*sizeof(int));
     68                                        GridElementsRealloc=xReNew<int>(GridElements,(current_size+max_number_elements));
    6969                                        if (!GridElementsRealloc){
    7070                                                noerr=0;
     
    129129
    130130        /*Allocate segmentflags: */
    131         riftsegments_uncompressed=xNewInit<int>(nsegs*5,0);
     131        riftsegments_uncompressed=xNewZeroInit<int>(nsegs*5);
    132132
    133133        /*Find the segments that belong to a rift: they are the ones that see two elements. The other ones belong to a boundary
     
    905905
    906906                /*allocate riftpenaltypairs, and riftnumpenaltypairs: */
    907                 if((numsegs/2-1)!=0)riftpenaltypairs=xNewInit<double>((numsegs/2-1)*RIFTPENALTYPAIRSWIDTH,0.0);
     907                if((numsegs/2-1)!=0)riftpenaltypairs=xNewZeroInit<double>((numsegs/2-1)*RIFTPENALTYPAIRSWIDTH);
    908908               
    909909                /*Go through only one flank of the rifts, not counting the tips: */
  • issm/trunk-jpl/src/c/toolkits/issm/SeqMat.cpp

    r12429 r12446  
    3333        this->N=pN;
    3434        this->matrix=NULL;
    35         if(M*N) this->matrix=xNewInit<double>(pM*pN,0.0);
     35        if(M*N) this->matrix=xNewZeroInit<double>(pM*pN);
    3636}
    3737/*}}}*/
     
    4242        this->N=pN;
    4343        this->matrix=NULL;
    44         if(M*N) this->matrix=xNewInit<double>(pM*pN,0.0);
     44        if(M*N) this->matrix=xNewZeroInit<double>(pM*pN);
    4545}
    4646/*}}}*/
     
    5454        this->matrix=NULL;
    5555        if(M*N){
    56                 this->matrix=xNewInit<double>(pM*pN,0.0);
     56                this->matrix=xNewZeroInit<double>(pM*pN);
    5757                memcpy(this->matrix,serial_mat,pM*pN*sizeof(double));
    5858        }
     
    6666        this->N=pN;
    6767        this->matrix=NULL;
    68         if(M*N) this->matrix=xNewInit<double>(pM*pN,0.0);
     68        if(M*N) this->matrix=xNewZeroInit<double>(pM*pN);
    6969}
    7070/*}}}*/
  • issm/trunk-jpl/src/c/toolkits/issm/SeqVec.cpp

    r12429 r12446  
    3131        this->M=pM;
    3232        this->vector=NULL;
    33         if(this->M) this->vector=xNewInit<double>(pM,0.0);
     33        if(this->M) this->vector=xNewZeroInit<double>(pM);
    3434}
    3535/*}}}*/
     
    4242        this->vector=NULL;
    4343        if(this->M){
    44                 this->vector=xNewInit<double>(pM,0.0);
     44                this->vector=xNewZeroInit<double>(pM);
    4545                memcpy(this->vector,buffer,pM*sizeof(double));
    4646        }
Note: See TracChangeset for help on using the changeset viewer.