1 /*
2    Copyright (c) 2001, 2013, Oracle and/or its affiliates.
3    Copyright (c) 2009, 2019, MariaDB Corporation.
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; version 2 of the License.
8 
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13 
14    You should have received a copy of the GNU General Public License
15    along with this program; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1335  USA */
17 
18 /* This is the include file that should be included 'first' in every C file. */
19 
20 #ifndef MY_GLOBAL_INCLUDED
21 #define MY_GLOBAL_INCLUDED
22 
23 /* Client library users on Windows need this macro defined here. */
24 #if !defined(__WIN__) && defined(_WIN32)
25 #define __WIN__
26 #endif
27 
28 /*
29   InnoDB depends on some MySQL internals which other plugins should not
30   need.  This is because of InnoDB's foreign key support, "safe" binlog
31   truncation, and other similar legacy features.
32 
33   We define accessors for these internals unconditionally, but do not
34   expose them in mysql/plugin.h.  They are declared in ha_innodb.h for
35   InnoDB's use.
36 */
37 #define INNODB_COMPATIBILITY_HOOKS
38 
39 #ifdef __CYGWIN__
40 /* We use a Unix API, so pretend it's not Windows */
41 #undef WIN
42 #undef WIN32
43 #undef _WIN
44 #undef _WIN32
45 #undef _WIN64
46 #undef __WIN__
47 #undef __WIN32__
48 #define HAVE_ERRNO_AS_DEFINE
49 #define _POSIX_MONOTONIC_CLOCK
50 #define _POSIX_THREAD_CPUTIME
51 #endif /* __CYGWIN__ */
52 
53 #if defined(__OpenBSD__) && (OpenBSD >= 200411)
54 #define HAVE_ERRNO_AS_DEFINE
55 #endif
56 
57 #if defined(i386) && !defined(__i386__)
58 #define __i386__
59 #endif
60 
61 /* Macros to make switching between C and C++ mode easier */
62 #ifdef __cplusplus
63 #define C_MODE_START    extern "C" {
64 #define C_MODE_END	}
65 #else
66 #define C_MODE_START
67 #define C_MODE_END
68 #endif
69 
70 #ifdef __cplusplus
71 #define CPP_UNNAMED_NS_START  namespace {
72 #define CPP_UNNAMED_NS_END    }
73 #endif
74 
75 #include <my_config.h>
76 
77 #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
78 #define HAVE_PSI_INTERFACE
79 #endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
80 
81 /* Make it easier to add conditional code in _expressions_ */
82 #ifdef __WIN__
83 #define IF_WIN(A,B) A
84 #else
85 #define IF_WIN(A,B) B
86 #endif
87 
88 #ifdef EMBEDDED_LIBRARY
89 #define IF_EMBEDDED(A,B) A
90 #else
91 #define IF_EMBEDDED(A,B) B
92 #endif
93 
94 #ifdef WITH_PARTITION_STORAGE_ENGINE
95 #define IF_PARTITIONING(A,B) A
96 #else
97 #define IF_PARTITIONING(A,B) B
98 #endif
99 
100 #if defined (_WIN32)
101 /*
102  off_t is 32 bit long. We do not use C runtime functions
103  with off_t but native Win32 file IO APIs, that work with
104  64 bit offsets.
105 */
106 #undef SIZEOF_OFF_T
107 #define SIZEOF_OFF_T 8
108 
109 /*
110  Prevent inclusion of  Windows GDI headers - they define symbol
111  ERROR that conflicts with mysql headers.
112 */
113 #ifndef NOGDI
114 #define NOGDI
115 #endif
116 
117 /* Include common headers.*/
118 #include <winsock2.h>
119 #include <ws2tcpip.h> /* SOCKET */
120 #include <io.h>       /* access(), chmod() */
121 #include <process.h>  /* getpid() */
122 
123 #define sleep(a) Sleep((a)*1000)
124 
125 /* Define missing access() modes. */
126 #define F_OK 0
127 #define W_OK 2
128 #define R_OK 4                        /* Test for read permission.  */
129 
130 /* Define missing file locking constants. */
131 #define F_RDLCK 1
132 #define F_WRLCK 2
133 #define F_UNLCK 3
134 #define F_TO_EOF 0x3FFFFFFF
135 
136 /* Shared memory and named pipe connections are supported. */
137 #define HAVE_SMEM 1
138 #define HAVE_NAMED_PIPE 1
139 #define shared_memory_buffer_length 16000
140 #define default_shared_memory_base_name "MYSQL"
141 #endif /* _WIN32*/
142 
143 
144 /* Workaround for _LARGE_FILES and _LARGE_FILE_API incompatibility on AIX */
145 #if defined(_AIX) && defined(_LARGE_FILE_API)
146 #undef _LARGE_FILE_API
147 #undef __GNUG__
148 #endif
149 
150 /*
151   The macros below are used to allow build of Universal/fat binaries of
152   MySQL and MySQL applications under darwin.
153 */
154 #if defined(__APPLE__) && defined(__MACH__)
155 #  undef SIZEOF_CHARP
156 #  undef SIZEOF_INT
157 #  undef SIZEOF_LONG
158 #  undef SIZEOF_LONG_LONG
159 #  undef SIZEOF_OFF_T
160 #  undef WORDS_BIGENDIAN
161 #  define SIZEOF_INT 4
162 #  define SIZEOF_LONG_LONG 8
163 #  define SIZEOF_OFF_T 8
164 #  if defined(__i386__) || defined(__ppc__)
165 #    define SIZEOF_CHARP 4
166 #    define SIZEOF_LONG 4
167 #  elif defined(__x86_64__) || defined(__ppc64__) || defined(__aarch64__)
168 #    define SIZEOF_CHARP 8
169 #    define SIZEOF_LONG 8
170 #  else
171 #    error Building FAT binary for an unknown architecture.
172 #  endif
173 #  if defined(__ppc__) || defined(__ppc64__)
174 #    define WORDS_BIGENDIAN
175 #  endif
176 #endif /* defined(__APPLE__) && defined(__MACH__) */
177 
178 
179 /*
180   The macros below are borrowed from include/linux/compiler.h in the
181   Linux kernel. Use them to indicate the likelihood of the truthfulness
182   of a condition. This serves two purposes - newer versions of gcc will be
183   able to optimize for branch predication, which could yield siginficant
184   performance gains in frequently executed sections of the code, and the
185   other reason to use them is for documentation
186 */
187 
188 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
189 #define __builtin_expect(x, expected_value) (x)
190 #endif
191 
192 /* Fix problem with S_ISLNK() on Linux */
193 #if defined(TARGET_OS_LINUX) || defined(__GLIBC__)
194 #undef  _GNU_SOURCE
195 #define _GNU_SOURCE 1
196 #endif
197 
198 /*
199   Temporary solution to solve bug#7156. Include "sys/types.h" before
200   the thread headers, else the function madvise() will not be defined
201 */
202 #if defined(HAVE_SYS_TYPES_H) && ( defined(sun) || defined(__sun) )
203 #include <sys/types.h>
204 #endif
205 
206 #define __EXTENSIONS__ 1	/* We want some extension */
207 #ifndef __STDC_EXT__
208 #define __STDC_EXT__ 1          /* To get large file support on hpux */
209 #endif
210 
211 /*
212   Solaris 9 include file <sys/feature_tests.h> refers to X/Open document
213 
214     System Interfaces and Headers, Issue 5
215 
216   saying we should define _XOPEN_SOURCE=500 to get POSIX.1c prototypes,
217   but apparently other systems (namely FreeBSD) don't agree.
218 
219   On a newer Solaris 10, the above file recognizes also _XOPEN_SOURCE=600.
220   Furthermore, it tests that if a program requires older standard
221   (_XOPEN_SOURCE<600 or _POSIX_C_SOURCE<200112L) it cannot be
222   run on a new compiler (that defines _STDC_C99) and issues an #error.
223   It's also an #error if a program requires new standard (_XOPEN_SOURCE=600
224   or _POSIX_C_SOURCE=200112L) and a compiler does not define _STDC_C99.
225 
226   To add more to this mess, Sun Studio C compiler defines _STDC_C99 while
227   C++ compiler does not!
228 
229   So, in a desperate attempt to get correct prototypes for both
230   C and C++ code, we define either _XOPEN_SOURCE=600 or _XOPEN_SOURCE=500
231   depending on the compiler's announced C standard support.
232 
233   Cleaner solutions are welcome.
234 */
235 #ifdef __sun
236 #if __STDC_VERSION__ - 0 >= 199901L
237 #define _XOPEN_SOURCE 600
238 #else
239 #define _XOPEN_SOURCE 500
240 #endif
241 #endif
242 
243 
244 #ifdef _AIX
245 /*
246   AIX includes inttypes.h from sys/types.h
247   Explicitly request format macros before the first inclusion of inttypes.h
248 */
249 #if !defined(__STDC_FORMAT_MACROS)
250 #define __STDC_FORMAT_MACROS
251 #endif  // !defined(__STDC_FORMAT_MACROS)
252 #endif
253 
254 
255 #if !defined(__WIN__)
256 #ifndef _POSIX_PTHREAD_SEMANTICS
257 #define _POSIX_PTHREAD_SEMANTICS /* We want posix threads */
258 #endif
259 
260 #if !defined(SCO)
261 #define _REENTRANT	1	/* Some thread libraries require this */
262 #endif
263 #if !defined(_THREAD_SAFE) && !defined(_AIX)
264 #define _THREAD_SAFE            /* Required for OSF1 */
265 #endif
266 #if defined(HPUX10) || defined(HPUX11)
267 C_MODE_START			/* HPUX needs this, signal.h bug */
268 #include <pthread.h>
269 C_MODE_END
270 #else
271 #include <pthread.h>		/* AIX must have this included first */
272 #endif
273 #if !defined(SCO) && !defined(_REENTRANT)
274 #define _REENTRANT	1	/* Threads requires reentrant code */
275 #endif
276 #endif /* !defined(__WIN__) */
277 
278 /* Go around some bugs in different OS and compilers */
279 #ifdef _AIX			/* By soren@t.dk */
280 #define _H_STRINGS
281 #define _SYS_STREAM_H
282 /* #define _AIX32_CURSES */	/* XXX: this breaks AIX 4.3.3 (others?). */
283 #define ulonglong2double(A) my_ulonglong2double(A)
284 #define my_off_t2double(A)  my_ulonglong2double(A)
285 C_MODE_START
my_ulonglong2double(unsigned long long A)286 inline double my_ulonglong2double(unsigned long long A) { return (double)A; }
287 C_MODE_END
288 #endif /* _AIX */
289 
290 #ifdef UNDEF_HAVE_INITGROUPS			/* For AIX 4.3 */
291 #undef HAVE_INITGROUPS
292 #endif
293 
294 /* gcc/egcs issues */
295 
296 #if defined(__GNUC) && defined(__EXCEPTIONS)
297 #error "Please add -fno-exceptions to CXXFLAGS and reconfigure/recompile"
298 #endif
299 
300 #if defined(_lint) && !defined(lint)
301 #define lint
302 #endif
303 #if SIZEOF_LONG_LONG > 4 && !defined(_LONG_LONG)
304 #define _LONG_LONG 1		/* For AIX string library */
305 #endif
306 
307 /* Workaround for _LARGE_FILES and _LARGE_FILE_API incompatibility on AIX */
308 #if defined(_AIX) && defined(_LARGE_FILE_API)
309 #undef _LARGE_FILE_API
310 #undef __GNUG__
311 #endif
312 
313 
314 #ifndef stdin
315 #include <stdio.h>
316 #endif
317 #include <stdarg.h>
318 #ifdef HAVE_STDLIB_H
319 #include <stdlib.h>
320 #endif
321 #ifdef HAVE_STDDEF_H
322 #include <stddef.h>
323 #endif
324 
325 #include <math.h>
326 #ifdef HAVE_LIMITS_H
327 #include <limits.h>
328 #endif
329 #ifdef HAVE_FLOAT_H
330 #include <float.h>
331 #endif
332 #ifdef HAVE_FENV_H
333 #include <fenv.h> /* For fesetround() */
334 #endif
335 
336 #ifdef HAVE_SYS_TYPES_H
337 #include <sys/types.h>
338 #endif
339 
340 /* Workaround for _LARGE_FILES and _LARGE_FILE_API incompatibility on AIX */
341 #if defined(_AIX) && defined(_LARGE_FILE_API)
342 #undef _LARGE_FILE_API
343 #undef __GNUG__
344 #endif
345 
346 
347 #ifdef HAVE_FCNTL_H
348 #include <fcntl.h>
349 #endif
350 #ifdef HAVE_SYS_STAT_H
351 #include <sys/stat.h>
352 #endif
353 #if TIME_WITH_SYS_TIME
354 # include <sys/time.h>
355 # include <time.h>
356 #else
357 # if HAVE_SYS_TIME_H
358 #  include <sys/time.h>
359 # else
360 #  include <time.h>
361 # endif
362 #endif /* TIME_WITH_SYS_TIME */
363 #ifdef HAVE_UNISTD_H
364 #include <unistd.h>
365 #endif
366 #if defined(__cplusplus) && defined(NO_CPLUSPLUS_ALLOCA)
367 #undef HAVE_ALLOCA
368 #undef HAVE_ALLOCA_H
369 #endif
370 #ifdef HAVE_ALLOCA_H
371 #include <alloca.h>
372 #endif
373 
374 #include <errno.h>				/* Recommended by debian */
375 /* We need the following to go around a problem with openssl on solaris */
376 #if defined(HAVE_CRYPT_H)
377 #include <crypt.h>
378 #endif
379 
380 /* Add checking if we are using likely/unlikely wrong */
381 #ifdef CHECK_UNLIKELY
382 C_MODE_START
383 extern void init_my_likely(), end_my_likely(FILE *);
384 extern int my_likely_ok(const char *file_name, uint line);
385 extern int my_likely_fail(const char *file_name, uint line);
386 C_MODE_END
387 
388 #define likely(A) ((A) ? (my_likely_ok(__FILE__, __LINE__),1) : (my_likely_fail(__FILE__, __LINE__), 0))
389 #define unlikely(A) ((A) ? (my_likely_fail(__FILE__, __LINE__),1) : (my_likely_ok(__FILE__, __LINE__), 0))
390 /*
391   These macros should be used when the check fails often when running benchmarks but
392   we know for sure that the check is correct in a production environment
393 */
394 #define checked_likely(A) (A)
395 #define checked_unlikely(A) (A)
396 #else
397 /**
398   The semantics of builtin_expect() are that
399   1) its two arguments are long
400   2) it's likely that they are ==
401   Those of our likely(x) are that x can be bool/int/longlong/pointer.
402 */
403 
404 #define likely(x)	__builtin_expect(((x) != 0),1)
405 #define unlikely(x)	__builtin_expect(((x) != 0),0)
406 #define checked_likely(x) likely(x)
407 #define checked_unlikely(x) unlikely(x)
408 #endif /* CHECK_UNLIKELY */
409 
410 /*
411   A lot of our programs uses asserts, so better to always include it
412   This also fixes a problem when people uses DBUG_ASSERT without including
413   assert.h
414 */
415 #include <assert.h>
416 
417 /* an assert that works at compile-time. only for constant expression */
418 #ifdef _some_old_compiler_that_does_not_understand_the_construct_below_
419 #define compile_time_assert(X)  do { } while(0)
420 #else
421 #define compile_time_assert(X)                                  \
422   do                                                            \
423   {                                                             \
424     typedef char compile_time_assert[(X) ? 1 : -1] __attribute__((unused)); \
425   } while(0)
426 #endif
427 
428 /* Go around some bugs in different OS and compilers */
429 #if defined (HPUX11) && defined(_LARGEFILE_SOURCE)
430 #ifndef _LARGEFILE64_SOURCE
431 #define _LARGEFILE64_SOURCE
432 #endif
433 #endif
434 
435 #if defined(_HPUX_SOURCE) && defined(HAVE_SYS_STREAM_H)
436 #include <sys/stream.h>		/* HPUX 10.20 defines ulong here. UGLY !!! */
437 #define HAVE_ULONG
438 #endif
439 #if defined(HPUX10) && defined(_LARGEFILE64_SOURCE)
440 /* Fix bug in setrlimit */
441 #undef setrlimit
442 #define setrlimit cma_setrlimit64
443 #endif
444 /* Declare madvise where it is not declared for C++, like Solaris */
445 #if HAVE_MADVISE && !HAVE_DECL_MADVISE && defined(__cplusplus)
446 extern "C" int madvise(void *addr, size_t len, int behav);
447 #endif
448 #ifdef HAVE_SYS_MMAN_H
449 #include <sys/mman.h>
450 #endif
451 /** FreeBSD equivalent */
452 #if defined(MADV_CORE) && !defined(MADV_DODUMP)
453 #define MADV_DODUMP MADV_CORE
454 #define MADV_DONTDUMP MADV_NOCORE
455 #define DODUMP_STR "MADV_CORE"
456 #define DONTDUMP_STR "MADV_NOCORE"
457 #else
458 #define DODUMP_STR "MADV_DODUMP"
459 #define DONTDUMP_STR "MADV_DONTDUMP"
460 #endif
461 
462 
463 #define QUOTE_ARG(x)		#x	/* Quote argument (before cpp) */
464 #define STRINGIFY_ARG(x) QUOTE_ARG(x)	/* Quote argument, after cpp */
465 
466 /* Paranoid settings. Define I_AM_PARANOID if you are paranoid */
467 #ifdef I_AM_PARANOID
468 #define DONT_ALLOW_USER_CHANGE 1
469 #define DONT_USE_MYSQL_PWD 1
470 #endif
471 
472 /* Does the system remember a signal handler after a signal ? */
473 #if !defined(HAVE_BSD_SIGNALS) && !defined(HAVE_SIGACTION)
474 #define SIGNAL_HANDLER_RESET_ON_DELIVERY
475 #endif
476 
477 /* don't assume that STDERR_FILENO is 2, mysqld can freopen */
478 #undef STDERR_FILENO
479 
480 #ifndef SO_EXT
481 #ifdef _WIN32
482 #define SO_EXT ".dll"
483 #else
484 #define SO_EXT ".so"
485 #endif
486 #endif
487 
488 /*
489    Suppress uninitialized variable warning without generating code.
490 */
491 #if defined(__GNUC__)
492 /* GCC specific self-initialization which inhibits the warning. */
493 #define UNINIT_VAR(x) x= x
494 #elif defined(_lint) || defined(FORCE_INIT_OF_VARS)
495 #define UNINIT_VAR(x) x= 0
496 #else
497 #define UNINIT_VAR(x) x
498 #endif
499 
500 /* This is only to be used when resetting variables in a class constructor */
501 #if defined(_lint) || defined(FORCE_INIT_OF_VARS)
502 #define LINT_INIT(x) x= 0
503 #else
504 #define LINT_INIT(x)
505 #endif
506 
507 #if !defined(HAVE_UINT)
508 #undef HAVE_UINT
509 #define HAVE_UINT
510 typedef unsigned int uint;
511 typedef unsigned short ushort;
512 #endif
513 
514 #define swap_variables(t, a, b) do { t dummy; dummy= a; a= b; b= dummy; } while(0)
515 #define MY_TEST(a) ((a) ? 1 : 0)
516 #define set_if_bigger(a,b)  do { if ((a) < (b)) (a)=(b); } while(0)
517 #define set_if_smaller(a,b) do { if ((a) > (b)) (a)=(b); } while(0)
518 #define set_bits(type, bit_count) (sizeof(type)*8 <= (bit_count) ? ~(type) 0 : ((((type) 1) << (bit_count)) - (type) 1))
519 #define test_all_bits(a,b) (((a) & (b)) == (b))
520 #define array_elements(A) ((uint) (sizeof(A)/sizeof(A[0])))
521 
522 /* Define some general constants */
523 #ifndef TRUE
524 #define TRUE		(1)	/* Logical true */
525 #define FALSE		(0)	/* Logical false */
526 #endif
527 
528 #include <my_compiler.h>
529 
530 /*
531   Wen using the embedded library, users might run into link problems,
532   duplicate declaration of __cxa_pure_virtual, solved by declaring it a
533   weak symbol.
534 */
535 #if defined(USE_MYSYS_NEW) && ! defined(DONT_DECLARE_CXA_PURE_VIRTUAL)
536 C_MODE_START
537 int __cxa_pure_virtual () __attribute__ ((weak));
538 C_MODE_END
539 #endif
540 
541 /* The DBUG_ON flag always takes precedence over default DBUG_OFF */
542 #if defined(DBUG_ON) && defined(DBUG_OFF)
543 #undef DBUG_OFF
544 #endif
545 
546 /* We might be forced to turn debug off, if not turned off already */
547 #if (defined(FORCE_DBUG_OFF) || defined(_lint)) && !defined(DBUG_OFF)
548 #  define DBUG_OFF
549 #  ifdef DBUG_ON
550 #    undef DBUG_ON
551 #  endif
552 #endif
553 
554 #ifdef DBUG_OFF
555 #undef EXTRA_DEBUG
556 #endif
557 
558 /* Some types that is different between systems */
559 
560 typedef int	File;		/* File descriptor */
561 #ifdef _WIN32
562 typedef SOCKET my_socket;
563 #else
564 typedef int	my_socket;	/* File descriptor for sockets */
565 #define INVALID_SOCKET -1
566 #endif
567 /* Type for functions that handles signals */
568 #define sig_handler RETSIGTYPE
569 #if defined(__GNUC__) && !defined(_lint)
570 typedef char	pchar;		/* Mixed prototypes can take char */
571 typedef char	puchar;		/* Mixed prototypes can take char */
572 typedef char	pbool;		/* Mixed prototypes can take char */
573 typedef short	pshort;		/* Mixed prototypes can take short int */
574 typedef float	pfloat;		/* Mixed prototypes can take float */
575 #else
576 typedef int	pchar;		/* Mixed prototypes can't take char */
577 typedef uint	puchar;		/* Mixed prototypes can't take char */
578 typedef int	pbool;		/* Mixed prototypes can't take char */
579 typedef int	pshort;		/* Mixed prototypes can't take short int */
580 typedef double	pfloat;		/* Mixed prototypes can't take float */
581 #endif
582 C_MODE_START
583 typedef int	(*qsort_cmp)(const void *,const void *);
584 typedef int	(*qsort_cmp2)(void*, const void *,const void *);
585 C_MODE_END
586 #define qsort_t RETQSORTTYPE	/* Broken GCC can't handle typedef !!!! */
587 #ifdef HAVE_SYS_SOCKET_H
588 #include <sys/socket.h>
589 #endif
590 typedef SOCKET_SIZE_TYPE size_socket;
591 
592 #ifndef SOCKOPT_OPTLEN_TYPE
593 #define SOCKOPT_OPTLEN_TYPE size_socket
594 #endif
595 
596 /* file create flags */
597 
598 #ifndef O_SHARE			/* Probably not windows */
599 #define O_SHARE		0	/* Flag to my_open for shared files */
600 #ifndef O_BINARY
601 #define O_BINARY	0	/* Flag to my_open for binary files */
602 #endif
603 #ifndef FILE_BINARY
604 #define FILE_BINARY	O_BINARY /* Flag to my_fopen for binary streams */
605 #endif
606 #ifdef HAVE_FCNTL
607 #define HAVE_FCNTL_LOCK
608 #define F_TO_EOF	0L	/* Param to lockf() to lock rest of file */
609 #endif
610 #endif /* O_SHARE */
611 
612 #ifndef O_SEQUENTIAL
613 #define O_SEQUENTIAL	0
614 #endif
615 #ifndef O_SHORT_LIVED
616 #define O_SHORT_LIVED	0
617 #endif
618 #ifndef O_NOFOLLOW
619 #define O_NOFOLLOW      0
620 #endif
621 #ifndef O_CLOEXEC
622 #define O_CLOEXEC       0
623 #endif
624 #ifdef __GLIBC__
625 #define STR_O_CLOEXEC "e"
626 #else
627 #define STR_O_CLOEXEC ""
628 #endif
629 #ifndef SOCK_CLOEXEC
630 #define SOCK_CLOEXEC    0
631 #else
632 #define HAVE_SOCK_CLOEXEC
633 #endif
634 
635 /* additional file share flags for win32 */
636 #ifdef __WIN__
637 #define _SH_DENYRWD     0x110    /* deny read/write mode & delete */
638 #define _SH_DENYWRD     0x120    /* deny write mode & delete      */
639 #define _SH_DENYRDD     0x130    /* deny read mode & delete       */
640 #define _SH_DENYDEL     0x140    /* deny delete only              */
641 #endif /* __WIN__ */
642 
643 
644 /* General constants */
645 #define FN_LEN		256	/* Max file name len */
646 #define FN_HEADLEN	253	/* Max length of filepart of file name */
647 #define FN_EXTLEN	20	/* Max length of extension (part of FN_LEN) */
648 #define FN_REFLEN	512	/* Max length of full path-name */
649 #define FN_EXTCHAR	'.'
650 #define FN_HOMELIB	'~'	/* ~/ is used as abbrev for home dir */
651 #define FN_CURLIB	'.'	/* ./ is used as abbrev for current dir */
652 #define FN_PARENTDIR	".."	/* Parent directory; Must be a string */
653 
654 #ifdef _WIN32
655 #define FN_LIBCHAR	'\\'
656 #define FN_LIBCHAR2	'/'
657 #define FN_DIRSEP       "/\\"               /* Valid directory separators */
658 #define FN_EXEEXT   ".exe"
659 #define FN_SOEXT    ".dll"
660 #define FN_ROOTDIR	"\\"
661 #define FN_DEVCHAR	':'
662 #define FN_NETWORK_DRIVES	/* Uses \\ to indicate network drives */
663 #define FN_NO_CASE_SENCE	/* Files are not case-sensitive */
664 #else
665 #define FN_LIBCHAR	'/'
666 #define FN_LIBCHAR2	'/'
667 #define FN_DIRSEP       "/"     /* Valid directory separators */
668 #define FN_EXEEXT   ""
669 #define FN_SOEXT    ".so"
670 #define FN_ROOTDIR	"/"
671 #endif
672 
673 /*
674   MY_FILE_MIN is  Windows speciality and is used to quickly detect
675   the mismatch of CRT and mysys file IO usage on Windows at runtime.
676   CRT file descriptors can be in the range 0-2047, whereas descriptors returned
677   by my_open() will start with 2048. If a file descriptor with value less then
678   MY_FILE_MIN is passed to mysys IO function, chances are it stemms from
679   open()/fileno() and not my_open()/my_fileno.
680 
681   For Posix,  mysys functions are light wrappers around libc, and MY_FILE_MIN
682   is logically 0.
683 */
684 
685 #ifdef _WIN32
686 #define MY_FILE_MIN  2048
687 #else
688 #define MY_FILE_MIN  0
689 #endif
690 
691 /*
692   MY_NFILE is the default size of my_file_info array.
693 
694   It is larger on Windows, because it all file handles are stored in my_file_info
695   Default size is 16384 and this should be enough for most cases.If it is not
696   enough, --max-open-files with larger value can be used.
697 
698   For Posix , my_file_info array is only used to store filenames for
699   error reporting and its size is not a limitation for number of open files.
700 */
701 #ifdef _WIN32
702 #define MY_NFILE (16384 + MY_FILE_MIN)
703 #else
704 #define MY_NFILE 64
705 #endif
706 
707 #ifndef OS_FILE_LIMIT
708 #define OS_FILE_LIMIT	UINT_MAX
709 #endif
710 
711 /*
712   Io buffer size; Must be a power of 2 and a multiple of 512. May be
713   smaller what the disk page size. This influences the speed of the
714   isam btree library. eg to big to slow.
715 */
716 #define IO_SIZE			4096U
717 /*
718   How much overhead does malloc have. The code often allocates
719   something like 1024-MALLOC_OVERHEAD bytes
720 */
721 #define MALLOC_OVERHEAD 8
722 
723 	/* get memory in huncs */
724 #define ONCE_ALLOC_INIT		(uint) 4096
725 	/* Typical record cache */
726 #define RECORD_CACHE_SIZE	(uint) (128*1024)
727 	/* Typical key cache */
728 #define KEY_CACHE_SIZE		(uint) (128L*1024L*1024L)
729 	/* Default size of a key cache block  */
730 #define KEY_CACHE_BLOCK_SIZE	(uint) 1024
731 
732 	/* Some things that this system doesn't have */
733 
734 #ifdef _WIN32
735 #define NO_DIR_LIBRARY		/* Not standard dir-library */
736 #endif
737 
738 /* Some defines of functions for portability */
739 
740 #undef remove		/* Crashes MySQL on SCO 5.0.0 */
741 #ifndef __WIN__
742 #define closesocket(A)	close(A)
743 #endif
744 
745 #if defined(_MSC_VER)
746 #if !defined(_WIN64)
my_ulonglong2double(unsigned long long value)747 inline double my_ulonglong2double(unsigned long long value)
748 {
749   long long nr=(long long) value;
750   if (nr >= 0)
751     return (double) nr;
752   return (18446744073709551616.0 + (double) nr);
753 }
754 #define ulonglong2double my_ulonglong2double
755 #define my_off_t2double  my_ulonglong2double
756 #endif /* _WIN64 */
my_double2ulonglong(double d)757 inline unsigned long long my_double2ulonglong(double d)
758 {
759   double t= d - (double) 0x8000000000000000ULL;
760 
761   if (t >= 0)
762     return  ((unsigned long long) t) + 0x8000000000000000ULL;
763   return (unsigned long long) d;
764 }
765 #define double2ulonglong my_double2ulonglong
766 #endif
767 
768 #ifndef ulonglong2double
769 #define ulonglong2double(A) ((double) (ulonglong) (A))
770 #define my_off_t2double(A)  ((double) (my_off_t) (A))
771 #endif
772 #ifndef double2ulonglong
773 #define double2ulonglong(A) ((ulonglong) (double) (A))
774 #endif
775 
776 #ifndef offsetof
777 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
778 #endif
779 #define ulong_to_double(X) ((double) (ulong) (X))
780 
781 #ifndef STACK_DIRECTION
782 #error "please add -DSTACK_DIRECTION=1 or -1 to your CPPFLAGS"
783 #endif
784 
785 #if !defined(HAVE_STRTOK_R)
786 #define strtok_r(A,B,C) strtok((A),(B))
787 #endif
788 
789 #if SIZEOF_LONG_LONG >= 8
790 #define HAVE_LONG_LONG 1
791 #else
792 #error WHAT? sizeof(long long) < 8 ???
793 #endif
794 
795 /*
796   Some pre-ANSI-C99 systems like AIX 5.1 and Linux/GCC 2.95 define
797   ULONGLONG_MAX, LONGLONG_MIN, LONGLONG_MAX; we use them if they're defined.
798 */
799 
800 #if defined(HAVE_LONG_LONG) && !defined(LONGLONG_MIN)
801 #define LONGLONG_MIN	((long long) 0x8000000000000000LL)
802 #define LONGLONG_MAX	((long long) 0x7FFFFFFFFFFFFFFFLL)
803 #endif
804 
805 #if defined(HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)
806 /* First check for ANSI C99 definition: */
807 #ifdef ULLONG_MAX
808 #define ULONGLONG_MAX  ULLONG_MAX
809 #else
810 #define ULONGLONG_MAX ((unsigned long long)(~0ULL))
811 #endif
812 #endif /* defined (HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)*/
813 
814 #define INT_MIN64       (~0x7FFFFFFFFFFFFFFFLL)
815 #define INT_MAX64       0x7FFFFFFFFFFFFFFFLL
816 #define INT_MIN32       (~0x7FFFFFFFL)
817 #define INT_MAX32       0x7FFFFFFFL
818 #define UINT_MAX32      0xFFFFFFFFL
819 #define INT_MIN24       (~0x007FFFFF)
820 #define INT_MAX24       0x007FFFFF
821 #define UINT_MAX24      0x00FFFFFF
822 #define INT_MIN16       (~0x7FFF)
823 #define INT_MAX16       0x7FFF
824 #define UINT_MAX16      0xFFFF
825 #define INT_MIN8        (~0x7F)
826 #define INT_MAX8        0x7F
827 #define UINT_MAX8       0xFF
828 
829 /* From limits.h instead */
830 #ifndef DBL_MIN
831 #define DBL_MIN		4.94065645841246544e-324
832 #define FLT_MIN		((float)1.40129846432481707e-45)
833 #endif
834 #ifndef DBL_MAX
835 #define DBL_MAX		1.79769313486231470e+308
836 #define FLT_MAX		((float)3.40282346638528860e+38)
837 #endif
838 #ifndef SIZE_T_MAX
839 #define SIZE_T_MAX      (~((size_t) 0))
840 #endif
841 
842 /* Define missing math constants. */
843 #ifndef M_PI
844 #define M_PI 3.14159265358979323846
845 #endif
846 #ifndef M_E
847 #define M_E 2.7182818284590452354
848 #endif
849 #ifndef M_LN2
850 #define M_LN2 0.69314718055994530942
851 #endif
852 
853 /*
854   Max size that must be added to a so that we know Size to make
855   addressable obj.
856 */
857 #if SIZEOF_CHARP == 4
858 typedef long		my_ptrdiff_t;
859 #else
860 typedef long long	my_ptrdiff_t;
861 #endif
862 
863 #define MY_ALIGN(A,L)	   (((A) + (L) - 1) & ~((L) - 1))
864 #define MY_ALIGN_DOWN(A,L) ((A) & ~((L) - 1))
865 #define ALIGN_SIZE(A)	MY_ALIGN((A),sizeof(double))
866 #define ALIGN_MAX_UNIT  (sizeof(double))
867 /* Size to make addressable obj. */
868 #define ALIGN_PTR(A, t) ((t*) MY_ALIGN((A), sizeof(double)))
869 #define ADD_TO_PTR(ptr,size,type) (type) ((uchar*) (ptr)+size)
870 #define PTR_BYTE_DIFF(A,B) (my_ptrdiff_t) ((uchar*) (A) - (uchar*) (B))
871 #define PREV_BITS(type,A)	((type) (((type) 1 << (A)) -1))
872 
873 /*
874   Custom version of standard offsetof() macro which can be used to get
875   offsets of members in class for non-POD types (according to the current
876   version of C++ standard offsetof() macro can't be used in such cases and
877   attempt to do so causes warnings to be emitted, OTOH in many cases it is
878   still OK to assume that all instances of the class has the same offsets
879   for the same members).
880 
881   This is temporary solution which should be removed once File_parser class
882   and related routines are refactored.
883 */
884 
885 #define my_offsetof(TYPE, MEMBER) PTR_BYTE_DIFF(&((TYPE *)0x10)->MEMBER, 0x10)
886 
887 #define NullS		(char *) 0
888 
889 #ifdef STDCALL
890 #undef STDCALL
891 #endif
892 
893 #ifdef _WIN32
894 #define STDCALL __stdcall
895 #else
896 #define STDCALL
897 #endif
898 
899 /* Typdefs for easyier portability */
900 
901 #ifndef HAVE_UCHAR
902 typedef unsigned char	uchar;	/* Short for unsigned char */
903 #endif
904 
905 #ifndef HAVE_INT8
906 typedef signed char int8;       /* Signed integer >= 8  bits */
907 #endif
908 #ifndef HAVE_UINT8
909 typedef unsigned char uint8;    /* Unsigned integer >= 8  bits */
910 #endif
911 #ifndef HAVE_INT16
912 typedef short int16;
913 #endif
914 #ifndef HAVE_UINT16
915 typedef unsigned short uint16;
916 #endif
917 #if SIZEOF_INT == 4
918 #ifndef HAVE_INT32
919 typedef int int32;
920 #endif
921 #ifndef HAVE_UINT32
922 typedef unsigned int uint32;
923 #endif
924 #elif SIZEOF_LONG == 4
925 #ifndef HAVE_INT32
926 typedef long int32;
927 #endif
928 #ifndef HAVE_UINT32
929 typedef unsigned long uint32;
930 #endif
931 #else
932 #error Neither int or long is of 4 bytes width
933 #endif
934 
935 #if !defined(HAVE_ULONG) && !defined(__USE_MISC)
936 typedef unsigned long	ulong;		  /* Short for unsigned long */
937 #endif
938 #ifndef longlong_defined
939 /*
940   Using [unsigned] long long is preferable as [u]longlong because we use
941   [unsigned] long long unconditionally in many places,
942   for example in constants with [U]LL suffix.
943 */
944 #if defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
945 typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
946 typedef long long int	longlong;
947 #else
948 typedef unsigned long	ulonglong;	  /* ulong or unsigned long long */
949 typedef long		longlong;
950 #endif
951 #endif
952 #ifndef HAVE_INT64
953 typedef longlong int64;
954 #endif
955 #ifndef HAVE_UINT64
956 typedef ulonglong uint64;
957 #endif
958 
959 #if defined(NO_CLIENT_LONG_LONG)
960 typedef unsigned long my_ulonglong;
961 #elif defined (__WIN__)
962 typedef unsigned __int64 my_ulonglong;
963 #else
964 typedef unsigned long long my_ulonglong;
965 #endif
966 
967 #if SIZEOF_CHARP == SIZEOF_INT
968 typedef unsigned int intptr;
969 #elif SIZEOF_CHARP == SIZEOF_LONG
970 typedef unsigned long intptr;
971 #elif SIZEOF_CHARP == SIZEOF_LONG_LONG
972 typedef unsigned long long intptr;
973 #else
974 #error sizeof(void *) is neither sizeof(int) nor sizeof(long) nor sizeof(long long)
975 #endif
976 
977 #define MY_ERRPTR ((void*)(intptr)1)
978 
979 #if defined(_WIN32)
980 typedef unsigned long long my_off_t;
981 typedef unsigned long long os_off_t;
982 #else
983 typedef off_t os_off_t;
984 #if SIZEOF_OFF_T > 4
985 typedef ulonglong my_off_t;
986 #else
987 typedef unsigned long my_off_t;
988 #endif
989 #endif /*_WIN32*/
990 #define MY_FILEPOS_ERROR	(~(my_off_t) 0)
991 
992 /*
993   TODO Convert these to use Bitmap class.
994  */
995 typedef ulonglong table_map;          /* Used for table bits in join */
996 
997 /* often used type names - opaque declarations */
998 typedef const struct charset_info_st CHARSET_INFO;
999 typedef struct st_mysql_lex_string LEX_STRING;
1000 
1001 #if defined(__WIN__)
1002 #define socket_errno	WSAGetLastError()
1003 #define SOCKET_EINTR	WSAEINTR
1004 #define SOCKET_EAGAIN	WSAEINPROGRESS
1005 #define SOCKET_ETIMEDOUT WSAETIMEDOUT
1006 #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
1007 #define SOCKET_EADDRINUSE WSAEADDRINUSE
1008 #define SOCKET_ECONNRESET WSAECONNRESET
1009 #define SOCKET_ENFILE	ENFILE
1010 #define SOCKET_EMFILE	EMFILE
1011 #else /* Unix */
1012 #define socket_errno	errno
1013 #define closesocket(A)	close(A)
1014 #define SOCKET_EINTR	EINTR
1015 #define SOCKET_EAGAIN	EAGAIN
1016 #define SOCKET_EWOULDBLOCK EWOULDBLOCK
1017 #define SOCKET_EADDRINUSE EADDRINUSE
1018 #define SOCKET_ETIMEDOUT ETIMEDOUT
1019 #define SOCKET_ECONNRESET ECONNRESET
1020 #define SOCKET_ENFILE	ENFILE
1021 #define SOCKET_EMFILE	EMFILE
1022 #endif
1023 
1024 #include <mysql/plugin.h>  /* my_bool */
1025 
1026 typedef ulong		myf;	/* Type of MyFlags in my_funcs */
1027 
1028 #define MYF(v)		(myf) (v)
1029 
1030 /*
1031   Defines to make it possible to prioritize register assignments. No
1032   longer that important with modern compilers.
1033 */
1034 #ifndef USING_X
1035 #define reg1 register
1036 #define reg2 register
1037 #define reg3 register
1038 #define reg4 register
1039 #define reg5 register
1040 #define reg6 register
1041 #define reg7 register
1042 #define reg8 register
1043 #define reg9 register
1044 #define reg10 register
1045 #define reg11 register
1046 #define reg12 register
1047 #define reg13 register
1048 #define reg14 register
1049 #define reg15 register
1050 #define reg16 register
1051 #endif
1052 
1053 /*
1054   MYSQL_PLUGIN_IMPORT macro is used to export mysqld data
1055   (i.e variables) for usage in storage engine loadable plugins.
1056   Outside of Windows, it is dummy.
1057 */
1058 #ifndef MYSQL_PLUGIN_IMPORT
1059 #if (defined(_WIN32) && defined(MYSQL_DYNAMIC_PLUGIN))
1060 #define MYSQL_PLUGIN_IMPORT __declspec(dllimport)
1061 #else
1062 #define MYSQL_PLUGIN_IMPORT
1063 #endif
1064 #endif
1065 
1066 #include <my_dbug.h>
1067 
1068 /* Some helper macros */
1069 #define YESNO(X) ((X) ? "yes" : "no")
1070 
1071 #define MY_HOW_OFTEN_TO_ALARM	2	/* How often we want info on screen */
1072 #define MY_HOW_OFTEN_TO_WRITE	10000	/* How often we want info on screen */
1073 
1074 #include <my_byteorder.h>
1075 
1076 #ifdef HAVE_CHARSET_utf8mb4
1077 #define MYSQL_UNIVERSAL_CLIENT_CHARSET "utf8mb4"
1078 #elif defined(HAVE_CHARSET_utf8)
1079 #define MYSQL_UNIVERSAL_CLIENT_CHARSET "utf8"
1080 #else
1081 #define MYSQL_UNIVERSAL_CLIENT_CHARSET MYSQL_DEFAULT_CHARSET_NAME
1082 #endif
1083 
1084 #if defined(EMBEDDED_LIBRARY) && !defined(HAVE_EMBEDDED_PRIVILEGE_CONTROL)
1085 #define NO_EMBEDDED_ACCESS_CHECKS
1086 #endif
1087 
1088 #ifdef _WIN32
1089 #define dlsym(lib, name) (void*)GetProcAddress((HMODULE)lib, name)
1090 #define dlopen(libname, unused) LoadLibraryEx(libname, NULL, 0)
1091 #define RTLD_DEFAULT GetModuleHandle(NULL)
1092 #define dlclose(lib) FreeLibrary((HMODULE)lib)
dlerror(void)1093 static inline char *dlerror(void)
1094 {
1095   static char win_errormsg[2048];
1096   FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM,
1097     0, GetLastError(), 0, win_errormsg, 2048, NULL);
1098   return win_errormsg;
1099 }
1100 #define HAVE_DLOPEN 1
1101 #define HAVE_DLERROR 1
1102 #endif
1103 
1104 #ifdef HAVE_DLFCN_H
1105 #include <dlfcn.h>
1106 #endif
1107 
1108 #ifdef HAVE_DLOPEN
1109 #ifndef HAVE_DLERROR
1110 #define dlerror() ""
1111 #endif
1112 #ifndef HAVE_DLADDR
1113 #define dladdr(A, B) 0
1114 /* Dummy definition in case we're missing dladdr() */
1115 typedef struct { const char *dli_fname, dli_fbase; } Dl_info;
1116 #endif
1117 #else
1118 #define dlerror() "No support for dynamic loading (static build?)"
1119 #define dlopen(A,B) 0
1120 #define dlsym(A,B) 0
1121 #define dlclose(A) 0
1122 #define dladdr(A, B) 0
1123 /* Dummy definition in case we're missing dladdr() */
1124 typedef struct { const char *dli_fname, dli_fbase; } Dl_info;
1125 #endif
1126 
1127 /*
1128  *  Include standard definitions of operator new and delete.
1129  */
1130 #ifdef __cplusplus
1131 #include <new>
1132 #endif
1133 
1134 /* Length of decimal number represented by INT32. */
1135 #define MY_INT32_NUM_DECIMAL_DIGITS 11
1136 
1137 /* Length of decimal number represented by INT64. */
1138 #define MY_INT64_NUM_DECIMAL_DIGITS 21
1139 
1140 #ifdef __cplusplus
1141 #include <limits> /* should be included before min/max macros */
1142 #endif
1143 
1144 /* Define some useful general macros (should be done after all headers). */
1145 #define MY_MAX(a, b)	((a) > (b) ? (a) : (b))
1146 #define MY_MIN(a, b)	((a) < (b) ? (a) : (b))
1147 
1148 #define CMP_NUM(a,b)    (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
1149 
1150 /*
1151   Only Linux is known to need an explicit sync of the directory to make sure a
1152   file creation/deletion/renaming in(from,to) this directory durable.
1153 */
1154 #ifdef TARGET_OS_LINUX
1155 #define NEED_EXPLICIT_SYNC_DIR 1
1156 #else
1157 /*
1158   On linux default rwlock scheduling policy is good enough for
1159   waiting_threads.c, on other systems use our special implementation
1160   (which is slower).
1161 
1162   QQ perhaps this should be tested in configure ? how ?
1163 */
1164 #define WT_RWLOCKS_USE_MUTEXES 1
1165 #endif
1166 
1167 #if !defined(__cplusplus) && !defined(bool)
1168 #define bool In_C_you_should_use_my_bool_instead()
1169 #endif
1170 
1171 /* Provide __func__ macro definition for platforms that miss it. */
1172 #if !defined (__func__)
1173 #if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L
1174 #  if __GNUC__ >= 2
1175 #    define __func__ __FUNCTION__
1176 #  else
1177 #    define __func__ "<unknown>"
1178 #  endif
1179 #elif defined(_MSC_VER)
1180 #  if _MSC_VER < 1300
1181 #    define __func__ "<unknown>"
1182 #  else
1183 #    define __func__ __FUNCTION__
1184 #  endif
1185 #elif defined(__BORLANDC__)
1186 #  define __func__ __FUNC__
1187 #else
1188 #  define __func__ "<unknown>"
1189 #endif
1190 #endif /* !defined(__func__) */
1191 
1192 /* Defines that are unique to the embedded version of MySQL */
1193 
1194 #ifdef EMBEDDED_LIBRARY
1195 
1196 /* Things we don't need in the embedded version of MySQL */
1197 /* TODO HF add #undef HAVE_VIO if we don't want client in embedded library */
1198 
1199 #undef HAVE_SMEM				/* No shared memory */
1200 
1201 #else
1202 #define HAVE_REPLICATION
1203 #define HAVE_EXTERNAL_CLIENT
1204 #endif /* EMBEDDED_LIBRARY */
1205 
1206 /* Workaround for _LARGE_FILES and _LARGE_FILE_API incompatibility on AIX */
1207 #if defined(_AIX) && defined(_LARGE_FILE_API)
1208 #undef _LARGE_FILE_API
1209 #undef __GNUG__
1210 #endif
1211 
1212 /*
1213   Provide defaults for the CPU cache line size, if it has not been detected by
1214   CMake using getconf
1215 */
1216 #if !defined(CPU_LEVEL1_DCACHE_LINESIZE) || CPU_LEVEL1_DCACHE_LINESIZE == 0
1217   #if defined(CPU_LEVEL1_DCACHE_LINESIZE) && CPU_LEVEL1_DCACHE_LINESIZE == 0
1218     #undef CPU_LEVEL1_DCACHE_LINESIZE
1219   #endif
1220 
1221   #if defined(__s390__)
1222     #define CPU_LEVEL1_DCACHE_LINESIZE 256
1223   #elif defined(__powerpc__) || defined(__aarch64__)
1224     #define CPU_LEVEL1_DCACHE_LINESIZE 128
1225   #else
1226     #define CPU_LEVEL1_DCACHE_LINESIZE 64
1227   #endif
1228 #endif
1229 
1230 #define FLOATING_POINT_DECIMALS 31
1231 
1232 /* Keep client compatible with earlier versions */
1233 #ifdef MYSQL_SERVER
1234 #define NOT_FIXED_DEC           DECIMAL_NOT_SPECIFIED
1235 #else
1236 #define NOT_FIXED_DEC           FLOATING_POINT_DECIMALS
1237 #endif
1238 #endif /* my_global_h */
1239