Main Page   Class Hierarchy   Compound List   File List   Compound Members   File Members  

alloc.h

Go to the documentation of this file.
00001 //***************************************************************************
00002 #ifndef ALLOC_H
00003 #define ALLOC_H
00004 //***************************************************************************
00005 #include <iostream.h>
00006 #include <stdlib.h>
00007 //***************************************************************************
00008 void print_malloc(char *filename, int lineno, int mem_size);
00009 void fatal_malloc(char *filename, int lineno, int mem_size);
00010 //***************************************************************************
00011 extern double mem_consumed;
00012 static double my_memory_allocated = 0.0;
00013 static double my_memory_deleted = 0.0;
00014 #define GLOBAL_VAR_FOR_ALLOCATION my_memory_allocated
00015 #define GLOBAL_VAR_FOR_DELETION my_memory_deleted
00016 /*
00017 extern double GLOBAL_VAR_FOR_ALLOCATION;
00018 extern double GLOBAL_VAR_FOR_DELETION;
00019 */
00020 //***************************************************************************
00021 #define IS_ZERO(x) (fabs(x) < 1.0e-8)
00022 void myprintf(char *fmt, ...);
00023 //***************************************************************************
00024 inline double TotalMemoryAllocated()  // in bytes
00025 {
00026 return GLOBAL_VAR_FOR_ALLOCATION;
00027 }
00028 //***************************************************************************
00029 inline double TotalMemoryDeleted()   // in bytes
00030 {
00031 return GLOBAL_VAR_FOR_DELETION;
00032 }
00033 //***************************************************************************
00034 inline double TotalMemoryConsumed()  // in bytes
00035 {
00036 return (GLOBAL_VAR_FOR_ALLOCATION-GLOBAL_VAR_FOR_DELETION);
00037 }
00038 //***************************************************************************
00039 template <class Etype>
00040 inline void MY_MALLOC(Etype **ptr, int n = 1)
00041 {
00042 Etype *a = NULL;
00043 a = new Etype[n];
00044 GLOBAL_VAR_FOR_ALLOCATION += 1.0*n*sizeof(Etype);
00045 
00046 if (a == NULL)
00047         {
00048          cout << "MY_MALLOC: Insufficient memory, could not allocate " << sizeof(Etype)*n << " bytes!" << endl;
00049          exit(-1);
00050         }
00051 
00052 *ptr = a;
00053 }
00054 //***************************************************************************
00055 template <class Etype>
00056 inline void ALLOC1D(Etype **ptr, int m = 1)
00057 {
00058 Etype *a = NULL;
00059 
00060 #ifdef ANIDEBUG
00061 cout << "Allocating " << m << "x" << sizeof(Etype) << " = " << m*sizeof(Etype) << " bytes..." << flush;
00062 #endif
00063 
00064 a = new Etype[m];
00065 GLOBAL_VAR_FOR_ALLOCATION += 1.0*m*sizeof(Etype);
00066 if (a == NULL)
00067         {
00068          cout << "ALLOC1D: Insufficient memory, could not allocate " << sizeof(Etype)*m << " bytes!" << endl;
00069          exit(-1);
00070         }
00071 
00072 *ptr = a;
00073 
00074 #ifdef ANIDEBUG
00075 cout << "done!" << endl << flush;
00076 #endif
00077 }
00078 //***************************************************************************
00079 template <class Etype>
00080 inline void ALLOC2D(Etype ***ptr, int m, int n)
00081 {
00082 Etype **a = NULL;
00083 a = new Etype*[m];
00084 GLOBAL_VAR_FOR_ALLOCATION += 1.0*m*sizeof(Etype*);
00085 
00086 if (a == NULL)
00087         {
00088          cout << "ALLOC2D: Insufficient memory, could not allocate " << sizeof(Etype*)*m << " bytes!" << endl;
00089          exit(-1);
00090         }
00091 
00092 for (int i = 0; i < m; i++)
00093         ALLOC1D(&a[i], n);
00094 
00095 *ptr = a;
00096 }
00097 //***************************************************************************
00098 template <class Etype>
00099 inline void ALLOC3D(Etype ****ptr, int m, int n, int o)
00100 {
00101 Etype ***a = NULL;
00102 a = new Etype**[m];
00103 GLOBAL_VAR_FOR_ALLOCATION += 1.0*m*sizeof(Etype**);
00104 
00105 if (a == NULL)
00106         {
00107          cout << "ALLOC3D: Insufficient memory, could not allocate " << sizeof(Etype**)*m << " bytes!" << endl;
00108          exit(-1);
00109         }
00110 
00111 for (int i = 0; i < m; i++)
00112         ALLOC2D(&a[i], n, o);
00113 
00114 *ptr = a;
00115 }
00116 //***************************************************************************
00117 template <class Etype>
00118 inline void ALLOC4D(Etype *****ptr, int m, int n, int o, int p)
00119 {
00120 Etype ****a = NULL;
00121 a = new Etype***[m];
00122 GLOBAL_VAR_FOR_ALLOCATION += 1.0*m*sizeof(Etype***);
00123 
00124 if (a == NULL)
00125         {
00126          cout << "ALLOC4D: Insufficient memory, could not allocate " << sizeof(Etype***)*m << " bytes!" << endl;
00127          exit(-1);
00128         }
00129 
00130 for (int i = 0; i < m; i++)
00131         ALLOC3D(&a[i], n, o, p);
00132 
00133 *ptr = a;
00134 }
00135 //***************************************************************************
00136 template <class Etype>
00137 inline void FREE1D(Etype **ptr, int m = 1)
00138 {
00139 Etype *a = *ptr;
00140 
00141 if (a == NULL) return;
00142 
00143 delete [] a;
00144 GLOBAL_VAR_FOR_DELETION += 1.0*m*sizeof(Etype);
00145 *ptr = NULL;
00146 }
00147 //***************************************************************************
00148 template <class Etype>
00149 inline void FREE2D(Etype ***ptr, int m, int n)
00150 {
00151 Etype **a = *ptr;
00152 
00153 if (a == NULL) return;
00154 
00155 for (int i = 0; i < m; i++)
00156         FREE1D(&a[i], n);
00157 
00158 delete [] a;
00159 GLOBAL_VAR_FOR_DELETION += 1.0*m*sizeof(Etype*);
00160 *ptr = NULL;
00161 }
00162 //***************************************************************************
00163 template <class Etype>
00164 inline void FREE3D(Etype ****ptr, int m, int n, int o)
00165 {
00166 Etype ***a = *ptr;
00167 
00168 if (a == NULL) return;
00169 
00170 for (int i = 0; i < m; i++)
00171         FREE2D(&a[i], n, o);
00172 
00173 delete [] a;
00174 GLOBAL_VAR_FOR_DELETION += 1.0*m*sizeof(Etype**);
00175 *ptr = NULL;
00176 }
00177 //***************************************************************************
00178 template <class Etype>
00179 inline void FREE4D(Etype *****ptr, int m, int n, int o, int p)
00180 {
00181 Etype ****a = *ptr;
00182 
00183 if (a == NULL) return;
00184 
00185 for (int i = 0; i < m; i++)
00186         FREE3D(&a[i], n, o, p);
00187 
00188 delete [] a;
00189 GLOBAL_VAR_FOR_DELETION += 1.0*m*sizeof(Etype***);
00190 *ptr = NULL;
00191 }
00192 //***************************************************************************
00193 #endif // ALLOC_H
00194 //***************************************************************************

Generated on Sun Jun 16 17:36:48 2002 for POSSE: Portable Object-oriented Scientific Steering Environment by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001