1 /*
2    Copyright (c) 2004, 2016, Oracle and/or its affiliates. All rights reserved.
3 
4    This program is free software; you can redistribute it and/or modify
5    it under the terms of the GNU General Public License, version 2.0,
6    as published by the Free Software Foundation.
7 
8    This program is also distributed with certain software (including
9    but not limited to OpenSSL) that is licensed under separate terms,
10    as designated in a particular file or component or in included license
11    documentation.  The authors of MySQL hereby grant you an additional
12    permission to link the program and your derivative works with the
13    separately licensed software that they have included with MySQL.
14 
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License, version 2.0, for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
23 */
24 
25 #ifndef NDB_GLOBAL_H
26 #define NDB_GLOBAL_H
27 
28 #ifdef _WIN32
29 /* Workaround for Bug#32082: VOID refdefinition results in compile errors */
30 #ifndef DONT_DEFINE_VOID
31 #define DONT_DEFINE_VOID
32 #endif
33 #endif
34 
35 #include <my_global.h>
36 
37 #ifdef HAVE_NDB_CONFIG_H
38 #include "ndb_config.h"
39 #endif
40 
41 #include <mysql_com.h>
42 #include <ndb_types.h>
43 
44 #ifndef NDB_PORT
45 /* Default port used by ndb_mgmd */
46 #define NDB_PORT 1186
47 #endif
48 
49 #if defined(_WIN32) || defined(_WIN64) || defined(__WIN32__) || defined(WIN32)
50 #define NDB_WIN32 1
51 #define NDB_WIN 1
52 #define PATH_MAX 256
53 #define DIR_SEPARATOR "\\"
54 
55 /* Disable a few compiler warnings on Windows */
56 /* 4355: 'this': used in base member initializer list */
57 #pragma warning(disable: 4355)
58 
59 #else
60 #undef NDB_WIN32
61 #undef NDB_WIN
62 #define DIR_SEPARATOR "/"
63 #endif
64 
65 #if ! (NDB_SIZEOF_CHAR == SIZEOF_CHAR)
66 #error "Invalid define for Uint8"
67 #endif
68 
69 #if ! (NDB_SIZEOF_INT == SIZEOF_INT)
70 #error "Invalid define for Uint32"
71 #endif
72 
73 #if ! (NDB_SIZEOF_LONG_LONG == SIZEOF_LONG_LONG)
74 #error "Invalid define for Uint64"
75 #endif
76 
77 #include <signal.h>
78 
79 #ifdef _AIX
80 #undef _H_STRINGS
81 #endif
82 #include <m_string.h>
83 
84 #ifndef NDB_REMOVE_BZERO
85 /*
86   Make it possible to use bzero in NDB although
87   MySQL headers redefines it to an invalid symbol
88 */
89 #ifdef bzero
90 #undef bzero
91 #endif
92 
93 #ifdef HAVE_STRINGS_H
94 #include <strings.h>
95 #endif
96 
97 #if !defined(bzero) && !defined(HAVE_BZERO)
98 #define bzero(A,B) memset((A),0,(B))
99 #endif
100 #endif
101 
102 #include <m_ctype.h>
103 #include <ctype.h>
104 
105 #ifdef HAVE_STDARG_H
106 #include <stdarg.h>
107 #endif
108 
109 #ifdef TIME_WITH_SYS_TIME
110 #include <time.h>
111 #endif
112 
113 #ifdef HAVE_FCNTL_H
114 #include <fcntl.h>
115 #endif
116 
117 #include <sys/stat.h>
118 
119 #ifdef HAVE_SYS_PARAM_H
120 #include <sys/param.h>
121 #endif
122 
123 #ifdef HAVE_SYS_RESOURCE_H
124 #include <sys/resource.h>
125 #endif
126 
127 #ifdef HAVE_SYS_WAIT_H
128 #include <sys/wait.h>
129 #endif
130 
131 #ifdef HAVE_SYS_MMAN_H
132 #include <sys/mman.h>
133 #endif
134 
135 #ifndef HAVE_STRDUP
136 extern char * strdup(const char *s);
137 #endif
138 
139 static const char table_name_separator =  '/';
140 
141 #if defined(_AIX) || defined(WIN32) || defined(NDB_VC98)
142 #define STATIC_CONST(x) enum { x }
143 #else
144 #define STATIC_CONST(x) static const Uint32 x
145 #endif
146 
147 #ifdef  __cplusplus
148 extern "C" {
149 #endif
150 
151 #include <assert.h>
152 
153 #ifdef  __cplusplus
154 }
155 #endif
156 
157 #include "ndb_init.h"
158 
159 #ifndef PATH_MAX
160 #define PATH_MAX 1024
161 #endif
162 
163 #if defined(_lint) || defined(FORCE_INIT_OF_VARS)
164 #define LINT_SET_PTR = {0,0}
165 #else
166 #define LINT_SET_PTR
167 #endif
168 
169 #ifndef MIN
170 #define MIN(x,y) (((x)<(y))?(x):(y))
171 #endif
172 
173 #ifndef MAX
174 #define MAX(x,y) (((x)>(y))?(x):(y))
175 #endif
176 
177 /*
178   Dont allow use of min() or max() macros
179    - in order to enforce forward compatibilty
180 */
181 
182 #ifdef min
183 #undef min
184 #endif
185 
186 #ifdef max
187 #undef max
188 #endif
189 
190 #define NDB_O_DIRECT_WRITE_ALIGNMENT 512
191 
192 #ifndef STATIC_ASSERT
193 #if defined VM_TRACE
194 /**
195  * Compile-time assert for use from procedure body
196  * Zero length array not allowed in C
197  * Add use of array to avoid compiler warning
198  */
199 #define STATIC_ASSERT(expr) { char static_assert[(expr)? 1 : 0] = {'\0'}; if (static_assert[0]) {}; }
200 #else
201 #define STATIC_ASSERT(expr)
202 #endif
203 #endif
204 
205 #define NDB_ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
206 
207 
208 /*
209   NDB_STATIC_ASSERT(expr)
210    - Check coding assumptions during compile time
211      by laying out code that will generate a compiler error
212      if the expression is false.
213 */
214 
215 #if (_MSC_VER > 1500) || (defined __GXX_EXPERIMENTAL_CXX0X__)
216 
217 /*
218   Prefer to use the 'static_assert' function from C++0x
219   to get best error message
220 */
221 #define NDB_STATIC_ASSERT(expr) static_assert(expr, #expr)
222 
223 #else
224 
225 /*
226   Fallback to use home grown solution
227   (i.e use mysys version)
228 */
229 
230 #define NDB_STATIC_ASSERT(expr) compile_time_assert(expr)
231 
232 #endif
233 
234 
235 #if (_MSC_VER > 1500)
236 #define HAVE___HAS_TRIVIAL_CONSTRUCTOR
237 #define HAVE___IS_POD
238 #endif
239 
240 #ifdef HAVE___HAS_TRIVIAL_CONSTRUCTOR
241 #define ASSERT_TYPE_HAS_CONSTRUCTOR(x)     \
242   NDB_STATIC_ASSERT(!__has_trivial_constructor(x))
243 #else
244 #define ASSERT_TYPE_HAS_CONSTRUCTOR(x)
245 #endif
246 
247 /**
248  * visual studio is stricter than gcc for __is_pod, settle for __has_trivial_constructor
249  *  until we really really made all signal data classes POD
250  *
251  * UPDATE: also gcc fails to compile our code with gcc4.4.3
252  */
253 #ifdef HAVE___HAS_TRIVIAL_CONSTRUCTOR
254 #define NDB_ASSERT_POD(x) \
255   NDB_STATIC_ASSERT(__has_trivial_constructor(x))
256 #else
257 #define NDB_ASSERT_POD(x)
258 #endif
259 
260 /**
261  *  MY_ATTRIBUTE((noreturn)) was introduce in gcc 2.5
262  */
263 #if (GCC_VERSION >= 2005)
264 #define ATTRIBUTE_NORETURN MY_ATTRIBUTE((noreturn))
265 #else
266 #define ATTRIBUTE_NORETURN
267 #endif
268 
269 /**
270  *  MY_ATTRIBUTE((noinline)) was introduce in gcc 3.1
271  */
272 #if (GCC_VERSION >= 3001)
273 #define ATTRIBUTE_NOINLINE MY_ATTRIBUTE((noinline))
274 #else
275 #define ATTRIBUTE_NOINLINE
276 #endif
277 
278 /*
279  * require is like a normal assert, only it's always on (eg. in release)
280  */
281 C_MODE_START
282 /** see below */
283 typedef int(*RequirePrinter)(const char *fmt, ...);
284 void require_failed(int exitcode, RequirePrinter p,
285                     const char* expr, const char* file, int line);
286 int ndbout_printer(const char * fmt, ...);
287 C_MODE_END
288 /*
289  *  this allows for an exit() call if exitcode is not zero
290  *  and takes a Printer to print the error
291  */
292 #define require_exit_or_core_with_printer(v, exitcode, printer) \
293   do { if (likely(!(!(v)))) break;                                    \
294        require_failed((exitcode), (printer), #v, __FILE__, __LINE__); \
295   } while (0)
296 
297 /*
298  *  this allows for an exit() call if exitcode is not zero
299 */
300 #define require_exit_or_core(v, exitcode) \
301        require_exit_or_core_with_printer((v), (exitcode), 0)
302 
303 /*
304  * this require is like a normal assert.  (only it's always on)
305 */
306 #define require(v) require_exit_or_core_with_printer((v), 0, 0)
307 
308 #endif
309