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