Index: /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/FetchMatlabData.cpp	(revision 12434)
@@ -93,5 +93,5 @@
 	if (pndims)*pndims=outmatrix_ndims;
 	if (psize )*psize =outmatrix_size;
-	else xfree((void**)&outmatrix_size);
+	else xDelete<int>(outmatrix_size);
 
 }
@@ -124,5 +124,5 @@
 
 			/*Convert double matrix into integer matrix: */
-			outmatrix=(int*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(int));
+			outmatrix=xNew<int>(outmatrix_rows*outmatrix_cols);
 			for(i=0;i<outmatrix_rows*outmatrix_cols;i++)outmatrix[i]=(int)doublematrix[i];
 		}
@@ -166,5 +166,5 @@
 
 			/*Convert double matrix into integer matrix: */
-			outmatrix=(bool*)xmalloc(outmatrix_rows*outmatrix_cols*sizeof(bool));
+			outmatrix=xNew<bool>(outmatrix_rows*outmatrix_cols);
 			for(i=0;i<outmatrix_rows;i++)outmatrix[i]=(bool)doublematrix[i];
 		}
@@ -227,7 +227,7 @@
 
 			/*Convert double matrix into bool matrix: */
-			outmatrix=(bool*)xmalloc(outmatrix_numel*sizeof(bool));
+			outmatrix=xNew<bool>(outmatrix_numel);
 			for(i=0;i<outmatrix_numel;i++)outmatrix[i]=(bool)doublematrix[i];
-			xfree((void**)&doublematrix);
+			xDelete<double>(doublematrix);
 		}
 	}
@@ -242,5 +242,5 @@
 	if (pndims)*pndims=outmatrix_ndims;
 	if (psize )*psize =outmatrix_size;
-	else xfree((void**)&outmatrix_size);
+	else xDelete<int>(outmatrix_size);
 
 }
@@ -292,5 +292,5 @@
 
 		/*Convert double vector into integer vector: */
-		outvector=(int*)xmalloc(outvector_rows*sizeof(int));
+		outvector=xNew<int>(outvector_rows);
 		for(i=0;i<outvector_rows;i++)outvector[i]=(int)doublevector[i];
 	}
@@ -324,5 +324,5 @@
 
 		/*Convert double vector into integer vector: */
-		outvector=(bool*)xmalloc(outvector_rows*sizeof(bool));
+		outvector=xNew<bool>(outvector_rows);
 		for(i=0;i<outvector_rows;i++)outvector[i]=(bool)doublevector[i];
 	}
@@ -356,5 +356,5 @@
 
 		/*Convert double vector into float vector: */
-		outvector=(float*)xmalloc(outvector_rows*sizeof(float));
+		outvector=xNew<float>(outvector_rows);
 		for(i=0;i<outvector_rows;i++)outvector[i]=(float)doublevector[i];
 	}
@@ -384,5 +384,5 @@
 		
 		stringlen = mxGetM(dataref)*mxGetN(dataref)+1;
-		outstring = (char*)xmalloc(sizeof(mxChar)*stringlen);
+		outstring =xNew<char>(stringlen);
 		mxGetString(dataref,outstring,stringlen);
 	}
@@ -420,5 +420,5 @@
 	if (pndims)*pndims=outmatrix_ndims;
 	if (psize )*psize =outmatrix_size;
-	else xfree((void**)&outmatrix_size);
+	else xDelete<int>(outmatrix_size);
 
 }
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToDoubleMatrix.cpp	(revision 12434)
@@ -39,5 +39,5 @@
 		
 		if(rows*cols){
-			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+			matrix=xNewInit<double>(rows*cols,0.0);
 
 			/*Now, get ir,jc and pr: */
@@ -64,5 +64,5 @@
 		/*Create serial matrix: */
 		if(rows*cols){
-			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+			matrix=xNewInit<double>(rows*cols,0.0);
 
 			for(i=0;i<rows;i++){
@@ -81,5 +81,5 @@
 		/*Create serial matrix: */
 		if(rows*cols){
-			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+			matrix=xNewInit<double>(rows*cols,0.0);
 
 			for(i=0;i<rows;i++){
@@ -98,5 +98,5 @@
 		/*Create serial matrix: */
 		if(rows*cols){
-			matrix=(double*)xcalloc(rows*cols,sizeof(double));
+			matrix=xNewInit<double>(rows*cols,0.0);
 
 			for(i=0;i<rows;i++){
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabMatrixToPetscMatrix.cpp	(revision 12434)
@@ -9,5 +9,5 @@
 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
 #endif
-
+#include "../../shared/shared.h"
 
 /*Petsc includes: */
@@ -19,21 +19,19 @@
 #include "mex.h"
 
-#include "../../shared/shared.h"
-
 int MatlabMatrixToPetscMatrix(Mat* pmatrix,int* pmatrix_rows,int* pmatrix_cols,const mxArray* mxmatrix){
 
 	int rows, cols;
-	double* mxmatrix_ptr=NULL;
-	double* tmatrix=NULL;
+	double *mxmatrix_ptr = NULL;
+	double *tmatrix      = NULL;
 	int ierr;
 	int i,j;
 
 	/*output: */
-	Mat matrix=NULL;
+	Mat matrix = NULL;
 
 	/*matlab indices: */
-	mwIndex*    ir=NULL;
-	mwIndex*    jc=NULL;
-	double* pr=NULL;
+	mwIndex *ir = NULL;
+	mwIndex *jc = NULL;
+	double  *pr = NULL;
 	int     count;
 	int     nnz;
@@ -41,6 +39,6 @@
 
 	/*petsc indices: */
-	int* idxm=NULL;
-	int* idxn=NULL;
+	int *idxm = NULL;
+	int *idxn = NULL;
 	
 	/*Ok, first check if we are dealing with a sparse or full matrix: */
@@ -74,8 +72,6 @@
 			}
 		}
-
 	}
 	else{
-
 		/*Dealing with dense matrix: recover pointer and size: */
 		mxmatrix_ptr=(double*)mxGetPr(mxmatrix);
@@ -84,5 +80,5 @@
 
 		/*transpose, as Petsc now does not allows MAT_COLUMN_ORIENTED matrices in MatSetValues: */
-		tmatrix=(double*)xmalloc(rows*cols*sizeof(double));
+		tmatrix=xNew<double>(rows*cols);
 		for(i=0;i<cols;i++){
 			for(j=0;j<rows;j++){
@@ -95,6 +91,6 @@
 
 		/*Insert mxmatrix_ptr values into petsc matrix: */
-		idxm=(int*)xmalloc(rows*sizeof(int));
-		idxn=(int*)xmalloc(cols*sizeof(int));
+		idxm=xNew<int>(rows);
+		idxn=xNew<int>(cols);
 
 		for(i=0;i<rows;i++)idxm[i]=i;
@@ -103,6 +99,5 @@
 		ierr=MatSetValues(matrix,rows,idxm,cols,idxn,tmatrix,INSERT_VALUES); CHKERRQ(ierr);
 
-		xfree((void**)&tmatrix);
-
+		xDelete<double>(tmatrix);
 	}
 
@@ -110,5 +105,4 @@
 	MatAssemblyBegin(matrix,MAT_FINAL_ASSEMBLY); 
 	MatAssemblyEnd(matrix,MAT_FINAL_ASSEMBLY);
-
 
 	/*Assign output pointer: */
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabNArrayToNArray.cpp	(revision 12434)
@@ -39,6 +39,6 @@
 	ndims=mxGetNumberOfDimensions(mxmatrix);
 	ipt  =mxGetDimensions(mxmatrix);
-	size =(int *) xcalloc(ndims,sizeof(int));
-	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+	size =xNew<int>(ndims);
+	for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
 
 	/*Ok, first check if we are dealing with a sparse or full matrix: */
@@ -51,5 +51,5 @@
 		nz=(int)((double)nnz/(double)rows);
 
-		matrix=(double*)xcalloc(rows*cols,sizeof(double));
+		matrix=xNewInit<double>(rows*cols,0.0);
 
 		/*Now, get ir,jc and pr: */
@@ -74,7 +74,7 @@
 		
 		/*Create serial matrix: */
-		matrix=(double*)xcalloc(numel,sizeof(double));
-
-		dims=(int *) xcalloc(ndims,sizeof(int));
+		matrix=xNewInit<double>(numel,0.0);
+
+		dims=xNew<int>(ndims);
 		for(i=0;i<numel;i++){
 			ColumnWiseDimsFromIndex(dims,i,size,ndims);
@@ -82,5 +82,5 @@
 			*(matrix+j)=*(mxmatrix_ptr+i);
 		}
-		xfree((void**)&dims);
+		xDelete<int>(dims);
 		
 	}
@@ -119,6 +119,6 @@
 	ndims=mxGetNumberOfDimensions(mxmatrix);
 	ipt  =mxGetDimensions(mxmatrix);
-	size =(int *) xcalloc(ndims,sizeof(int));
-	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+	size =xNew<int>(ndims);
+	for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
 
 	/*Ok, first check if we are dealing with a sparse or full matrix: */
@@ -131,5 +131,5 @@
 		nz=(int)((double)nnz/(double)rows);
 
-		matrix=(bool*)xcalloc(rows*cols,sizeof(bool));
+		matrix=xNewInit<bool>(rows*cols,false);
 
 		/*Now, get ir,jc and pm: */
@@ -154,7 +154,6 @@
 		
 		/*Create serial matrix: */
-		matrix=(bool*)xcalloc(numel,sizeof(bool));
-
-		dims=(int *) xcalloc(ndims,sizeof(int));
+		matrix=xNew<bool>(numel);
+		dims=xNew<int>(ndims);
 		for(i=0;i<numel;i++){
 			ColumnWiseDimsFromIndex(dims,i,size,ndims);
@@ -162,6 +161,5 @@
 			*(matrix+j)=(bool)*(mxmatrix_ptr+i);
 		}
-		xfree((void**)&dims);
-		
+		xDelete<int>(dims);
 	}
 
@@ -199,6 +197,6 @@
 	ndims=mxGetNumberOfDimensions(mxmatrix);
 	ipt  =mxGetDimensions(mxmatrix);
-	size =(int *) xcalloc(ndims,sizeof(int));
-	for (i=0; i<ndims; i++) size[i]=(int)ipt[i];
+	size =xNew<int>(ndims);
+	for (i=0;i<ndims;i++) size[i]=(int)ipt[i];
 
 	/*Ok, first check if we are dealing with a sparse or full matrix: */
@@ -211,5 +209,5 @@
 		nz=(int)((double)nnz/(double)rows);
 
-		matrix=(char*)xcalloc(rows*cols,sizeof(double));
+		matrix=xNew<char>(rows*cols);
 
 		/*Now, get ir,jc and pm: */
@@ -234,5 +232,5 @@
 		
 		/*Create serial matrix: */
-		matrix=(char*)xcalloc(numel+1,sizeof(mxChar));
+		matrix=xNew<char>(numel+1);
 
 		/*looping code adapted from Matlab example explore.c: */
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToDoubleVector.cpp	(revision 12434)
@@ -51,5 +51,5 @@
 
 		if(rows){
-			vector=(double*)xcalloc(rows,sizeof(double));
+			vector=xNewInit<double>(rows,0.0);
 
 			/*Now, get ir,jc and pr: */
@@ -81,5 +81,5 @@
 		/*allocate and memcpy*/
 		if(rows){
-			vector=(double*)xmalloc(rows*sizeof(double));
+			vector=xNew<double>(rows);
 			memcpy(vector,mxvector_ptr,rows*sizeof(double));
 		}
Index: /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/MatlabVectorToPetscVector.cpp	(revision 12434)
@@ -79,5 +79,5 @@
 
 		/*Insert mxvector_ptr values into petsc vector: */
-		idxm=(int*)xmalloc(rows*sizeof(int));
+		idxm=xNew<int>(rows);
 
 		for(i=0;i<rows;i++)idxm[i]=i;
Index: /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/OptionParse.cpp	(revision 12434)
@@ -22,5 +22,5 @@
 	/*check and parse the name  */
 	odouble=new OptionDouble;
-	odouble->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	odouble->name =xNew<char>(strlen(name)+1);
 	memcpy(odouble->name,name,(strlen(name)+1)*sizeof(char));
 
@@ -29,5 +29,4 @@
 		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",odouble->name,"double",odouble->name,mxGetClassName(prhs[0]));
 	}
-
 	FetchData(&odouble->values,&odouble->numel,&odouble->ndims,&odouble->size,prhs[0]);
 
@@ -41,5 +40,5 @@
 	/*check and parse the name  */
 	ological=new OptionLogical;
-	ological->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
+	ological->name =xNew<char>(strlen(name)+1);
 	memcpy(ological->name,name,(strlen(name)+1)*sizeof(char));
 
@@ -48,5 +47,4 @@
 		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ological->name,"logical",ological->name,mxGetClassName(prhs[0]));
 	}
-
 	FetchData(&ological->values,&ological->numel,&ological->ndims,&ological->size,prhs[0]);
 
@@ -60,5 +58,5 @@
 	/*check and parse the name  */
 	ochar=new OptionChar();
-	ochar->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
+	ochar->name =xNew<char>(strlen(name)+1);
 	memcpy(ochar->name,name,(strlen(name)+1)*sizeof(char));
 
@@ -67,5 +65,4 @@
 		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ochar->name,"char",ochar->name,mxGetClassName(prhs[0]));
 	}
-
 	FetchData(&ochar->values,&ochar->numel,&ochar->ndims,&ochar->size,prhs[0]);
 
@@ -85,5 +82,5 @@
 	/*check and parse the name  */
 	ostruct=new OptionStruct;
-	ostruct->name =(char*)xmalloc((strlen(name)+1)*sizeof(char));
+	ostruct->name =xNew<char>(strlen(name)+1);
 	memcpy(ostruct->name,name,(strlen(name)+1)*sizeof(char));
 
@@ -92,11 +89,10 @@
 		_error_("Value of option \"%s\" must be class \"%s\", not class \"%s\".",ostruct->name,"struct",ostruct->name,mxGetClassName(prhs[0]));
 	}
-
 	ostruct->numel=mxGetNumberOfElements(prhs[0]);
 	ostruct->ndims=mxGetNumberOfDimensions(prhs[0]);
 	ipt           =mxGetDimensions(prhs[0]);
-	ostruct->size =(int *) xmalloc(ostruct->ndims*sizeof(int));
+	ostruct->size =xNew<int>(ostruct->ndims);
 	for (i=0; i<ostruct->ndims; i++) ostruct->size[i]=(int)ipt[i];
-	if (ostruct->numel) ostruct->values=(Options**) xmalloc(ostruct->numel*sizeof(Options *));
+	if (ostruct->numel) ostruct->values=xNew<Options*>(ostruct->numel);
 
 	/*loop through and process each element of the struct array  */
@@ -132,5 +128,5 @@
 	/*check and parse the name  */
 	ocell=new OptionCell;
-	ocell->name =(char *) xmalloc((strlen(name)+1)*sizeof(char));
+	ocell->name =xNew<char>(strlen(name)+1);
 	memcpy(ocell->name,name,(strlen(name)+1)*sizeof(char));
 
@@ -143,10 +139,10 @@
 	ocell->ndims=mxGetNumberOfDimensions(prhs[0]);
 	ipt         =mxGetDimensions(prhs[0]);
-	ocell->size =(int *) xmalloc(ocell->ndims*sizeof(int));
+	ocell->size =xNew<int>(ocell->ndims);
 	for (i=0; i<ocell->ndims; i++) ocell->size[i]=(int)ipt[i];
 	ocell->values=new Options;
 
 	/*loop through and process each element of the cell array  */
-	dims=(int *) xmalloc(ocell->ndims*sizeof(int));
+	dims=xNew<int>(ocell->ndims);
 	for (cindex=0; cindex<ocell->numel; cindex++) {
 		ColumnWiseDimsFromIndex(dims,(int)cindex,ocell->size,ocell->ndims);
@@ -163,5 +159,5 @@
 		option=NULL;
 	}
-	xfree((void**)&dims);
+	xDelete<int>(dims);
 
 	return(ocell);
Index: /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/PrintfFunction.cpp	(revision 12434)
@@ -27,5 +27,5 @@
 
 		/*allocate buffer for given string size*/
-		buffer=(char*)xmalloc(size*sizeof(char));
+		buffer=xNew<char>(size);
 
 		/* Try to print in the allocated space. */
@@ -47,5 +47,5 @@
 		 size*=2;  /* twice the old size */
 
-		xfree((void**)&buffer);
+		xDelete<char>(buffer);
 	}
 
@@ -54,5 +54,5 @@
 
 	/*Clean up and return*/
-	xfree((void**)&buffer);
+	xDelete<char>(buffer);
 	return 1;
 }
Index: /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp
===================================================================
--- /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12433)
+++ /issm/trunk-jpl/src/c/matlab/io/WriteMatlabData.cpp	(revision 12434)
@@ -249,5 +249,5 @@
 
 		/*Free ressources:*/
-		xfree((void**)&matrix_ptr);
+		xDelete<double>(matrix_ptr);
 
 	}
@@ -290,5 +290,5 @@
 
 	/*Clean-up and return*/
-	xfree((void**)&vector_ptr);
+	xDelete<double>(vector_ptr);
 	*pdataref=dataref;
 }
