Changeset 12434


Ignore:
Timestamp:
06/15/12 16:43:53 (13 years ago)
Author:
Mathieu Morlighem
Message:

changing xmalloc to xNew and xfree to xDelete

Location:
issm/trunk-jpl/src/c/matlab/io
Files:
9 edited

Legend:

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

    r12397 r12434  
    9393        if (pndims)*pndims=outmatrix_ndims;
    9494        if (psize )*psize =outmatrix_size;
    95         else xfree((void**)&outmatrix_size);
     95        else xDelete<int>(outmatrix_size);
    9696
    9797}
     
    124124
    125125                        /*Convert double matrix into integer matrix: */
    126                         outmatrix=(int*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(int));
     126                        outmatrix=xNew<int>(outmatrix_rows*outmatrix_cols);
    127127                        for(i=0;i<outmatrix_rows*outmatrix_cols;i++)outmatrix[i]=(int)doublematrix[i];
    128128                }
     
    166166
    167167                        /*Convert double matrix into integer matrix: */
    168                         outmatrix=(bool*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(bool));
     168                        outmatrix=xNew<bool>(outmatrix_rows*outmatrix_cols);
    169169                        for(i=0;i<outmatrix_rows;i++)outmatrix[i]=(bool)doublematrix[i];
    170170                }
     
    227227
    228228                        /*Convert double matrix into bool matrix: */
    229                         outmatrix=(bool*)xmalloc(outmatrix_numel*sizeof(bool));
     229                        outmatrix=xNew<bool>(outmatrix_numel);
    230230                        for(i=0;i<outmatrix_numel;i++)outmatrix[i]=(bool)doublematrix[i];
    231                         xfree((void**)&doublematrix);
     231                        xDelete<double>(doublematrix);
    232232                }
    233233        }
     
    242242        if (pndims)*pndims=outmatrix_ndims;
    243243        if (psize )*psize =outmatrix_size;
    244         else xfree((void**)&outmatrix_size);
     244        else xDelete<int>(outmatrix_size);
    245245
    246246}
     
    292292
    293293                /*Convert double vector into integer vector: */
    294                 outvector=(int*)xmalloc(outvector_rows*sizeof(int));
     294                outvector=xNew<int>(outvector_rows);
    295295                for(i=0;i<outvector_rows;i++)outvector[i]=(int)doublevector[i];
    296296        }
     
    324324
    325325                /*Convert double vector into integer vector: */
    326                 outvector=(bool*)xmalloc(outvector_rows*sizeof(bool));
     326                outvector=xNew<bool>(outvector_rows);
    327327                for(i=0;i<outvector_rows;i++)outvector[i]=(bool)doublevector[i];
    328328        }
     
    356356
    357357                /*Convert double vector into float vector: */
    358                 outvector=(float*)xmalloc(outvector_rows*sizeof(float));
     358                outvector=xNew<float>(outvector_rows);
    359359                for(i=0;i<outvector_rows;i++)outvector[i]=(float)doublevector[i];
    360360        }
     
    384384               
    385385                stringlen = mxGetM(dataref)*mxGetN(dataref)+1;
    386                 outstring = (char*)xmalloc(sizeof(mxChar)*stringlen);
     386                outstring =xNew<char>(stringlen);
    387387                mxGetString(dataref,outstring,stringlen);
    388388        }
     
    420420        if (pndims)*pndims=outmatrix_ndims;
    421421        if (psize )*psize =outmatrix_size;
    422         else xfree((void**)&outmatrix_size);
     422        else xDelete<int>(outmatrix_size);
    423423
    424424}
  • issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp

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

    r12013 r12434  
    99#error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
    1010#endif
    11 
     11#include "../../shared/shared.h"
    1212
    1313/*Petsc includes: */
     
    1919#include "mex.h"
    2020
    21 #include "../../shared/shared.h"
    22 
    2321int MatlabMatrixToPetscMatrix(Mat* pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
    2422
    2523        int rows, cols;
    26         double* mxmatrix_ptr=NULL;
    27         double* tmatrix=NULL;
     24        double *mxmatrix_ptr = NULL;
     25        double *tmatrix      = NULL;
    2826        int ierr;
    2927        int i,j;
    3028
    3129        /*output: */
    32         Mat matrix=NULL;
     30        Mat matrix = NULL;
    3331
    3432        /*matlab indices: */
    35         mwIndex*    ir=NULL;
    36         mwIndex*    jc=NULL;
    37         double* pr=NULL;
     33        mwIndex *ir = NULL;
     34        mwIndex *jc = NULL;
     35        double  *pr = NULL;
    3836        int     count;
    3937        int     nnz;
     
    4139
    4240        /*petsc indices: */
    43         int* idxm=NULL;
    44         int* idxn=NULL;
     41        int *idxm = NULL;
     42        int *idxn = NULL;
    4543       
    4644        /*Ok, first check if we are dealing with a sparse or full matrix: */
     
    7472                        }
    7573                }
    76 
    7774        }
    7875        else{
    79 
    8076                /*Dealing with dense matrix: recover pointer and size: */
    8177                mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
     
    8480
    8581                /*transpose, as Petsc now does not allows MAT_COLUMN_ORIENTED matrices in MatSetValues: */
    86                 tmatrix=(double*)xmalloc(rows*cols*sizeof(double));
     82                tmatrix=xNew<double>(rows*cols);
    8783                for(i=0;i<cols;i++){
    8884                        for(j=0;j<rows;j++){
     
    9591
    9692                /*Insert mxmatrix_ptr values into petsc matrix: */
    97                 idxm=(int*)xmalloc(rows*sizeof(int));
    98                 idxn=(int*)xmalloc(cols*sizeof(int));
     93                idxm=xNew<int>(rows);
     94                idxn=xNew<int>(cols);
    9995
    10096                for(i=0;i<rows;i++)idxm[i]=i;
     
    10399                ierr=MatSetValues(matrix,rows,idxm,cols,idxn,tmatrix,INSERT_VALUES); CHKERRQ(ierr);
    104100
    105                 xfree((void**)&tmatrix);
    106 
     101                xDelete<double>(tmatrix);
    107102        }
    108103
     
    110105        MatAssemblyBegin(matrix,MAT_FINAL_ASSEMBLY);
    111106        MatAssemblyEnd(matrix,MAT_FINAL_ASSEMBLY);
    112 
    113107
    114108        /*Assign output pointer: */
  • issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp

    r12365 r12434  
    3939        ndims=mxGetNumberOfDimensions(mxmatrix);
    4040        ipt  =mxGetDimensions(mxmatrix);
    41         size =(int *) xcalloc(ndims,sizeof(int));
    42         for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
     41        size =xNew<int>(ndims);
     42        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    4343
    4444        /*Ok, first check if we are dealing with a sparse or full matrix: */
     
    5151                nz=(int)((double)nnz/(double)rows);
    5252
    53                 matrix=(double*)xcalloc(rows*cols,sizeof(double));
     53                matrix=xNewInit<double>(rows*cols,0.0);
    5454
    5555                /*Now, get ir,jc and pr: */
     
    7474               
    7575                /*Create serial matrix: */
    76                 matrix=(double*)xcalloc(numel,sizeof(double));
    77 
    78                 dims=(int *) xcalloc(ndims,sizeof(int));
     76                matrix=xNewInit<double>(numel,0.0);
     77
     78                dims=xNew<int>(ndims);
    7979                for(i=0;i<numel;i++){
    8080                        ColumnWiseDimsFromIndex(dims,i,size,ndims);
     
    8282                        *(matrix+j)=*(mxmatrix_ptr+i);
    8383                }
    84                 xfree((void**)&dims);
     84                xDelete<int>(dims);
    8585               
    8686        }
     
    119119        ndims=mxGetNumberOfDimensions(mxmatrix);
    120120        ipt  =mxGetDimensions(mxmatrix);
    121         size =(int *) xcalloc(ndims,sizeof(int));
    122         for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
     121        size =xNew<int>(ndims);
     122        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    123123
    124124        /*Ok, first check if we are dealing with a sparse or full matrix: */
     
    131131                nz=(int)((double)nnz/(double)rows);
    132132
    133                 matrix=(bool*)xcalloc(rows*cols,sizeof(bool));
     133                matrix=xNewInit<bool>(rows*cols,false);
    134134
    135135                /*Now, get ir,jc and pm: */
     
    154154               
    155155                /*Create serial matrix: */
    156                 matrix=(bool*)xcalloc(numel,sizeof(bool));
    157 
    158                 dims=(int *) xcalloc(ndims,sizeof(int));
     156                matrix=xNew<bool>(numel);
     157                dims=xNew<int>(ndims);
    159158                for(i=0;i<numel;i++){
    160159                        ColumnWiseDimsFromIndex(dims,i,size,ndims);
     
    162161                        *(matrix+j)=(bool)*(mxmatrix_ptr+i);
    163162                }
    164                 xfree((void**)&dims);
    165                
     163                xDelete<int>(dims);
    166164        }
    167165
     
    199197        ndims=mxGetNumberOfDimensions(mxmatrix);
    200198        ipt  =mxGetDimensions(mxmatrix);
    201         size =(int *) xcalloc(ndims,sizeof(int));
    202         for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
     199        size =xNew<int>(ndims);
     200        for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
    203201
    204202        /*Ok, first check if we are dealing with a sparse or full matrix: */
     
    211209                nz=(int)((double)nnz/(double)rows);
    212210
    213                 matrix=(char*)xcalloc(rows*cols,sizeof(double));
     211                matrix=xNew<char>(rows*cols);
    214212
    215213                /*Now, get ir,jc and pm: */
     
    234232               
    235233                /*Create serial matrix: */
    236                 matrix=(char*)xcalloc(numel+1,sizeof(mxChar));
     234                matrix=xNew<char>(numel+1);
    237235
    238236                /*looping code adapted from Matlab example explore.c: */
  • issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp

    r12011 r12434  
    5151
    5252                if(rows){
    53                         vector=(double*)xcalloc(rows,sizeof(double));
     53                        vector=xNewInit<double>(rows,0.0);
    5454
    5555                        /*Now, get ir,jc and pr: */
     
    8181                /*allocate and memcpy*/
    8282                if(rows){
    83                         vector=(double*)xmalloc(rows*sizeof(double));
     83                        vector=xNew<double>(rows);
    8484                        memcpy(vector,mxvector_ptr,rows*sizeof(double));
    8585                }
  • issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp

    r12013 r12434  
    7979
    8080                /*Insert mxvector_ptr values into petsc vector: */
    81                 idxm=(int*)xmalloc(rows*sizeof(int));
     81                idxm=xNew<int>(rows);
    8282
    8383                for(i=0;i<rows;i++)idxm[i]=i;
  • issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp

    r12365 r12434  
    2222        /*check and parse the name  */
    2323        odouble=new OptionDouble;
    24         odouble->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
     24        odouble->name =xNew<char>(strlen(name)+1);
    2525        memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
    2626
     
    2929                _error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",odouble->name,"double",odouble->name,mxGetClassName(prhs[0]));
    3030        }
    31 
    3231        FetchData(&odouble->values,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
    3332
     
    4140        /*check and parse the name  */
    4241        ological=new OptionLogical;
    43         ological->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
     42        ological->name =xNew<char>(strlen(name)+1);
    4443        memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
    4544
     
    4847                _error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ological->name,"logical",ological->name,mxGetClassName(prhs[0]));
    4948        }
    50 
    5149        FetchData(&ological->values,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
    5250
     
    6058        /*check and parse the name  */
    6159        ochar=new OptionChar();
    62         ochar->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
     60        ochar->name =xNew<char>(strlen(name)+1);
    6361        memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
    6462
     
    6765                _error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ochar->name,"char",ochar->name,mxGetClassName(prhs[0]));
    6866        }
    69 
    7067        FetchData(&ochar->values,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
    7168
     
    8582        /*check and parse the name  */
    8683        ostruct=new OptionStruct;
    87         ostruct->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
     84        ostruct->name =xNew<char>(strlen(name)+1);
    8885        memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
    8986
     
    9289                _error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ostruct->name,"struct",ostruct->name,mxGetClassName(prhs[0]));
    9390        }
    94 
    9591        ostruct->numel=mxGetNumberOfElements(prhs[0]);
    9692        ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
    9793        ipt           =mxGetDimensions(prhs[0]);
    98         ostruct->size =(int *) xmalloc(ostruct->ndims*sizeof(int));
     94        ostruct->size =xNew<int>(ostruct->ndims);
    9995        for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
    100         if (ostruct->numel) ostruct->values=(Options**) xmalloc(ostruct->numel*sizeof(Options *));
     96        if (ostruct->numel) ostruct->values=xNew<Options*>(ostruct->numel);
    10197
    10298        /*loop through and process each element of the struct array  */
     
    132128        /*check and parse the name  */
    133129        ocell=new OptionCell;
    134         ocell->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
     130        ocell->name =xNew<char>(strlen(name)+1);
    135131        memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
    136132
     
    143139        ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
    144140        ipt         =mxGetDimensions(prhs[0]);
    145         ocell->size =(int *) xmalloc(ocell->ndims*sizeof(int));
     141        ocell->size =xNew<int>(ocell->ndims);
    146142        for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
    147143        ocell->values=new Options;
    148144
    149145        /*loop through and process each element of the cell array  */
    150         dims=(int *) xmalloc(ocell->ndims*sizeof(int));
     146        dims=xNew<int>(ocell->ndims);
    151147        for (cindex=0; cindex<ocell->numel; cindex++) {
    152148                ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims);
     
    163159                option=NULL;
    164160        }
    165         xfree((void**)&dims);
     161        xDelete<int>(dims);
    166162
    167163        return(ocell);
  • issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp

    r12016 r12434  
    2727
    2828                /*allocate buffer for given string size*/
    29                 buffer=(char*)xmalloc(size*sizeof(char));
     29                buffer=xNew<char>(size);
    3030
    3131                /* Try to print in the allocated space. */
     
    4747                 size*=2;  /* twice the old size */
    4848
    49                 xfree((void**)&buffer);
     49                xDelete<char>(buffer);
    5050        }
    5151
     
    5454
    5555        /*Clean up and return*/
    56         xfree((void**)&buffer);
     56        xDelete<char>(buffer);
    5757        return 1;
    5858}
  • issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp

    r12365 r12434  
    249249
    250250                /*Free ressources:*/
    251                 xfree((void**)&matrix_ptr);
     251                xDelete<double>(matrix_ptr);
    252252
    253253        }
     
    290290
    291291        /*Clean-up and return*/
    292         xfree((void**)&vector_ptr);
     292        xDelete<double>(vector_ptr);
    293293        *pdataref=dataref;
    294294}
Note: See TracChangeset for help on using the changeset viewer.