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