Changeset 14894
- Timestamp:
- 05/04/13 21:55:35 (12 years ago)
- Location:
- issm/trunk-jpl/src/c
- Files:
-
- 2 deleted
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
issm/trunk-jpl/src/c/Makefile.am
r14877 r14894 184 184 ./shared/shared.h\ 185 185 ./shared/Alloc/alloc.h\ 186 ./shared/Alloc/alloc.cpp\187 ./shared/Alloc/xNewDelete.h\188 186 ./shared/MemOps/xMemCpy.h\ 189 187 ./shared/Matrix/matrix.h\ -
issm/trunk-jpl/src/c/classes/objects/Contour.h
r13414 r14894 10 10 #include "../../include/include.h" 11 11 #include "../../shared/Exceptions/exceptions.h" 12 #include "../../shared/Alloc/ xNewDelete.h"12 #include "../../shared/Alloc/alloc.h" 13 13 #include "../../shared/MemOps/xMemCpy.h" 14 14 #include "../../io/io.h" -
issm/trunk-jpl/src/c/shared/Alloc/alloc.h
r14890 r14894 1 1 /* \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 3 3 */ 4 4 … … 6 6 #define _ALLOC_H_ 7 7 8 #include "../../include/include.h" 9 #include "./xNewDelete.h" 8 #include <cassert> 10 9 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 18 template <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 }/*}}}*/ 29 template <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 }/*}}}*/ 52 template <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 }/*}}}*/ 64 template <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 }/*}}}*/ 82 template <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 }/*}}}*/ 94 template <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 }/*}}}*/ 103 template <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 }/*}}}*/ 15 137 16 138 #endif -
issm/trunk-jpl/src/c/shared/Exceptions/exprintf.cpp
r13758 r14894 8 8 #include <stdarg.h> 9 9 #include <stdio.h> 10 #include "../Alloc/xNewDelete.h"11 10 #include "../Alloc/alloc.h" 12 11 -
issm/trunk-jpl/src/c/shared/Threads/LaunchThread.cpp
r14057 r14894 20 20 21 21 #include "./issm_threads.h" 22 #include "../Alloc/ xNewDelete.h"22 #include "../Alloc/alloc.h" 23 23 #include "../Exceptions/exceptions.h" 24 24 #include "../../include/include.h" -
issm/trunk-jpl/src/c/shared/TriMesh/SplitMeshForRifts.cpp
r14222 r14894 3 3 */ 4 4 #include "./trimesh.h" 5 #include "../Alloc/xNewDelete.h"6 5 #include "../Alloc/alloc.h" 7 6 -
issm/trunk-jpl/src/c/shared/TriMesh/TriMeshUtils.cpp
r14235 r14894 7 7 #include "./trimesh.h" 8 8 #include "../Exceptions/exceptions.h" 9 #include "../Alloc/xNewDelete.h"10 9 #include "../Alloc/alloc.h" 11 10 #include "../../include/include.h" -
issm/trunk-jpl/src/c/shared/shared.h
r13831 r14894 7 7 8 8 #include "./Alloc/alloc.h" 9 #include "./Alloc/xNewDelete.h"10 9 #include "./Bamg/shared.h" 11 10 #include "./Elements/elements.h" -
issm/trunk-jpl/src/c/toolkits/mpi/patches/GetOwnershipBoundariesFromRange.cpp
r14706 r14894 12 12 #include <stdio.h> 13 13 #include "../../../shared/Alloc/alloc.h" 14 #include "../../../classes/IssmComm.h" 14 15 15 16 void GetOwnershipBoundariesFromRange(int* plower_row,int* pupper_row,int range,COMM comm){
Note:
See TracChangeset
for help on using the changeset viewer.