/[escript]/trunk/esysUtils/src/maths.h
ViewVC logotype

Diff of /trunk/esysUtils/src/maths.h

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1565 by gross, Thu May 22 10:19:47 2008 UTC revision 1628 by phornby, Fri Jul 11 13:12:46 2008 UTC
# Line 28  Line 28 
28  /**************************************************************/  /**************************************************************/
29    
30  /* some system values */  /* some system values */
31    /* FIXME: This is not satisfactory.                                */
32    /* _ECC, __INTEL_COMPILER, and other                               */
33    /* intel compiler pre-defines need to be handled                   */
34    /* (__ICL, __ICC come to mind)                                     */
35  #if ( defined __INTEL_COMPILER )  #if ( defined __INTEL_COMPILER )
36  #include <mathimf.h>  #include <mathimf.h>
37  #else  #else
# Line 88  typedef int err_t; Line 92  typedef int err_t;
92                                  _a1_=s; \                                  _a1_=s; \
93                                 }                                 }
94  /**************************************************************/  /**************************************************************/
   
95  /*    memory allocation:                                      */  /*    memory allocation:                                      */
96    /*    Wise to not use PASO_MALLOC/FREE/REALLOC and            */
97    /*    PASO_THREAD... directly. These are only for tailoring   */
98    /*    the main macros that follow                             */
99    /**************************************************************/
100    
101  /*  #if defined(_WIN32) /* Use python for memory management on windows. */
     This is useful for seeing what memory is being allocated if you get an "Out of memory" error  
 #define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))  
102    
103      This is useful for seeing where memory is being freed if you get an "glibc detected...free" error    #include <python.h>
 #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }  
 */  
104    
105  #define TMPMEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))    #define PASO_MALLOC PyMem_Malloc
106  #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }    #define PASO_FREE PyMem_Free
107  #ifdef __ECC    #define PASO_REALLOC PyMem_Realloc
   /*#define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)); if( !(_LENGTH_) ) printf( "Trying malloc(0) at %s %d\n", __FILE__, __LINE__ ); //else printf( "malloc(%d) at %s %d\n", _LENGTH_, __FILE__, __LINE__ );*/  
   #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
   #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }  
   #ifdef _OPENMP  
       #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) kmp_malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
       #define THREAD_MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { kmp_free(_PTR_); (_PTR_) = NULL; }  
   #else  
      #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  
      #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)  
   #endif  
   #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_) if( (_POINTER_)!=NULL ){ _POINTER_ = (_TYPE_*)realloc((void*)(_POINTER_),((size_t)(_LENGTH_))*sizeof(_TYPE_) );  }else{ _POINTER_ = (_TYPE_*)malloc( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); }  
108    
109  #else  #else
110    #define MEMALLOC(_LENGTH_,_TYPE_) (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_))  
111    #define MEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { free(_PTR_); (_PTR_) = NULL; }    #include <malloc.h>
112    #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) TMPMEMALLOC(_LENGTH_,_TYPE_)  
113    #define THREAD_MEMFREE(_PTR_) TMPMEMFREE(_PTR_)    #define PASO_MALLOC malloc
114    #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_) if( (_POINTER_)!=NULL ){ _POINTER_ = (_TYPE_*)realloc((void*)(_POINTER_),((size_t)(_LENGTH_))*sizeof(_TYPE_) );  }else{ _POINTER_ = (_TYPE_*)malloc( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); }    #define PASO_FREE free
115      #define PASO_REALLOC realloc
116    
117  #endif  #endif
118    
119  #define TMPMEMALLOC_AND_PASOCHECK(_LENGTH_,_TYPE_) ( MEMALLOC(_LENGTH_,_TYPE_);  
120    /* FIXME: This is not satisfactory.                                */
121    /* _ECC, __INTEL_COMPILER, and other                               */
122    /* intel compiler pre-defines need to be handled                   */
123    /* (__ICL, __ICC come to mind)                                     */
124    /* Also, _WIN32 may take this branch one day...                    */
125    /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
126    
127    #if defined(__ECC) && defined(_OPENMP) /* ECC version of intel compiler with openmp. */
128      #include <omp.h>
129      #define PASO_THREAD_MALLOC kmp_malloc
130      #define PASO_THREAD_FREE kmp_free
131    #else
132      #define PASO_THREAD_MALLOC PASO_MALLOC
133      #define PASO_THREAD_FREE PASO_FREE
134    #endif
135    
136    /******************The main macros ************************************/
137    
138    #define MEMALLOC(_LENGTH_,_TYPE_)                                     \
139      (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
140    
141    /* do {} while(0) -  an old trick for bracketing a macro that */
142    /* makes sure a semi-colon does no harm.                      */
143    
144    #define MEMFREE(_PTR_)                                                  \
145    do                                                                      \
146    {                                                                       \
147      if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; }   \
148    } while(0)
149    
150    #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_)                             \
151    do                                                                        \
152    {                                                                         \
153       if( (_POINTER_)!=NULL )                                                \
154       {                                                                      \
155          _POINTER_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_),               \
156                                       ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
157       }                                                                      \
158       else                                                                   \
159       {                                                                      \
160          _POINTER_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
161       }                                                                      \
162    } while(0)
163    
164    #define TMPMEMALLOC MEMALLOC
165    #define TMPMEMFREE MEMFREE
166    
167    #define THREAD_MEMALLOC(_LENGTH_,_TYPE_)                          \
168       (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
169    
170    #define THREAD_MEMFREE(_PTR_)                                                \
171    do                                                                           \
172    {                                                                            \
173      if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
174    } while(0)
175    
176    
177    /*
178        This was useful for seeing what memory is being allocated if you get an "Out of memory" error
179            if used again, bracket with do {} while(0)
180    #define TMPMEMALLOC(_LENGTH_,_TYPE_) (printf("TMPMEMALLOC at %s %d #bytes=%d*%d=%d\n", __FILE__, __LINE__, _LENGTH_, sizeof(_TYPE_), ((size_t)(_LENGTH_) * sizeof(_TYPE_))) , (_TYPE_*) malloc(((size_t)(_LENGTH_))*sizeof(_TYPE_)))
181    
182        This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
183            if used again, bracket with do {} while(0)
184    #define TMPMEMFREE(_PTR_) if ((void *)(_PTR_) != NULL ) { printf("TMPMEMFREE AAA %s %d\n", __FILE__, __LINE__); free(_PTR_); (_PTR_) = NULL; printf("TMPMEMFREE BBB %s %d\n", __FILE__, __LINE__); }
185    */
186    
187  #endif /* #ifndef INC_PASO_COMMON */  #endif /* #ifndef INC_PASO_COMMON */

Legend:
Removed from v.1565  
changed lines
  Added in v.1628

  ViewVC Help
Powered by ViewVC 1.1.26