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   Cast a member of a structure to the structure that contains it.
377 
378   @param  ptr     Pointer to the member.
379   @param  type    Type of the structure that contains the member.
380   @param  member  Name of the member within the structure.
381 */
382 #define my_container_of(ptr, type, member)              \
383   ({                                                    \
384     const typeof(((type *)0)->member) *__mptr= (ptr);   \
385     (type *)((char *)__mptr - offsetof(type, member));  \
386   })
387 
388 /*
389   A lot of our programs uses asserts, so better to always include it
390   This also fixes a problem when people uses DBUG_ASSERT without including
391   assert.h
392 */
393 #include <assert.h>
394 
395 /* an assert that works at compile-time. only for constant expression */
396 #ifdef _some_old_compiler_that_does_not_understand_the_construct_below_
397 #define compile_time_assert(X)  do { } while(0)
398 #else
399 #define compile_time_assert(X)                                  \
400   do                                                            \
401   {                                                             \
402     typedef char compile_time_assert[(X) ? 1 : -1] MY_ATTRIBUTE((unused)); \
403   } while(0)
404 #endif
405 
406 /* Go around some bugs in different OS and compilers */
407 #if defined (HPUX11) && defined(_LARGEFILE_SOURCE)
408 #ifndef _LARGEFILE64_SOURCE
409 #define _LARGEFILE64_SOURCE
410 #endif
411 #endif
412 
413 #if defined(_HPUX_SOURCE) && defined(HAVE_SYS_STREAM_H)
414 #include <sys/stream.h>		/* HPUX 10.20 defines ulong here. UGLY !!! */
415 #define HAVE_ULONG
416 #endif
417 #if defined(HPUX10) && defined(_LARGEFILE64_SOURCE)
418 /* Fix bug in setrlimit */
419 #undef setrlimit
420 #define setrlimit cma_setrlimit64
421 #endif
422 /* Declare madvise where it is not declared for C++, like Solaris */
423 #if HAVE_MADVISE && !HAVE_DECL_MADVISE && defined(__cplusplus)
424 extern "C" int madvise(void *addr, size_t len, int behav);
425 #endif
426 
427 #define QUOTE_ARG(x)		#x	/* Quote argument (before cpp) */
428 #define STRINGIFY_ARG(x) QUOTE_ARG(x)	/* Quote argument, after cpp */
429 
430 /* Paranoid settings. Define I_AM_PARANOID if you are paranoid */
431 #ifdef I_AM_PARANOID
432 #define DONT_ALLOW_USER_CHANGE 1
433 #define DONT_USE_MYSQL_PWD 1
434 #endif
435 
436 /* Does the system remember a signal handler after a signal ? */
437 #if !defined(HAVE_BSD_SIGNALS) && !defined(HAVE_SIGACTION)
438 #define SIGNAL_HANDLER_RESET_ON_DELIVERY
439 #endif
440 
441 /*
442   Deprecated workaround for false-positive uninitialized variables
443   warnings. Those should be silenced using tool-specific heuristics.
444 
445   Enabled by default for g++ due to the bug referenced below.
446 */
447 #if defined(_lint) || defined(FORCE_INIT_OF_VARS) || \
448     (defined(__GNUC__) && defined(__cplusplus))
449 #define LINT_INIT(var) var= 0
450 #else
451 #define LINT_INIT(var)
452 #endif
453 
454 #ifndef SO_EXT
455 #ifdef _WIN32
456 #define SO_EXT ".dll"
457 #elif defined(__APPLE__)
458 #define SO_EXT ".dylib"
459 #else
460 #define SO_EXT ".so"
461 #endif
462 #endif
463 
464 /*
465    Suppress uninitialized variable warning without generating code.
466 
467    The _cplusplus is a temporary workaround for C++ code pending a fix
468    for a g++ bug (http://gcc.gnu.org/bugzilla/show_bug.cgi?id=34772).
469 */
470 #if defined(_lint) || defined(FORCE_INIT_OF_VARS) || \
471     defined(__cplusplus) || !defined(__GNUC__)
472 #define UNINIT_VAR(x) x= 0
473 #else
474 /* GCC specific self-initialization which inhibits the warning. */
475 #define UNINIT_VAR(x) x= x
476 #endif
477 
478 #if !defined(HAVE_UINT)
479 #undef HAVE_UINT
480 #define HAVE_UINT
481 typedef unsigned int uint;
482 typedef unsigned short ushort;
483 #endif
484 
485 #define swap_variables(t, a, b) { t dummy; dummy= a; a= b; b= dummy; }
486 #define MY_TEST(a)		((a) ? 1 : 0)
487 #define set_if_bigger(a,b)  do { if ((a) < (b)) (a)=(b); } while(0)
488 #define set_if_smaller(a,b) do { if ((a) > (b)) (a)=(b); } while(0)
489 #define test_all_bits(a,b) (((a) & (b)) == (b))
490 #define array_elements(A) ((uint) (sizeof(A)/sizeof(A[0])))
491 
492 /* Define some general constants */
493 #ifndef TRUE
494 #define TRUE		(1)	/* Logical true */
495 #define FALSE		(0)	/* Logical false */
496 #endif
497 
498 #include <my_compiler.h>
499 
500 /* The DBUG_ON flag always takes precedence over default DBUG_OFF */
501 #if defined(DBUG_ON) && defined(DBUG_OFF)
502 #undef DBUG_OFF
503 #endif
504 
505 /* We might be forced to turn debug off, if not turned off already */
506 #if (defined(FORCE_DBUG_OFF) || defined(_lint)) && !defined(DBUG_OFF)
507 #  define DBUG_OFF
508 #  ifdef DBUG_ON
509 #    undef DBUG_ON
510 #  endif
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 fuctions that handles signals */
523 #define sig_handler RETSIGTYPE
524 C_MODE_START
525 typedef void	(*sig_return)();/* Returns type from signal */
526 C_MODE_END
527 #if defined(__GNUC__) && !defined(_lint)
528 typedef char	pchar;		/* Mixed prototypes can take char */
529 typedef char	puchar;		/* Mixed prototypes can take char */
530 typedef char	pbool;		/* Mixed prototypes can take char */
531 typedef short	pshort;		/* Mixed prototypes can take short int */
532 typedef float	pfloat;		/* Mixed prototypes can take float */
533 #else
534 typedef int	pchar;		/* Mixed prototypes can't take char */
535 typedef uint	puchar;		/* Mixed prototypes can't take char */
536 typedef int	pbool;		/* Mixed prototypes can't take char */
537 typedef int	pshort;		/* Mixed prototypes can't take short int */
538 typedef double	pfloat;		/* Mixed prototypes can't take float */
539 #endif
540 C_MODE_START
541 typedef int	(*qsort_cmp)(const void *,const void *);
542 typedef int	(*qsort_cmp2)(const void*, const void *,const void *);
543 C_MODE_END
544 #define qsort_t RETQSORTTYPE	/* Broken GCC cant handle typedef !!!! */
545 #ifdef HAVE_SYS_SOCKET_H
546 #include <sys/socket.h>
547 #endif
548 typedef SOCKET_SIZE_TYPE size_socket;
549 
550 #ifndef SOCKOPT_OPTLEN_TYPE
551 #define SOCKOPT_OPTLEN_TYPE size_socket
552 #endif
553 
554 /* file create flags */
555 
556 #ifndef O_SHARE			/* Probably not windows */
557 #define O_SHARE		0	/* Flag to my_open for shared files */
558 #ifndef O_BINARY
559 #define O_BINARY	0	/* Flag to my_open for binary files */
560 #endif
561 #ifndef FILE_BINARY
562 #define FILE_BINARY	O_BINARY /* Flag to my_fopen for binary streams */
563 #endif
564 #ifdef HAVE_FCNTL
565 #define HAVE_FCNTL_LOCK
566 #define F_TO_EOF	0L	/* Param to lockf() to lock rest of file */
567 #endif
568 #endif /* O_SHARE */
569 
570 #ifndef O_TEMPORARY
571 #define O_TEMPORARY	0
572 #endif
573 #ifndef O_SHORT_LIVED
574 #define O_SHORT_LIVED	0
575 #endif
576 #ifndef O_NOFOLLOW
577 #define O_NOFOLLOW      0
578 #endif
579 
580 /* additional file share flags for win32 */
581 #ifdef __WIN__
582 #define _SH_DENYRWD     0x110    /* deny read/write mode & delete */
583 #define _SH_DENYWRD     0x120    /* deny write mode & delete      */
584 #define _SH_DENYRDD     0x130    /* deny read mode & delete       */
585 #define _SH_DENYDEL     0x140    /* deny delete only              */
586 #endif /* __WIN__ */
587 
588 
589 /* General constants */
590 #define FN_LEN		256	/* Max file name len */
591 #define FN_HEADLEN	253	/* Max length of filepart of file name */
592 #define FN_EXTLEN	20	/* Max length of extension (part of FN_LEN) */
593 #define FN_REFLEN	512	/* Max length of full path-name */
594 #define FN_EXTCHAR	'.'
595 #define FN_HOMELIB	'~'	/* ~/ is used as abbrev for home dir */
596 #define FN_CURLIB	'.'	/* ./ is used as abbrev for current dir */
597 #define FN_PARENTDIR	".."	/* Parent directory; Must be a string */
598 
599 #ifdef _WIN32
600 #define FN_LIBCHAR	'\\'
601 #define FN_LIBCHAR2	'/'
602 #define FN_DIRSEP       "/\\"               /* Valid directory separators */
603 #define FN_EXEEXT   ".exe"
604 #define FN_SOEXT    ".dll"
605 #define FN_ROOTDIR	"\\"
606 #define FN_DEVCHAR	':'
607 #define FN_NETWORK_DRIVES	/* Uses \\ to indicate network drives */
608 #define FN_NO_CASE_SENCE	/* Files are not case-sensitive */
609 #else
610 #define FN_LIBCHAR	'/'
611 #define FN_LIBCHAR2	'/'
612 #define FN_DIRSEP       "/"     /* Valid directory separators */
613 #define FN_EXEEXT   ""
614 #define FN_SOEXT    ".so"
615 #define FN_ROOTDIR	"/"
616 #endif
617 
618 /*
619   MY_FILE_MIN is  Windows speciality and is used to quickly detect
620   the mismatch of CRT and mysys file IO usage on Windows at runtime.
621   CRT file descriptors can be in the range 0-2047, whereas descriptors returned
622   by my_open() will start with 2048. If a file descriptor with value less then
623   MY_FILE_MIN is passed to mysys IO function, chances are it stemms from
624   open()/fileno() and not my_open()/my_fileno.
625 
626   For Posix,  mysys functions are light wrappers around libc, and MY_FILE_MIN
627   is logically 0.
628 */
629 
630 #ifdef _WIN32
631 #define MY_FILE_MIN  2048
632 #else
633 #define MY_FILE_MIN  0
634 #endif
635 
636 /*
637   MY_NFILE is the default size of my_file_info array.
638 
639   It is larger on Windows, because it all file handles are stored in my_file_info
640   Default size is 16384 and this should be enough for most cases.If it is not
641   enough, --max-open-files with larger value can be used.
642 
643   For Posix , my_file_info array is only used to store filenames for
644   error reporting and its size is not a limitation for number of open files.
645 */
646 #ifdef _WIN32
647 #define MY_NFILE (16384 + MY_FILE_MIN)
648 #else
649 #define MY_NFILE 64
650 #endif
651 
652 #ifndef OS_FILE_LIMIT
653 #define OS_FILE_LIMIT	UINT_MAX
654 #endif
655 
656 /*
657   Io buffer size; Must be a power of 2 and a multiple of 512. May be
658   smaller what the disk page size. This influences the speed of the
659   isam btree library. eg to big to slow.
660 */
661 #define IO_SIZE			4096
662 /*
663   How much overhead does malloc have. The code often allocates
664   something like 1024-MALLOC_OVERHEAD bytes
665 */
666 #define MALLOC_OVERHEAD 8
667 
668 	/* get memory in huncs */
669 #define ONCE_ALLOC_INIT		(uint) (4096-MALLOC_OVERHEAD)
670 	/* Typical record cash */
671 #define RECORD_CACHE_SIZE	(uint) (64*1024-MALLOC_OVERHEAD)
672 	/* Typical key cash */
673 #define KEY_CACHE_SIZE		(uint) (8*1024*1024)
674 	/* Default size of a key cache block  */
675 #define KEY_CACHE_BLOCK_SIZE	(uint) 1024
676 
677 
678 	/* Some things that this system doesn't have */
679 
680 #ifdef _WIN32
681 #define NO_DIR_LIBRARY		/* Not standard dir-library */
682 #endif
683 
684 /* Some defines of functions for portability */
685 
686 #undef remove		/* Crashes MySQL on SCO 5.0.0 */
687 #ifndef __WIN__
688 #define closesocket(A)	close(A)
689 #endif
690 
691 #if (_MSC_VER)
692 #if !defined(_WIN64)
my_ulonglong2double(unsigned long long value)693 inline double my_ulonglong2double(unsigned long long value)
694 {
695   long long nr=(long long) value;
696   if (nr >= 0)
697     return (double) nr;
698   return (18446744073709551616.0 + (double) nr);
699 }
700 #define ulonglong2double my_ulonglong2double
701 #define my_off_t2double  my_ulonglong2double
702 #endif /* _WIN64 */
my_double2ulonglong(double d)703 inline unsigned long long my_double2ulonglong(double d)
704 {
705   double t= d - (double) 0x8000000000000000ULL;
706 
707   if (t >= 0)
708     return  ((unsigned long long) t) + 0x8000000000000000ULL;
709   return (unsigned long long) d;
710 }
711 #define double2ulonglong my_double2ulonglong
712 #endif
713 
714 #ifndef ulonglong2double
715 #define ulonglong2double(A) ((double) (ulonglong) (A))
716 #define my_off_t2double(A)  ((double) (my_off_t) (A))
717 #endif
718 #ifndef double2ulonglong
719 #define double2ulonglong(A) ((ulonglong) (double) (A))
720 #endif
721 
722 #ifndef offsetof
723 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
724 #endif
725 #define ulong_to_double(X) ((double) (ulong) (X))
726 
727 #ifndef STACK_DIRECTION
728 #error "please add -DSTACK_DIRECTION=1 or -1 to your CPPFLAGS"
729 #endif
730 
731 #if !defined(HAVE_STRTOK_R)
732 #define strtok_r(A,B,C) strtok((A),(B))
733 #endif
734 
735 /* This is from the old m-machine.h file */
736 
737 #if SIZEOF_LONG_LONG > 4
738 #define HAVE_LONG_LONG 1
739 #endif
740 
741 /*
742   Some pre-ANSI-C99 systems like AIX 5.1 and Linux/GCC 2.95 define
743   ULONGLONG_MAX, LONGLONG_MIN, LONGLONG_MAX; we use them if they're defined.
744 */
745 
746 #if defined(HAVE_LONG_LONG) && !defined(LONGLONG_MIN)
747 #define LONGLONG_MIN	((long long) 0x8000000000000000LL)
748 #define LONGLONG_MAX	((long long) 0x7FFFFFFFFFFFFFFFLL)
749 #endif
750 
751 #if defined(HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)
752 /* First check for ANSI C99 definition: */
753 #ifdef ULLONG_MAX
754 #define ULONGLONG_MAX  ULLONG_MAX
755 #else
756 #define ULONGLONG_MAX ((unsigned long long)(~0ULL))
757 #endif
758 #endif /* defined (HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)*/
759 
760 #define INT_MIN64       (~0x7FFFFFFFFFFFFFFFLL)
761 #define INT_MAX64       0x7FFFFFFFFFFFFFFFLL
762 #define INT_MIN32       (~0x7FFFFFFFL)
763 #define INT_MAX32       0x7FFFFFFFL
764 #define UINT_MAX32      0xFFFFFFFFL
765 #define INT_MIN24       (~0x007FFFFF)
766 #define INT_MAX24       0x007FFFFF
767 #define UINT_MAX24      0x00FFFFFF
768 #define INT_MIN16       (~0x7FFF)
769 #define INT_MAX16       0x7FFF
770 #define UINT_MAX16      0xFFFF
771 #define INT_MIN8        (~0x7F)
772 #define INT_MAX8        0x7F
773 #define UINT_MAX8       0xFF
774 
775 /* From limits.h instead */
776 #ifndef DBL_MIN
777 #define DBL_MIN		4.94065645841246544e-324
778 #define FLT_MIN		((float)1.40129846432481707e-45)
779 #endif
780 #ifndef DBL_MAX
781 #define DBL_MAX		1.79769313486231470e+308
782 #define FLT_MAX		((float)3.40282346638528860e+38)
783 #endif
784 #ifndef SIZE_T_MAX
785 #define SIZE_T_MAX      (~((size_t) 0))
786 #endif
787 
788 #include <math.h>
789 
790 #if (__cplusplus >= 201103L)
791   /* For C++11 use the new std functions rather than C99 macros. */
792   #include <cmath>
793   #define my_isfinite(X) std::isfinite(X)
794   #define my_isnan(X) std::isnan(X)
795   #define my_isinf(X) std::isinf(X)
796 #else
797   #ifdef HAVE_LLVM_LIBCPP /* finite is deprecated in libc++ */
798     #define my_isfinite(X) isfinite(X)
799   #else
800     #define my_isfinite(X) finite(X)
801   #endif
802   #define my_isnan(X) isnan(X)
803   #ifdef HAVE_ISINF
804     /* System-provided isinf() is available and safe to use */
805     #define my_isinf(X) isinf(X)
806   #else /* !HAVE_ISINF */
807     #define my_isinf(X) (!my_isfinite(X) && !my_isnan(X))
808   #endif
809 #endif /* __cplusplus >= 201103L */
810 
811 /* Define missing math constants. */
812 #ifndef M_PI
813 #define M_PI 3.14159265358979323846
814 #endif
815 #ifndef M_E
816 #define M_E 2.7182818284590452354
817 #endif
818 #ifndef M_LN2
819 #define M_LN2 0.69314718055994530942
820 #endif
821 
822 /*
823   Max size that must be added to a so that we know Size to make
824   adressable obj.
825 */
826 #if SIZEOF_CHARP == 4
827 typedef long		my_ptrdiff_t;
828 #else
829 typedef long long	my_ptrdiff_t;
830 #endif
831 
832 #define MY_ALIGN(A,L)	(((A) + (L) - 1) & ~((L) - 1))
833 #define ALIGN_SIZE(A)	MY_ALIGN((A),sizeof(double))
834 /* Size to make adressable obj. */
835 #define ADD_TO_PTR(ptr,size,type) (type) ((uchar*) (ptr)+size)
836 #define PTR_BYTE_DIFF(A,B) (my_ptrdiff_t) ((uchar*) (A) - (uchar*) (B))
837 
838 /*
839   Custom version of standard offsetof() macro which can be used to get
840   offsets of members in class for non-POD types (according to the current
841   version of C++ standard offsetof() macro can't be used in such cases and
842   attempt to do so causes warnings to be emitted, OTOH in many cases it is
843   still OK to assume that all instances of the class has the same offsets
844   for the same members).
845 
846   This is temporary solution which should be removed once File_parser class
847   and related routines are refactored.
848 */
849 
850 #define my_offsetof(TYPE, MEMBER) \
851         ((size_t)((char *)&(((TYPE *)0x10)->MEMBER) - (char*)0x10))
852 
853 #define NullS		(char *) 0
854 
855 #ifdef STDCALL
856 #undef STDCALL
857 #endif
858 
859 #ifdef _WIN32
860 #define STDCALL __stdcall
861 #else
862 #define STDCALL
863 #endif
864 
865 /* Typdefs for easyier portability */
866 
867 #ifndef HAVE_UCHAR
868 typedef unsigned char	uchar;	/* Short for unsigned char */
869 #endif
870 
871 #ifndef HAVE_INT8
872 typedef signed char int8;       /* Signed integer >= 8  bits */
873 #endif
874 #ifndef HAVE_UINT8
875 typedef unsigned char uint8;    /* Unsigned integer >= 8  bits */
876 #endif
877 #ifndef HAVE_INT16
878 typedef short int16;
879 #endif
880 #ifndef HAVE_UINT16
881 typedef unsigned short uint16;
882 #endif
883 #if SIZEOF_INT == 4
884 #ifndef HAVE_INT32
885 typedef int int32;
886 #endif
887 #ifndef HAVE_UINT32
888 typedef unsigned int uint32;
889 #endif
890 #elif SIZEOF_LONG == 4
891 #ifndef HAVE_INT32
892 typedef long int32;
893 #endif
894 #ifndef HAVE_UINT32
895 typedef unsigned long uint32;
896 #endif
897 #else
898 #error Neither int or long is of 4 bytes width
899 #endif
900 
901 #if !defined(HAVE_ULONG) && !defined(__USE_MISC)
902 typedef unsigned long	ulong;		  /* Short for unsigned long */
903 #endif
904 #ifndef longlong_defined
905 /*
906   Using [unsigned] long long is preferable as [u]longlong because we use
907   [unsigned] long long unconditionally in many places,
908   for example in constants with [U]LL suffix.
909 */
910 #if defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
911 typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
912 typedef long long int	longlong;
913 #else
914 typedef unsigned long	ulonglong;	  /* ulong or unsigned long long */
915 typedef long		longlong;
916 #endif
917 #endif
918 #ifndef HAVE_INT64
919 typedef longlong int64;
920 #endif
921 #ifndef HAVE_UINT64
922 typedef ulonglong uint64;
923 #endif
924 
925 #if defined(NO_CLIENT_LONG_LONG)
926 typedef unsigned long my_ulonglong;
927 #elif defined (__WIN__)
928 typedef unsigned __int64 my_ulonglong;
929 #else
930 typedef unsigned long long my_ulonglong;
931 #endif
932 
933 #if SIZEOF_CHARP == SIZEOF_INT
934 typedef int intptr;
935 #elif SIZEOF_CHARP == SIZEOF_LONG
936 typedef long intptr;
937 #elif SIZEOF_CHARP == SIZEOF_LONG_LONG
938 typedef long long intptr;
939 #else
940 #error sizeof(void *) is neither sizeof(int) nor sizeof(long) nor sizeof(long long)
941 #endif
942 
943 #define MY_ERRPTR ((void*)(intptr)1)
944 
945 #if defined(_WIN32)
946 typedef unsigned long long my_off_t;
947 typedef unsigned long long os_off_t;
948 #else
949 typedef off_t os_off_t;
950 #if SIZEOF_OFF_T > 4
951 typedef ulonglong my_off_t;
952 #else
953 typedef unsigned long my_off_t;
954 #endif
955 #endif /*_WIN32*/
956 #define MY_FILEPOS_ERROR	(~(my_off_t) 0)
957 
958 /*
959   TODO Convert these to use Bitmap class.
960  */
961 typedef ulonglong table_map;          /* Used for table bits in join */
962 typedef ulonglong nesting_map;  /* Used for flags of nesting constructs */
963 
964 #if defined(__WIN__)
965 #define socket_errno	WSAGetLastError()
966 #define SOCKET_EINTR	WSAEINTR
967 #define SOCKET_EAGAIN	WSAEINPROGRESS
968 #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
969 #define SOCKET_EADDRINUSE WSAEADDRINUSE
970 #define SOCKET_ETIMEDOUT WSAETIMEDOUT
971 #define SOCKET_ECONNRESET WSAECONNRESET
972 #define SOCKET_ENFILE	ENFILE
973 #define SOCKET_EMFILE	EMFILE
974 #else /* Unix */
975 #define socket_errno	errno
976 #define closesocket(A)	close(A)
977 #define SOCKET_EINTR	EINTR
978 #define SOCKET_EAGAIN	EAGAIN
979 #define SOCKET_EWOULDBLOCK EWOULDBLOCK
980 #define SOCKET_EADDRINUSE EADDRINUSE
981 #define SOCKET_ETIMEDOUT ETIMEDOUT
982 #define SOCKET_ECONNRESET ECONNRESET
983 #define SOCKET_ENFILE	ENFILE
984 #define SOCKET_EMFILE	EMFILE
985 #endif
986 
987 typedef int		myf;	/* Type of MyFlags in my_funcs */
988 typedef char		my_bool; /* Small bool */
989 
990 /* Macros for converting *constants* to the right type */
991 #define MYF(v)		(myf) (v)
992 
993 #ifndef LL
994 #ifdef HAVE_LONG_LONG
995 #define LL(A) A ## LL
996 #else
997 #define LL(A) A ## L
998 #endif
999 #endif
1000 
1001 #ifndef ULL
1002 #ifdef HAVE_LONG_LONG
1003 #define ULL(A) A ## ULL
1004 #else
1005 #define ULL(A) A ## UL
1006 #endif
1007 #endif
1008 
1009 /*
1010   Defines to make it possible to prioritize register assignments. No
1011   longer that important with modern compilers.
1012 */
1013 #ifndef USING_X
1014 #define reg1 register
1015 #define reg2 register
1016 #define reg3 register
1017 #define reg4 register
1018 #define reg5 register
1019 #define reg6 register
1020 #define reg7 register
1021 #define reg8 register
1022 #define reg9 register
1023 #define reg10 register
1024 #define reg11 register
1025 #define reg12 register
1026 #define reg13 register
1027 #define reg14 register
1028 #define reg15 register
1029 #define reg16 register
1030 #endif
1031 
1032 /* Some helper macros */
1033 #define YESNO(X) ((X) ? "yes" : "no")
1034 
1035 #define MY_HOW_OFTEN_TO_ALARM	2	/* How often we want info on screen */
1036 #define MY_HOW_OFTEN_TO_WRITE	1000	/* How often we want info on screen */
1037 
1038 #include <my_byteorder.h>
1039 
1040 #ifdef HAVE_CHARSET_utf8
1041 #define MYSQL_UNIVERSAL_CLIENT_CHARSET "utf8"
1042 #else
1043 #define MYSQL_UNIVERSAL_CLIENT_CHARSET MYSQL_DEFAULT_CHARSET_NAME
1044 #endif
1045 
1046 #if defined(EMBEDDED_LIBRARY) && !defined(HAVE_EMBEDDED_PRIVILEGE_CONTROL)
1047 #define NO_EMBEDDED_ACCESS_CHECKS
1048 #endif
1049 
1050 #if defined(_WIN32)
1051 #define dlsym(lib, name) (void*)GetProcAddress((HMODULE)lib, name)
1052 #define dlopen(libname, unused) LoadLibraryEx(libname, NULL, 0)
1053 #define dlclose(lib) FreeLibrary((HMODULE)lib)
1054 #ifndef HAVE_DLOPEN
1055 #define HAVE_DLOPEN
1056 #endif
1057 #endif
1058 
1059 #ifdef HAVE_DLOPEN
1060 #if defined(HAVE_DLFCN_H)
1061 #include <dlfcn.h>
1062 #endif
1063 #endif
1064 
1065 #ifndef HAVE_DLERROR
1066 #ifdef _WIN32
1067 #define DLERROR_GENERATE(errmsg, error_number) \
1068   char win_errormsg[2048]; \
1069   if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, \
1070                    0, error_number, 0, win_errormsg, 2048, NULL)) \
1071   { \
1072     char *ptr; \
1073     for (ptr= &win_errormsg[0] + strlen(win_errormsg) - 1; \
1074          ptr >= &win_errormsg[0] && strchr("\r\n\t\0x20", *ptr); \
1075          ptr--) \
1076       *ptr= 0; \
1077     errmsg= win_errormsg; \
1078   } \
1079   else \
1080     errmsg= ""
1081 #define dlerror() ""
1082 #define dlopen_errno GetLastError()
1083 #else /* _WIN32 */
1084 #define dlerror() "No support for dynamic loading (static build?)"
1085 #define DLERROR_GENERATE(errmsg, error_number) errmsg= dlerror()
1086 #define dlopen_errno errno
1087 #endif /* _WIN32 */
1088 #else /* HAVE_DLERROR */
1089 #define DLERROR_GENERATE(errmsg, error_number) errmsg= dlerror()
1090 #define dlopen_errno errno
1091 #endif /* HAVE_DLERROR */
1092 
1093 
1094 /*
1095  *  Include standard definitions of operator new and delete.
1096  */
1097 #ifdef __cplusplus
1098 #include <new>
1099 #endif
1100 
1101 /* Length of decimal number represented by INT32. */
1102 #define MY_INT32_NUM_DECIMAL_DIGITS 11U
1103 
1104 /* Length of decimal number represented by INT64. */
1105 #define MY_INT64_NUM_DECIMAL_DIGITS 21U
1106 
1107 /* Define some useful general macros (should be done after all headers). */
1108 #define MY_MAX(a, b)	((a) > (b) ? (a) : (b))
1109 #define MY_MIN(a, b)	((a) < (b) ? (a) : (b))
1110 
1111 /*
1112   Only Linux is known to need an explicit sync of the directory to make sure a
1113   file creation/deletion/renaming in(from,to) this directory durable.
1114 */
1115 #ifdef TARGET_OS_LINUX
1116 #define NEED_EXPLICIT_SYNC_DIR 1
1117 #else
1118 /*
1119   On linux default rwlock scheduling policy is good enough for
1120   waiting_threads.c, on other systems use our special implementation
1121   (which is slower).
1122 
1123   QQ perhaps this should be tested in configure ? how ?
1124 */
1125 #define WT_RWLOCKS_USE_MUTEXES 1
1126 #endif
1127 
1128 #if !defined(__cplusplus) && !defined(bool)
1129 #define bool In_C_you_should_use_my_bool_instead()
1130 #endif
1131 
1132 /* Provide __func__ macro definition for platforms that miss it. */
1133 #if __STDC_VERSION__ < 199901L
1134 #  if __GNUC__ >= 2
1135 #    define __func__ __FUNCTION__
1136 #  else
1137 #    define __func__ "<unknown>"
1138 #  endif
1139 #elif defined(_MSC_VER)
1140 #  if _MSC_VER < 1300
1141 #    define __func__ "<unknown>"
1142 #  else
1143 #    define __func__ __FUNCTION__
1144 #  endif
1145 #elif defined(__BORLANDC__)
1146 #  define __func__ __FUNC__
1147 #else
1148 #  define __func__ "<unknown>"
1149 #endif
1150 
1151 #ifndef HAVE_RINT
1152 /**
1153    All integers up to this number can be represented exactly as double precision
1154    values (DBL_MANT_DIG == 53 for IEEE 754 hardware).
1155 */
1156 #define MAX_EXACT_INTEGER ((1LL << DBL_MANT_DIG) - 1)
1157 
1158 /**
1159    rint(3) implementation for platforms that do not have it.
1160    Always rounds to the nearest integer with ties being rounded to the nearest
1161    even integer to mimic glibc's rint() behavior in the "round-to-nearest"
1162    FPU mode. Hardware-specific optimizations are possible (frndint on x86).
1163    Unlike this implementation, hardware will also honor the FPU rounding mode.
1164 */
1165 
rint(double x)1166 static inline double rint(double x)
1167 {
1168   double f, i;
1169   f = modf(x, &i);
1170   /*
1171     All doubles with absolute values > MAX_EXACT_INTEGER are even anyway,
1172     no need to check it.
1173   */
1174   if (x > 0.0)
1175     i += (double) ((f > 0.5) || (f == 0.5 &&
1176                                  i <= (double) MAX_EXACT_INTEGER &&
1177                                  (longlong) i % 2));
1178   else
1179     i -= (double) ((f < -0.5) || (f == -0.5 &&
1180                                   i >= (double) -MAX_EXACT_INTEGER &&
1181                                   (longlong) i % 2));
1182   return i;
1183 }
1184 #endif /* HAVE_RINT */
1185 
1186 /*
1187   MYSQL_PLUGIN_IMPORT macro is used to export mysqld data
1188   (i.e variables) for usage in storage engine loadable plugins.
1189   Outside of Windows, it is dummy.
1190 */
1191 #ifndef MYSQL_PLUGIN_IMPORT
1192 #if (defined(_WIN32) && defined(MYSQL_DYNAMIC_PLUGIN))
1193 #define MYSQL_PLUGIN_IMPORT __declspec(dllimport)
1194 #else
1195 #define MYSQL_PLUGIN_IMPORT
1196 #endif
1197 #endif
1198 
1199 #include <my_dbug.h>
1200 
1201 /* Defines that are unique to the embedded version of MySQL */
1202 
1203 #ifdef EMBEDDED_LIBRARY
1204 
1205 /* Things we don't need in the embedded version of MySQL */
1206 /* TODO HF add #undef HAVE_VIO if we don't want client in embedded library */
1207 
1208 #undef HAVE_OPENSSL
1209 #undef HAVE_SMEM				/* No shared memory */
1210 
1211 #endif /* EMBEDDED_LIBRARY */
1212 
1213 
1214 enum loglevel {
1215    ERROR_LEVEL=       0,
1216    WARNING_LEVEL=     1,
1217    INFORMATION_LEVEL= 2
1218 };
1219 
1220 
1221 /*
1222   Visual Studio before the version 2010 did not have lldiv_t.
1223   In Visual Studio 2010, _MSC_VER is defined as 1600.
1224 */
1225 #if defined(_MSC_VER) && (_MSC_VER < 1600)
1226 typedef struct
1227 {
1228   long long int quot;   /* Quotient.  */
1229   long long int rem;    /* Remainder.  */
1230 } lldiv_t;
1231 #endif
1232 
1233 #endif  // MY_GLOBAL_INCLUDED
1234