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