Changeset 14894


Ignore:
Timestamp:
05/04/13 21:55:35 (12 years ago)
Author:
Eric.Larour
Message:

CHG: got rid of xdelete altogether, relying on templated xNew and xDelete operators

Location:
issm/trunk-jpl/src/c
Files:
2 deleted
9 edited

Legend:

Unmodified
Added
Removed
  • issm/trunk-jpl/src/c/Makefile.am

    r14877 r14894  
    184184                                        ./shared/shared.h\
    185185                                        ./shared/Alloc/alloc.h\
    186                                         ./shared/Alloc/alloc.cpp\
    187                                         ./shared/Alloc/xNewDelete.h\
    188186                                        ./shared/MemOps/xMemCpy.h\
    189187                                        ./shared/Matrix/matrix.h\
  • issm/trunk-jpl/src/c/classes/objects/Contour.h

    r13414 r14894  
    1010#include "../../include/include.h"
    1111#include "../../shared/Exceptions/exceptions.h"
    12 #include "../../shared/Alloc/xNewDelete.h"
     12#include "../../shared/Alloc/alloc.h"
    1313#include "../../shared/MemOps/xMemCpy.h"
    1414#include "../../io/io.h"
  • issm/trunk-jpl/src/c/shared/Alloc/alloc.h

    r14890 r14894  
    11/* \file alloc.h
    2  * \brief: header file for memory allocations.
     2 * \brief: header file for memory allocations as well as templated new/delete checking for non-null pointers
    33 */
    44
     
    66#define _ALLOC_H_
    77
    8 #include "../../include/include.h"
    9 #include "./xNewDelete.h"
     8#include <cassert>
    109
    11 template <class doubletype> class Matrix;
    12 template <class doubletype> class Vector;
    13 void xdelete(Matrix<IssmDouble>** pvptr);
    14 void xdelete(Vector<IssmDouble>** pvptr);
     10/* memory management of types T with non-trivial constructors require C++ style memory management*/
     11#define USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     12/* but for speed one may alternatively use C memory management but can do so safely only for T that are at most
     13 * plain old data structures (POD)*/
     14#ifndef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     15#include <cstdlib>
     16#endif
     17
     18template <class T> T* xNew(unsigned int size) { /*{{{*/
     19#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     20  T* aT_p=new T[size];
     21  assert(aT_p);
     22  return aT_p;
     23#else
     24  T* aT_p=(T*)malloc(size*sizeof(T));
     25  assert(aT_p);
     26  return aT_p;
     27#endif 
     28}/*}}}*/
     29template <class T> T** xNew(unsigned int dim1, unsigned int dim2) { /*{{{*/
     30#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     31  T* buf=new T[dim1*dim2];
     32  assert(buf );
     33  T** aT_pp =new T*[dim1];
     34  assert(aT_pp );
     35  for (unsigned int i=0;i<dim1;++i) {
     36    aT_pp [i]=buf;
     37    buf+=dim2;
     38  }
     39  return aT_pp ;
     40#else
     41  T* buf=(T*)malloc(dim1*dim2*sizeof(T));
     42  assert(buf );
     43  T** aT_pp =(T**)malloc(dim1*sizeof(T*));
     44  assert(aT_pp );
     45  for (unsigned int i=0;i<dim1;++i) {
     46    aT_pp [i]=buf;
     47    buf+=dim2;
     48  }
     49  return aT_pp ;
     50#endif
     51}/*}}}*/
     52template <class T> T* xNewZeroInit(unsigned int size) {/*{{{*/
     53#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     54  T* aT_p=xNew<T>(size);
     55  for (unsigned int i=0; i<size;++i)
     56    aT_p[i]=(T)0;
     57  return aT_p;
     58#else
     59  T* aT_p=(T*)calloc(size,sizeof(T));
     60  assert(aT_p);
     61  return aT_p;
     62#endif
     63}/*}}}*/
     64template <class T> T** xNewZeroInit(unsigned int dim1, unsigned int dim2) {/*{{{*/
     65#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     66  T** aT_pp=xNew<T>(dim1,dim2);
     67  for (unsigned int i=0; i<dim1*dim2;++i)
     68    (*aT_pp)[i]=(T)0;
     69  return aT_pp;
     70#else
     71  T* buf=(T*)calloc(dim1*dim2*sizeof(T));
     72  assert(buf );
     73  T** aT_pp =(T**)malloc(dim1*sizeof(T*));
     74  assert(aT_pp );
     75  for (unsigned int i=0;i<dim1;++i) {
     76    aT_pp [i]=buf;
     77    buf+=dim2;
     78  }
     79  return aT_pp ;
     80#endif
     81}/*}}}*/
     82template <class T> void xDelete(T**& aT_pp) {/*{{{*/
     83  if (aT_pp) {
     84#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     85    delete [](*aT_pp);
     86    delete [](aT_pp);
     87#else
     88    free((void*)*aT_pp)
     89    free((void**)aT_pp);
     90#endif
     91  }
     92  aT_pp=0;
     93}/*}}}*/
     94template <class T> void xDelete(T*& aT_p) {/*{{{*/
     95  if (aT_p)
     96#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     97    delete []aT_p;
     98#else
     99    free((void*)aT_p);
     100#endif
     101  aT_p=0;
     102}/*}}}*/
     103template <class T> T* xReNew(T* old, unsigned int old_size, unsigned int size) {/*{{{*/
     104#ifdef USE_CXX_MEMORY_MANAGMENT_FOR_NON_POD_TYPES
     105        T* aT_p=0;
     106        if (!old) { // no old memory
     107                if (size) 
     108                 aT_p=xNew<T>(size); // according to realloc behavior in manual page
     109        }
     110        else { // have old memory
     111                if (!size)  // but 0 size
     112                 xDelete<T>(old); // according to realloc behavior in manual page
     113                else { // non-zero size
     114                        assert(old_size); // have old memory - need to have old_size set or this call is bad
     115                        // allocate new, delete old; ; even for the case when size is
     116                        // less than old_size we can't just keep the memory unchanged
     117                        // because otherwise classes that have ctors/dtors with side-effects
     118                        // may misbehave, for example classes with static instance/operations counters.
     119                        aT_p=xNew<T>(size);
     120                        unsigned int iMax=(old_size<size)?old_size:size;
     121                        for (unsigned int i=0; i<iMax;++i) {
     122                                // we need to copy the items by explicit assignments
     123                                aT_p[i]=old[i];
     124                        }
     125                        xDelete<T>(old);
     126                }
     127        }
     128        return aT_p;
     129#else
     130        T* aT_p=0;
     131        aT_p=(T*)realloc((void*)old,size*sizeof(T));
     132        if (size)
     133         assert(aT_p); // according to realloc behavior in manual page
     134        return aT_p;
     135#endif
     136}/*}}}*/
    15137
    16138#endif
  • issm/trunk-jpl/src/c/shared/Exceptions/exprintf.cpp

    r13758 r14894  
    88#include <stdarg.h>
    99#include <stdio.h>
    10 #include "../Alloc/xNewDelete.h"
    1110#include "../Alloc/alloc.h"
    1211
  • issm/trunk-jpl/src/c/shared/Threads/LaunchThread.cpp

    r14057 r14894  
    2020
    2121#include "./issm_threads.h"
    22 #include "../Alloc/xNewDelete.h"
     22#include "../Alloc/alloc.h"
    2323#include "../Exceptions/exceptions.h"
    2424#include "../../include/include.h"
  • issm/trunk-jpl/src/c/shared/TriMesh/SplitMeshForRifts.cpp

    r14222 r14894  
    33 */
    44#include "./trimesh.h"
    5 #include "../Alloc/xNewDelete.h"
    65#include "../Alloc/alloc.h"
    76
  • issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp

    r14235 r14894  
    77#include "./trimesh.h"
    88#include "../Exceptions/exceptions.h"
    9 #include "../Alloc/xNewDelete.h"
    109#include "../Alloc/alloc.h"
    1110#include "../../include/include.h"
  • issm/trunk-jpl/src/c/shared/shared.h

    r13831 r14894  
    77
    88#include "./Alloc/alloc.h"
    9 #include "./Alloc/xNewDelete.h"
    109#include "./Bamg/shared.h"
    1110#include "./Elements/elements.h"
  • issm/trunk-jpl/src/c/toolkits/mpi/patches/GetOwnershipBoundariesFromRange.cpp

    r14706 r14894  
    1212#include <stdio.h>
    1313#include "../../../shared/Alloc/alloc.h"
     14#include "../../../classes/IssmComm.h"
    1415
    1516void GetOwnershipBoundariesFromRange(int* plower_row,int* pupper_row,int range,COMM comm){
Note: See TracChangeset for help on using the changeset viewer.