Changeset 13248


Ignore:
Timestamp:
09/04/12 16:12:34 (13 years ago)
Author:
Mathieu Morlighem
Message:

CHG: getting rid of xmalloc xcalloc and xrealloc. Use xNew xNewInit and xReNew

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  
    2626#include "../../include/include.h"
    2727#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 }
    5428
    5529void xfree(void* *pv){
     
    8357        }
    8458}
    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  
    1010template <class doubletype> class Matrix;
    1111template <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);
     12void xfree(void** pvptr);
    1613void xdelete(Matrix<IssmDouble>** pvptr);
    1714void xdelete(Vector<IssmDouble>** pvptr);
    1815
    1916#include "./xNewDelete.h"
    20 
    2117#endif
  • issm/trunk-jpl/src/c/shared/Alloc/xNewDelete.h

    r12448 r13248  
    6161T* xReNew(T* old, unsigned int old_size, unsigned int size) {
    6262#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
    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;
     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;
    8787#else
    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;
     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;
    9393#endif
    9494}
  • issm/trunk-jpl/src/c/shared/TriMesh/SplitMeshForRifts.cpp

    r12446 r13248  
    7676                                 instances of node in the triangulation *for those elements, to the
    7777                                 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
    7985                                //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++;
    8687
    8788                                //change elements owning this node
  • issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp

    r12447 r13248  
    265265
    266266        /*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);
    269269
    270270        /*First, update the existing segments to the new nodes :*/
     
    662662
    663663        /*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);
    666666        counter1=0;
    667667        counter2=0;
     
    10931093                                if(triple==1){
    10941094                                        /*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);
    10971097                                        x[nods]=(x[(int)node1-1]+x[(int)node2-1]+x[(int)node3-1])/3;
    10981098                                        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));
    11011100                                        /*First, reassign element el: */
    11021101                                        *(index+3*el+0)=node1;
Note: See TracChangeset for help on using the changeset viewer.