/[escript]/branches/windows_from_1456_trunk_1580_merged_in/paso/src/Common.h
ViewVC logotype

Contents of /branches/windows_from_1456_trunk_1580_merged_in/paso/src/Common.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1601 - (show annotations)
Sat Jun 14 08:00:35 2008 UTC (10 years, 8 months ago) by phornby
File MIME type: text/plain
File size: 7126 byte(s)
Completely re-factor this code so that muli-statement
macros are totally safe, and there is not a preponderance
of inscrutable ifdefs guarding comprehension of the code.
1
2 /* $Id$ */
3
4 /*******************************************************
5 *
6 * Copyright 2003-2007 by ACceSS MNRF
7 * Copyright 2007 by University of Queensland
8 *
9 * http://esscc.uq.edu.au
10 * Primary Business: Queensland, Australia
11 * Licensed under the Open Software License version 3.0
12 * http://www.opensource.org/licenses/osl-3.0.php
13 *
14 *******************************************************/
15
16 #ifndef INC_PASO_COMMON
17 #define INC_PASO_COMMON
18
19 /**************************************************************/
20
21 /* Finley finite element solver: common include file */
22
23 /**************************************************************/
24
25 /* Copyrights by ACcESS Australia, 2003 */
26 /* Version: $Id$ */
27
28 /**************************************************************/
29
30 /* some system values */
31 /* FIXME: This is not satisfactory. _ECC, __INTEL_COMPILER, and other */
32 /* intel compiler pre-defines need to be handled (__ICL, __ICC come to mind) */
33 #if ( defined __INTEL_COMPILER )
34 #include <mathimf.h>
35 #else
36 #include <math.h>
37 #endif
38
39
40 #include <float.h>
41 #include <stdio.h>
42 #include <limits.h>
43 #include <stdlib.h>
44 #include <string.h>
45
46 #define LenString_MAX FILENAME_MAX*2
47 #define LenErrorMsg_MAX LenString_MAX
48
49 /* on some arcitectures it could be a good idea to use long rather than int */
50 /* this has not really been tested */
51
52 typedef int dim_t;
53 typedef int index_t;
54 typedef int bool_t;
55 typedef int type_t;
56 typedef int err_t;
57
58 #define INDEX_T_MAX INT_MAX
59 #define EPSILON DBL_EPSILON
60 #define LARGE_POSITIVE_FLOAT DBL_MAX
61
62 /**************************************************************/
63
64 /* some useful functions: */
65
66 #define FALSE 0
67 #define TRUE 1
68 #define UNKNOWN -1
69 #define DBLE(_x_) (double)(_x_)
70 #define INDEX1(_X1_) (_X1_)
71 #define INDEX2(_X1_,_X2_,_N1_) ((_X1_)+(_N1_)*(_X2_))
72 #define INDEX3(_X1_,_X2_,_X3_,_N1_,_N2_) ((_X1_)+(_N1_)*INDEX2(_X2_,_X3_,_N2_))
73 #define INDEX4(_X1_,_X2_,_X3_,_X4_,_N1_,_N2_,_N3_) ((_X1_)+(_N1_)*INDEX3(_X2_,_X3_,_X4_,_N2_,_N3_))
74 #define INDEX5(_X1_,_X2_,_X3_,_X4_,_X5_,_N1_,_N2_,_N3_,_N4_) ((_X1_)+(_N1_)*INDEX4(_X2_,_X3_,_X4_,_X5_,_N2_,_N3_,_N4_))
75 #define INDEX6(_X1_,_X2_,_X3_,_X4_,_X5_,_X6_,_N1_,_N2_,_N3_,_N4_,_N5_) ((_X1_)+(_N1_)*INDEX5(_X2_,_X3_,_X4_,_X5_,_X6_,_N2_,_N3_,_N4_,_N5_))
76
77 #define MAX(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg1_) : (_arg2_))
78 #define MAX3(_arg1_,_arg2_,_arg3_) MAX(_arg1_,MAX(_arg2_,_arg3_))
79 #define MIN(_arg1_,_arg2_) ((_arg1_)>(_arg2_) ? (_arg2_) : (_arg1_))
80 #define MIN3(_arg1_,_arg2_,_arg3_) MIN(_arg1_,MIN(_arg2_,_arg3_))
81 #define ABS(_arg_) MAX((_arg_),-(_arg_))
82 #define SIGN(_arg_) ((_arg_)>0 ? 1 : ((_arg_)<0 ? -1 : 0 ))
83 #define SWAP(_a0_,_a1_,_type_) { \
84 _type_ s; \
85 s=(_a0_); \
86 _a0_= (_a1_); \
87 _a1_=s; \
88 }
89 /**************************************************************/
90 /* memory allocation: */
91 /* Wise to not use PASO_MALLOC/FREE/REALLOC and */
92 /* PASO_THREAD... directly. These are only for tailoring */
93 /* the main macros that follow */
94 /**************************************************************/
95
96 #if defined(_WIN32) // Use python for memory management on windows.
97
98 #include <python.h>
99
100 #define PASO_MALLOC PyMem_Malloc
101 #define PASO_FREE PyMem_Free
102 #define PASO_REALLOC PyMem_Realloc
103
104 #else
105
106 #include <malloc.h>
107
108 #define PASO_MALLOC malloc
109 #define PASO_FREE free
110 #define PASO_REALLOC realloc
111
112 #endif
113
114
115 /* FIXME: This is not satisfactory. _ECC, __INTEL_COMPILER, and other */
116 /* intel compiler pre-defines need to be handled (__ICL, __ICC come to mind) */
117 /* Also, _WIN32 may take this branch one day... */
118 /* SO KEEP ALL THREAD_MEMALLOC/FREEs CONFINED TO THE PASO LIBRARY. */
119
120 #if defined(__ECC) && defined(_OPENMP) // ECC version of intel compiler with openmp.
121 #include <omp.h>
122 #define PASO_THREAD_MALLOC kmp_malloc
123 #define PASO_THREAD_FREE kmp_free
124 #else
125 #define PASO_THREAD_MALLOC PASO_MALLOC
126 #define PASO_THREAD_FREE PASO_FREE
127 #endif
128
129 /******************The main macros ************************************/
130
131 #define MEMALLOC(_LENGTH_,_TYPE_) \
132 (_TYPE_*) PASO_MALLOC(((size_t)(_LENGTH_))*sizeof(_TYPE_))
133
134 /* do {} while(0) - an old trick for bracketing a macro that */
135 /* makes sure a semi-colon does no harm. */
136
137 #define MEMFREE(_PTR_) \
138 do \
139 { \
140 if ((void *)(_PTR_) != NULL ) { PASO_FREE(_PTR_); (_PTR_) = NULL; } \
141 } while(0)
142
143 #define MEMREALLOC(_POINTER_,_LENGTH_,_TYPE_) \
144 do \
145 { \
146 if( (_POINTER_)!=NULL ) \
147 { \
148 _POINTER_ = (_TYPE_*)PASO_REALLOC((void*)(_POINTER_), \
149 ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
150 } \
151 else \
152 { \
153 _POINTER_ = (_TYPE_*)PASO_MALLOC( ((size_t)(_LENGTH_))*sizeof(_TYPE_) ); \
154 } \
155 } while(0)
156
157 #define TMPMEMALLOC MEMALLOC
158 #define TMPMEMFREE MEMFREE
159
160 #define THREAD_MEMALLOC(_LENGTH_,_TYPE_) \
161 (_TYPE_*) PASO_THREAD_MALLOC/**/(((size_t)(_LENGTH_))*sizeof(_TYPE_))
162
163 #define THREAD_MEMFREE(_PTR_) \
164 do \
165 { \
166 if ((void *)(_PTR_) != NULL ) { PASO_THREAD_FREE(_PTR_); (_PTR_) = NULL; } \
167 } while(0)
168
169
170 /*
171 This was useful for seeing what memory is being allocated if you get an "Out of memory" error
172 if used again, bracket with do {} while(0)
173 #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_)))
174
175 This was useful for seeing where memory is being freed if you get an "glibc detected...free" error
176 if used again, bracket with do {} while(0)
177 #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__); }
178 */
179
180 #endif /* #ifndef INC_PASO_COMMON */

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

  ViewVC Help
Powered by ViewVC 1.1.26