Changeset 13248
- Timestamp:
- 09/04/12 16:12:34 (13 years ago)
- Location:
- issm/trunk-jpl/src/c/shared
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/shared/Alloc/alloc.cpp
r13216 r13248 26 26 #include "../../include/include.h" 27 27 #include "../../classes/objects/objects.h" 28 29 void* xmalloc(int size){30 31 void* memptr=NULL;32 33 if(!size)_error_("attempting to 0 size allocation!");34 35 /* Use the c library to do the allocation: */36 memptr=malloc(size);37 if(!memptr) _error_("memory allocation failed!");38 39 return memptr;40 }41 42 void* xcalloc(int n,int size){43 44 void* memptr=NULL;45 46 if(!size)_error_("attempting to 0 size allocation!");47 48 /* Use the c library to do the allocation: */49 memptr=calloc(n,size);50 if(!memptr) _error_("memory allocation failed!");51 52 return memptr;53 }54 28 55 29 void xfree(void* *pv){ … … 83 57 } 84 58 } 85 86 void* xrealloc( void* pv, int size){87 88 register void* value=NULL;89 90 if(!size)_error_("attempting to realloc to zero");91 value = (void*)realloc(pv,size);92 93 if (value == NULL) {94 _error_("virtual memory exhausted");95 }96 return value;97 } -
issm/trunk-jpl/src/c/shared/Alloc/alloc.h
r13216 r13248 10 10 template <class doubletype> class Matrix; 11 11 template <class doubletype> class Vector; 12 void* xmalloc(int size); 13 void* xcalloc(int n,int size); 14 void xfree(void** pvptr); 15 void* xrealloc ( void* pv, int size); 12 void xfree(void** pvptr); 16 13 void xdelete(Matrix<IssmDouble>** pvptr); 17 14 void xdelete(Vector<IssmDouble>** pvptr); 18 15 19 16 #include "./xNewDelete.h" 20 21 17 #endif -
issm/trunk-jpl/src/c/shared/Alloc/xNewDelete.h
r12448 r13248 61 61 T* xReNew(T* old, unsigned int old_size, unsigned int size) { 62 62 #ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 // we need to copy the items by explicit assignments81 aT_p[i]=old[i];82 83 84 85 86 63 T* aT_p=0; 64 if (!old) { // no old memory 65 if (size) 66 aT_p=xNew<T>(size); // according to realloc behavior in manual page 67 } 68 else { // have old memory 69 if (!size) // but 0 size 70 xDelete<T>(old); // according to realloc behavior in manual page 71 else { // non-zero size 72 assert(old_size); // have old memory - need to have old_size set or this call is bad 73 // allocate new, delete old; ; even for the case when size is 74 // less than old_size we can't just keep the memory unchanged 75 // because otherwise classes that have ctors/dtors with side-effects 76 // may misbehave, for example classes with static instance/operations counters. 77 aT_p=xNew<T>(size); 78 unsigned int iMax=(old_size<size)?old_size:size; 79 for (unsigned int i=0; i<iMax;++i) { 80 // we need to copy the items by explicit assignments 81 aT_p[i]=old[i]; 82 } 83 xDelete<T>(old); 84 } 85 } 86 return aT_p; 87 87 #else 88 89 90 91 92 88 T* aT_p=0; 89 aT_p=(T*)realloc((void*)old,size*sizeof(T)); 90 if (size) 91 assert(aT_p); // according to realloc behavior in manual page 92 return aT_p; 93 93 #endif 94 94 } -
issm/trunk-jpl/src/c/shared/TriMesh/SplitMeshForRifts.cpp
r12446 r13248 76 76 instances of node in the triangulation *for those elements, to the 77 77 new node.*/ 78 78 79 //create new node 80 x=xReNew<double>(x,nods,nods+1); 81 y=xReNew<double>(y,nods,nods+1); 82 x[nods]=x[node-1]; //matlab indexing 83 y[nods]=y[node-1]; //matlab indexing 84 79 85 //augment number of nodes 80 nods=nods+1; 81 //create new node 82 x=(double*)xrealloc(x,nods*sizeof(double)); 83 y=(double*)xrealloc(y,nods*sizeof(double)); 84 x[nods-1]=x[node-1]; //matlab indexing 85 y[nods-1]=y[node-1]; //matlab indexing 86 nods++; 86 87 87 88 //change elements owning this node -
issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp
r12447 r13248 265 265 266 266 /*Reallocate segments: */ 267 segments =(double*)xrealloc(segments,(nsegs+nriftsegs)*3*sizeof(double));268 segmentmarkerlist= (double*)xrealloc(segmentmarkerlist,(nsegs+nriftsegs)*sizeof(double));267 segments =xReNew<double>(segments, nsegs*3,(nsegs+nriftsegs)*3); 268 segmentmarkerlist=xReNew<double>(segmentmarkerlist,nsegs*3,(nsegs+nriftsegs)*3); 269 269 270 270 /*First, update the existing segments to the new nodes :*/ … … 662 662 663 663 /*Reallocate x and y: */ 664 xreal= (double*)xrealloc(x,newnods*sizeof(double));665 yreal= (double*)xrealloc(y,newnods*sizeof(double));664 xreal=xReNew<double>(x,nods,newnods); 665 yreal=xReNew<double>(y,nods,newnods); 666 666 counter1=0; 667 667 counter2=0; … … 1093 1093 if(triple==1){ 1094 1094 /*el is a corner element: we need to split it in 3 triangles: */ 1095 x= (double*)xrealloc(x,(nods+1)*sizeof(double));1096 y= (double*)xrealloc(y,(nods+1)*sizeof(double));1095 x=xReNew<double>(x,nods,nods+1); 1096 y=xReNew<double>(y,nods,nods+1); 1097 1097 x[nods]=(x[(int)node1-1]+x[(int)node2-1]+x[(int)node3-1])/3; 1098 1098 y[nods]=(y[(int)node1-1]+y[(int)node2-1]+y[(int)node3-1])/3; 1099 1100 index=(double*)xrealloc(index,(nel+2)*3*sizeof(double)); 1099 index=xReNew<double>(index,nel*3,(nel+2*3)); 1101 1100 /*First, reassign element el: */ 1102 1101 *(index+3*el+0)=node1;
Note:
See TracChangeset
for help on using the changeset viewer.