1/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
2
3 This library is free software; you can redistribute it and/or
4 modify it under the terms of the GNU Library General Public
5 License as published by the Free Software Foundation; either
6 version 2 of the License, or (at your option) any later version.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public
14 License along with this library; if not, write to the Free
15 Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
16 MA 02111-1301, USA */
17
18/* This is the main include file that should included 'first' in every
19 C file. */
20
21#ifndef _global_h
22#define _global_h
23
24#ifdef _WIN32
25#include <winsock2.h>
26#include <windows.h>
27#include <stdlib.h>
28#define strcasecmp _stricmp
29#define sleep(x) Sleep(1000*(x))
30#ifdef _MSC_VER
31#define inline __inline
32#if _MSC_VER < 1900
33#define snprintf _snprintf
34#endif
35#endif
36#define STDCALL __stdcall
37#endif
38
39#include <ma_config.h>
40#include <assert.h>
41#ifndef __GNUC__
42#define __attribute(A)
43#endif
44
45/* Fix problem with S_ISLNK() on Linux */
46#if defined(HAVE_LINUXTHREADS)
47#undef _GNU_SOURCE
48#define _GNU_SOURCE 1
49#endif
50
51/* The client defines this to avoid all thread code */
52#if defined(UNDEF_THREADS_HACK)
53#undef THREAD
54#undef HAVE_mit_thread
55#undef HAVE_LINUXTHREADS
56#undef HAVE_UNIXWARE7_THREADS
57#endif
58
59#ifdef HAVE_THREADS_WITHOUT_SOCKETS
60/* MIT pthreads does not work with unix sockets */
61#undef HAVE_SYS_UN_H
62#endif
63
64#define __EXTENSIONS__ 1 /* We want some extension */
65#ifndef __STDC_EXT__
66#define __STDC_EXT__ 1 /* To get large file support on hpux */
67#endif
68
69#if defined(THREAD) && !defined(_WIN32)
70#ifndef _POSIX_PTHREAD_SEMANTICS
71#define _POSIX_PTHREAD_SEMANTICS /* We want posix threads */
72#endif
73/* was #if defined(HAVE_LINUXTHREADS) || defined(HAVE_DEC_THREADS) || defined(HPUX) */
74#if !defined(SCO)
75#define _REENTRANT 1 /* Some thread libraries require this */
76#endif
77#if !defined(_THREAD_SAFE) && !defined(_AIX)
78#define _THREAD_SAFE /* Required for OSF1 */
79#endif
80#ifndef HAVE_mit_thread
81#ifdef HAVE_UNIXWARE7_THREADS
82#include <thread.h>
83#else
84#include <pthread.h> /* AIX must have this included first */
85#endif /* HAVE_UNIXWARE7_THREADS */
86#endif /* HAVE_mit_thread */
87#if !defined(SCO) && !defined(_REENTRANT)
88#define _REENTRANT 1 /* Threads requires reentrant code */
89#endif
90#endif /* THREAD */
91
92/* Go around some bugs in different OS and compilers */
93#ifdef _AIX /* By soren@t.dk */
94#define _H_STRINGS
95#define _SYS_STREAM_H
96#define _AIX32_CURSES
97#define ulonglong2double(A) my_ulonglong2double(A)
98#define my_off_t2double(A) my_ulonglong2double(A)
99#ifdef __cplusplus
100extern "C" {
101#endif
102double my_ulonglong2double(unsigned long long A);
103#ifdef __cplusplus
104}
105#endif
106#endif /* _AIX */
107
108#ifdef HAVE_BROKEN_SNPRINTF /* HPUX 10.20 don't have this defined */
109#undef HAVE_SNPRINTF
110#endif
111#if defined(HAVE_BROKEN_INLINE) && !defined(__cplusplus)
112#undef inline
113#define inline
114#endif
115
116#ifdef UNDEF_HAVE_GETHOSTBYNAME_R /* For OSF4.x */
117#undef HAVE_GETHOSTBYNAME_R
118#endif
119#ifdef UNDEF_HAVE_INITGROUPS /* For AIX 4.3 */
120#undef HAVE_INITGROUPS
121#endif
122
123/* Fix a bug in gcc 2.8.0 on IRIX 6.2 */
124#if SIZEOF_LONG == 4 && defined(__LONG_MAX__)
125#undef __LONG_MAX__ /* Is a longlong value in gcc 2.8.0 ??? */
126#define __LONG_MAX__ 2147483647
127#endif
128
129/* Fix problem when linking c++ programs with gcc 3.x */
130#ifdef DEFINE_CXA_PURE_VIRTUAL
131#define FIX_GCC_LINKING_PROBLEM extern "C" { int __cxa_pure_virtual() {return 0;} }
132#else
133#define FIX_GCC_LINKING_PROBLEM
134#endif
135
136/* egcs 1.1.2 has a problem with memcpy on Alpha */
137#if defined(__GNUC__) && defined(__alpha__) && ! (__GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95))
138#define BAD_MEMCPY
139#endif
140
141/* In Linux-alpha we have atomic.h if we are using gcc */
142#if defined(HAVE_LINUXTHREADS) && defined(__GNUC__) && defined(__alpha__) && (__GNUC__ > 2 || ( __GNUC__ == 2 && __GNUC_MINOR__ >= 95)) && !defined(HAVE_ATOMIC_ADD)
143#define HAVE_ATOMIC_ADD
144#define HAVE_ATOMIC_SUB
145#endif
146
147/* In Linux-ia64 including atomic.h will give us an error */
148#if (defined(HAVE_LINUXTHREADS) && defined(__GNUC__) && (defined(__ia64__) || defined(__powerpc64__))) || !defined(THREAD)
149#undef HAVE_ATOMIC_ADD
150#undef HAVE_ATOMIC_SUB
151#endif
152
153#if defined(_lint) && !defined(lint)
154#define lint
155#endif
156#if SIZEOF_LONG_LONG > 4 && !defined(_LONG_LONG)
157#define _LONG_LONG 1 /* For AIX string library */
158#endif
159
160#ifndef stdin
161#include <stdio.h>
162#endif
163#ifdef HAVE_STDLIB_H
164#include <stdlib.h>
165#endif
166#ifdef HAVE_STDDEF_H
167#include <stddef.h>
168#endif
169
170#include <math.h>
171#ifdef HAVE_LIMITS_H
172#include <limits.h>
173#endif
174#ifdef HAVE_FLOAT_H
175#include <float.h>
176#endif
177
178#ifdef HAVE_SYS_TYPES_H
179#include <sys/types.h>
180#endif
181#ifdef HAVE_FCNTL_H
182#include <fcntl.h>
183#endif
184#if defined(TIME_WITH_SYS_TIME)
185# include <sys/time.h>
186# include <time.h>
187#else
188# if defined(HAVE_SYS_TIME_H)
189# include <sys/time.h>
190# else
191# include <time.h>
192# endif
193#endif /* TIME_WITH_SYS_TIME */
194#ifdef HAVE_UNISTD_H
195#include <unistd.h>
196#endif
197#if defined(__cplusplus) && defined(NO_CPLUSPLUS_ALLOCA)
198#undef HAVE_ALLOCA
199#undef HAVE_ALLOCA_H
200#endif
201#ifdef HAVE_ALLOCA_H
202#include <alloca.h>
203#endif
204#ifdef HAVE_ATOMIC_ADD
205#define __SMP__
206#define CONFIG_SMP
207#include <asm/atomic.h>
208#endif
209#include <errno.h> /* Recommended by debian */
210#include <assert.h>
211
212/* Go around some bugs in different OS and compilers */
213#if defined(_HPUX_SOURCE) && defined(HAVE_SYS_STREAM_H)
214#include <sys/stream.h> /* HPUX 10.20 defines ulong here. UGLY !!! */
215#define HAVE_ULONG
216#endif
217#ifdef DONT_USE_FINITE /* HPUX 11.x has is_finite() */
218#undef HAVE_FINITE
219#endif
220#if defined(HPUX) && defined(_LARGEFILE64_SOURCE) && defined(THREAD)
221/* Fix bug in setrlimit */
222#undef setrlimit
223#define setrlimit cma_setrlimit64
224#endif
225
226/* We can not live without these */
227
228#define USE_MYFUNC 1 /* Must use syscall indirection */
229#define MASTER 1 /* Compile without unireg */
230#define ENGLISH 1 /* Messages in English */
231#define POSIX_MISTAKE 1 /* regexp: Fix stupid spec error */
232#define USE_REGEX 1 /* We want the use the regex library */
233/* Do not define for ultra sparcs */
234#define USE_BMOVE512 1 /* Use this unless the system bmove is faster */
235
236/* Paranoid settings. Define I_AM_PARANOID if you are paranoid */
237#ifdef I_AM_PARANOID
238#define DONT_ALLOW_USER_CHANGE 1
239#define DONT_USE_MYSQL_PWD 1
240#endif
241
242/* #define USE_some_charset 1 was deprecated by changes to configure */
243/* my_ctype my_to_upper, my_to_lower, my_sort_order gain theit right value */
244/* automagically during configuration */
245
246/* Does the system remember a signal handler after a signal ? */
247#ifndef HAVE_BSD_SIGNALS
248#define DONT_REMEMBER_SIGNAL
249#endif
250
251
252#if defined(_lint) || defined(FORCE_INIT_OF_VARS)
253#define LINT_INIT(var) var=0 /* No uninitialize-warning */
254#define LINT_INIT_STRUCT(var) memset(&var, 0, sizeof(var)) /* No uninitialize-warning */
255#else
256#define LINT_INIT(var)
257#define LINT_INIT_STRUCT(var)
258#endif
259
260/* Define some useful general macros */
261#if defined(__cplusplus) && defined(__GNUC__)
262#define max(a, b) ((a) >? (b))
263#define min(a, b) ((a) <? (b))
264#elif !defined(max)
265#define max(a, b) ((a) > (b) ? (a) : (b))
266#define min(a, b) ((a) < (b) ? (a) : (b))
267#endif
268
269#if defined(__EMX__) || !defined(HAVE_UINT)
270typedef unsigned int uint;
271typedef unsigned short ushort;
272#endif
273
274#define sgn(a) (((a) < 0) ? -1 : ((a) > 0) ? 1 : 0)
275#define swap(t,a,b) { register t dummy; dummy = a; a = b; b = dummy; }
276#define test(a) ((a) ? 1 : 0)
277#define set_if_bigger(a,b) { if ((a) < (b)) (a)=(b); }
278#define set_if_smaller(a,b) { if ((a) > (b)) (a)=(b); }
279#define test_all_bits(a,b) (((a) & (b)) == (b))
280#define set_bits(type, bit_count) (sizeof(type)*8 <= (bit_count) ? ~(type) 0 : ((((type) 1) << (bit_count)) - (type) 1))
281#define array_elements(A) ((uint) (sizeof(A)/sizeof(A[0])))
282#ifndef HAVE_RINT
283#define rint(A) floor((A)+0.5)
284#endif
285
286/* Define some general constants */
287#ifndef TRUE
288#define TRUE (1) /* Logical true */
289#define FALSE (0) /* Logical false */
290#endif
291
292#if defined(__GNUC__)
293#define function_volatile volatile
294#ifndef my_reinterpret_cast
295#define my_reinterpret_cast(A) reinterpret_cast<A>
296#endif
297#define my_const_cast(A) const_cast<A>
298#elif !defined(my_reinterpret_cast)
299#define my_reinterpret_cast(A) (A)
300#define my_const_cast(A) (A)
301#endif
302#if !defined(__GNUC__) && !defined(__clang__)
303#define __attribute__(A)
304#endif
305
306/* From old s-system.h */
307
308/*
309 Support macros for non ansi & other old compilers. Since such
310 things are no longer supported we do nothing. We keep then since
311 some of our code may still be needed to upgrade old customers.
312*/
313#define _VARARGS(X) X
314#define _STATIC_VARARGS(X) X
315
316#if defined(DBUG_ON) && defined(DBUG_OFF)
317#undef DBUG_OFF
318#endif
319
320#if defined(_lint) && !defined(DBUG_OFF)
321#define DBUG_OFF
322#endif
323
324#define MIN_ARRAY_SIZE 0 /* Zero or One. Gcc allows zero*/
325#define ASCII_BITS_USED 8 /* Bit char used */
326#define NEAR_F /* No near function handling */
327
328/* Some types that is different between systems */
329
330typedef int File; /* File descriptor */
331#ifndef my_socket_defined
332#define my_socket_defined
333#if defined(_WIN64)
334#define my_socket unsigned long long
335#elif defined(_WIN32)
336#define my_socket unsigned int
337#else
338typedef int my_socket;
339#endif
340#define my_socket_defined
341#endif
342#ifndef INVALID_SOCKET
343#define INVALID_SOCKET -1
344#endif
345
346#if defined(__GNUC__) && !defined(_lint)
347typedef char pchar; /* Mixed prototypes can take char */
348typedef char puchar; /* Mixed prototypes can take char */
349typedef char pbool; /* Mixed prototypes can take char */
350typedef short pshort; /* Mixed prototypes can take short int */
351typedef float pfloat; /* Mixed prototypes can take float */
352#else
353typedef int pchar; /* Mixed prototypes can't take char */
354typedef uint puchar; /* Mixed prototypes can't take char */
355typedef int pbool; /* Mixed prototypes can't take char */
356typedef int pshort; /* Mixed prototypes can't take short int */
357typedef double pfloat; /* Mixed prototypes can't take float */
358#endif
359typedef int (*qsort_cmp)(const void *,const void *);
360#ifdef HAVE_mit_thread
361#define qsort_t void
362#undef QSORT_TYPE_IS_VOID
363#define QSORT_TYPE_IS_VOID
364#else
365#define qsort_t RETQSORTTYPE /* Broken GCC can't handle typedef !!!! */
366#endif
367
368#ifdef HAVE_SYS_SOCKET_H
369#include <sys/socket.h>
370#endif
371typedef SOCKET_SIZE_TYPE size_socket;
372
373#ifndef SOCKOPT_OPTLEN_TYPE
374#define SOCKOPT_OPTLEN_TYPE size_socket
375#endif
376
377/* file create flags */
378
379#ifndef O_SHARE
380#define O_SHARE 0 /* Flag to my_open for shared files */
381#ifndef O_BINARY
382#define O_BINARY 0 /* Flag to my_open for binary files */
383#endif
384#define FILE_BINARY 0 /* Flag to my_fopen for binary streams */
385#ifdef HAVE_FCNTL
386#define HAVE_FCNTL_LOCK
387#define F_TO_EOF 0L /* Param to lockf() to lock rest of file */
388#endif
389#endif /* O_SHARE */
390#ifndef O_TEMPORARY
391#define O_TEMPORARY 0
392#endif
393#ifndef O_SHORT_LIVED
394#define O_SHORT_LIVED 0
395#endif
396
397/* #define USE_RECORD_LOCK */
398
399 /* Unsigned types supported by the compiler */
400#define UNSINT8 /* unsigned int8 (char) */
401#define UNSINT16 /* unsigned int16 */
402#define UNSINT32 /* unsigned int32 */
403
404 /* General constants */
405#define SC_MAXWIDTH 256 /* Max width of screen (for error messages) */
406#define FN_LEN 256 /* Max file name len */
407#define FN_HEADLEN 253 /* Max length of filepart of file name */
408#define FN_EXTLEN 20 /* Max length of extension (part of FN_LEN) */
409#define FN_REFLEN 512 /* Max length of full path-name */
410#define FN_EXTCHAR '.'
411#define FN_HOMELIB '~' /* ~/ is used as abbrev for home dir */
412#define FN_CURLIB '.' /* ./ is used as abbrev for current dir */
413#define FN_PARENTDIR ".." /* Parentdirectory; Must be a string */
414#define FN_DEVCHAR ':'
415
416#ifndef FN_LIBCHAR
417#ifdef _WIN32
418#define FN_LIBCHAR '\\'
419#define FN_ROOTDIR "\\"
420#else
421#define FN_LIBCHAR '/'
422#define FN_ROOTDIR "/"
423#endif
424#define MY_NFILE 1024 /* This is only used to save filenames */
425#endif
426
427/* #define EXT_IN_LIBNAME */
428/* #define FN_NO_CASE_SENCE */
429/* #define FN_UPPER_CASE TRUE */
430
431/*
432 Io buffer size; Must be a power of 2 and a multiple of 512. May be
433 smaller what the disk page size. This influences the speed of the
434 isam btree library. eg to big to slow.
435*/
436#define IO_SIZE 4096
437/*
438 How much overhead does malloc have. The code often allocates
439 something like 1024-MALLOC_OVERHEAD bytes
440*/
441#define MALLOC_OVERHEAD 8
442 /* get memory in huncs */
443#define ONCE_ALLOC_INIT (uint) (4096-MALLOC_OVERHEAD)
444 /* Typical record cash */
445#define RECORD_CACHE_SIZE (uint) (64*1024-MALLOC_OVERHEAD)
446 /* Typical key cash */
447#define KEY_CACHE_SIZE (uint) (8*1024*1024-MALLOC_OVERHEAD)
448
449 /* Some things that this system doesn't have */
450
451#define ONLY_OWN_DATABASES /* We are using only databases by monty */
452#define NO_PISAM /* Not needed anymore */
453#define NO_MISAM /* Not needed anymore */
454#define NO_HASH /* Not needed anymore */
455#ifdef _WIN32
456#define NO_DIR_LIBRARY /* Not standard dir-library */
457#define USE_MY_STAT_STRUCT /* For my_lib */
458#ifdef _MSC_VER
459typedef SSIZE_T ssize_t;
460#endif
461#endif
462
463/* Some things that this system does have */
464
465#ifndef HAVE_ITOA
466#define USE_MY_ITOA /* There is no itoa */
467#endif
468
469/* Some defines of functions for portability */
470
471#ifndef HAVE_ATOD
472#define atod atof
473#endif
474#ifdef USE_MY_ATOF
475#define atof my_atof
476extern void init_my_atof(void);
477extern double my_atof(const char*);
478#endif
479#undef remove /* Crashes MySQL on SCO 5.0.0 */
480#ifndef _WIN32
481#define closesocket(A) close(A)
482#endif
483#ifndef ulonglong2double
484#define ulonglong2double(A) ((double) (A))
485#define my_off_t2double(A) ((double) (A))
486#endif
487
488
489#ifndef offsetof
490#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
491#endif
492#define ulong_to_double(X) ((double) (ulong) (X))
493#define SET_STACK_SIZE(X) /* Not needed on real machines */
494
495
496#ifdef HAVE_LINUXTHREADS
497/* #define pthread_sigmask(A,B,C) sigprocmask((A),(B),(C)) */
498/* #define sigset(A,B) signal((A),(B)) */
499#endif
500
501#if defined(_lint) || defined(FORCE_INIT_OF_VARS) || \
502 defined(__cplusplus) || !defined(__GNUC__)
503#define UNINIT_VAR(x) x= 0
504#else
505/* GCC specific self-initialization which inhibits the warning. */
506#define UNINIT_VAR(x) x= x
507#endif
508
509
510/* This is from the old m-machine.h file */
511
512#if SIZEOF_LONG_LONG > 4
513#define HAVE_LONG_LONG 1
514#endif
515
516#if defined(HAVE_LONG_LONG) && !defined(LONGLONG_MIN)
517#define LONGLONG_MIN ((long long) 0x8000000000000000LL)
518#define LONGLONG_MAX ((long long) 0x7FFFFFFFFFFFFFFFLL)
519#endif
520
521
522#define INT_MIN64 (~0x7FFFFFFFFFFFFFFFLL)
523#define INT_MAX64 0x7FFFFFFFFFFFFFFFLL
524#define INT_MIN32 (~0x7FFFFFFFL)
525#define INT_MAX32 0x7FFFFFFFL
526#define UINT_MAX32 0xFFFFFFFFL
527#define INT_MIN24 (~0x007FFFFF)
528#define INT_MAX24 0x007FFFFF
529#define UINT_MAX24 0x00FFFFFF
530#define INT_MIN16 (~0x7FFF)
531#define INT_MAX16 0x7FFF
532#define UINT_MAX16 0xFFFF
533#define INT_MIN8 (~0x7F)
534#define INT_MAX8 0x7F
535#define UINT_MAX8 0xFF
536
537#ifndef ULL
538#ifdef HAVE_LONG_LONG
539#define ULL(A) A ## ULL
540#else
541#define ULL(A) A ## UL
542#endif
543#endif
544
545#if defined(HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)
546/* First check for ANSI C99 definition: */
547#ifdef ULLONG_MAX
548#define ULONGLONG_MAX ULLONG_MAX
549#else
550#define ULONGLONG_MAX ((unsigned long long)(~0ULL))
551#endif
552#endif /* defined (HAVE_LONG_LONG) && !defined(ULONGLONG_MAX)*/
553
554/* From limits.h instead */
555#ifndef DBL_MIN
556#define DBL_MIN 4.94065645841246544e-324
557#define FLT_MIN ((float)1.40129846432481707e-45)
558#endif
559#ifndef DBL_MAX
560#define DBL_MAX 1.79769313486231470e+308
561#define FLT_MAX ((float)3.40282346638528860e+38)
562#endif
563
564/*
565 Max size that must be added to a so that we know Size to make
566 addressable obj.
567*/
568typedef long my_ptrdiff_t;
569#define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1))
570#define ALIGN_SIZE(A) MY_ALIGN((A),sizeof(double))
571/* Size to make addressable obj. */
572#define ALIGN_PTR(A, t) ((t*) MY_ALIGN((A),sizeof(t)))
573 /* Offset of filed f in structure t */
574#define OFFSET(t, f) ((size_t)(char *)&((t *)0)->f)
575#define ADD_TO_PTR(ptr,size,type) (type) ((unsigned char*) (ptr)+size)
576#define PTR_BYTE_DIFF(A,B) (my_ptrdiff_t) ((unsigned char*) (A) - (unsigned char*) (B))
577
578#define NullS (char *) 0
579/* Nowadays we do not support MessyDos */
580#ifndef NEAR
581#define NEAR /* Who needs segments ? */
582#define FAR /* On a good machine */
583#ifndef HUGE_PTR
584#define HUGE_PTR
585#endif
586#endif
587#if defined(__IBMC__) || defined(__IBMCPP__)
588#define STDCALL _System _Export
589#elif !defined( STDCALL)
590#define STDCALL
591#endif
592
593/* Typdefs for easyier portability */
594
595#if defined(VOIDTYPE)
596typedef void *gptr; /* Generic pointer */
597#else
598typedef char *gptr; /* Generic pointer */
599#endif
600#ifndef HAVE_INT_8_16_32
601typedef char int8; /* Signed integer >= 8 bits */
602typedef short int16; /* Signed integer >= 16 bits */
603#endif
604#ifndef HAVE_UCHAR
605typedef unsigned char uchar; /* Short for unsigned char */
606#endif
607typedef unsigned char uint8; /* Short for unsigned integer >= 8 bits */
608typedef unsigned short uint16; /* Short for unsigned integer >= 16 bits */
609
610#if SIZEOF_INT == 4
611#ifndef HAVE_INT_8_16_32
612typedef int int32;
613#endif
614typedef unsigned int uint32; /* Short for unsigned integer >= 32 bits */
615#elif SIZEOF_LONG == 4
616#ifndef HAVE_INT_8_16_32
617typedef long int32;
618#endif
619typedef unsigned long uint32; /* Short for unsigned integer >= 32 bits */
620#else
621#error "Neither int or long is of 4 bytes width"
622#endif
623
624#if !defined(HAVE_ULONG) && !defined(HAVE_LINUXTHREADS) && !defined(__USE_MISC)
625typedef unsigned long ulong; /* Short for unsigned long */
626#endif
627#ifndef longlong_defined
628#if defined(HAVE_LONG_LONG) && SIZEOF_LONG != 8
629typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
630typedef long long int longlong;
631#else
632typedef unsigned long ulonglong; /* ulong or unsigned long long */
633typedef long longlong;
634#endif
635#define longlong_defined
636#endif
637
638#ifndef HAVE_INT64
639typedef longlong int64;
640#endif
641#ifndef HAVE_UINT64
642typedef ulonglong uint64;
643#endif
644
645#ifndef MIN
646#define MIN(a,b) (((a) < (b)) ? (a) : (b))
647#endif
648#ifndef MAX
649#define MAX(a,b) (((a) > (b)) ? (a) : (b))
650#endif
651#define CMP_NUM(a,b) (((a) < (b)) ? -1 : ((a) == (b)) ? 0 : 1)
652#ifdef USE_RAID
653/*
654 The following is done with a if to not get problems with pre-processors
655 with late define evaluation
656*/
657#if defined(SIZEOF_OFF_T) && SIZEOF_OFF_T == 4
658#define SYSTEM_SIZEOF_OFF_T 4
659#else
660#define SYSTEM_SIZEOF_OFF_T 8
661#endif
662#undef SIZEOF_OFF_T
663#define SIZEOF_OFF_T 8
664#else
665#define SYSTEM_SIZEOF_OFF_T SIZEOF_OFF_T
666#endif /* USE_RAID */
667
668#if defined(SIZEOF_OFF_T) && SIZEOF_OFF_T > 4
669typedef ulonglong my_off_t;
670#else
671typedef unsigned long my_off_t;
672#endif
673#define MY_FILEPOS_ERROR (~(my_off_t) 0)
674#ifndef _WIN32
675typedef off_t os_off_t;
676#endif
677
678#if defined(_WIN32)
679#define socket_errno WSAGetLastError()
680#define SOCKET_EINTR WSAEINTR
681#define SOCKET_EAGAIN WSAEWOULDBLOCK
682#define SOCKET_ENFILE ENFILE
683#define SOCKET_EMFILE EMFILE
684#define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
685#else /* Unix */
686#define socket_errno errno
687#define closesocket(A) close(A)
688#define SOCKET_EINTR EINTR
689#define SOCKET_EAGAIN EAGAIN
690#define SOCKET_EWOULDBLOCK EWOULDBLOCK
691#define SOCKET_ENFILE ENFILE
692#define SOCKET_EMFILE EMFILE
693#endif
694
695typedef uint8 int7; /* Most effective integer 0 <= x <= 127 */
696typedef short int15; /* Most effective integer 0 <= x <= 32767 */
697typedef char *my_string; /* String of characters */
698typedef unsigned long size_s; /* Size of strings (In string-funcs) */
699typedef int myf; /* Type of MyFlags in my_funcs */
700typedef char my_bool; /* Small bool */
701typedef unsigned long long my_ulonglong;
702#if !defined(bool) && !defined(bool_defined) && (!defined(HAVE_BOOL) || !defined(__cplusplus))
703typedef char bool; /* Ordinary boolean values 0 1 */
704#endif
705 /* Macros for converting *constants* to the right type */
706#define INT8(v) (int8) (v)
707#define INT16(v) (int16) (v)
708#define INT32(v) (int32) (v)
709#define MYF(v) (myf) (v)
710
711/*
712 Defines to make it possible to prioritize register assignments. No
713 longer that important with modern compilers.
714*/
715#ifndef USING_X
716#define reg1 register
717#define reg2 register
718#define reg3 register
719#define reg4 register
720#define reg5 register
721#define reg6 register
722#define reg7 register
723#define reg8 register
724#define reg9 register
725#define reg10 register
726#define reg11 register
727#define reg12 register
728#define reg13 register
729#define reg14 register
730#define reg15 register
731#define reg16 register
732#endif
733
734/* Defines for time function */
735#define SCALE_SEC 100
736#define SCALE_USEC 10000
737#define MY_HOW_OFTEN_TO_ALARM 2 /* How often we want info on screen */
738#define MY_HOW_OFTEN_TO_WRITE 1000 /* How often we want info on screen */
739
740#define NOT_FIXED_DEC 31
741
742#if defined(_WIN32) && defined(_MSVC)
743#define MYSQLND_LLU_SPEC "%I64u"
744#define MYSQLND_LL_SPEC "%I64d"
745#ifndef L64
746#define L64(x) x##i64
747#endif
748#else
749#define MYSQLND_LLU_SPEC "%llu"
750#define MYSQLND_LL_SPEC "%lld"
751#ifndef L64
752#define L64(x) x##LL
753#endif /* L64 */
754#endif /* _WIN32 */
755/*
756** Define-funktions for reading and storing in machine independent format
757** (low byte first)
758*/
759
760/* Optimized store functions for Intel x86 */
761#define int1store(T,A) *((int8*) (T)) = (A)
762#define uint1korr(A) (*(((uint8*)(A))))
763#if defined(__i386__) || defined(_WIN32)
764#define sint2korr(A) (*((int16 *) (A)))
765#define sint3korr(A) ((int32) ((((uchar) (A)[2]) & 128) ? \
766 (((uint32) 255L << 24) | \
767 (((uint32) (uchar) (A)[2]) << 16) |\
768 (((uint32) (uchar) (A)[1]) << 8) | \
769 ((uint32) (uchar) (A)[0])) : \
770 (((uint32) (uchar) (A)[2]) << 16) |\
771 (((uint32) (uchar) (A)[1]) << 8) | \
772 ((uint32) (uchar) (A)[0])))
773#define sint4korr(A) (*((long *) (A)))
774#define uint2korr(A) (*((uint16 *) (A)))
775#if defined(HAVE_purify) && !defined(_WIN32)
776#define uint3korr(A) (uint32) (((uint32) ((uchar) (A)[0])) +\
777 (((uint32) ((uchar) (A)[1])) << 8) +\
778 (((uint32) ((uchar) (A)[2])) << 16))
779#else
780/*
781 ATTENTION !
782
783 Please, note, uint3korr reads 4 bytes (not 3) !
784 It means, that you have to provide enough allocated space !
785*/
786#define uint3korr(A) (long) (*((unsigned int *) (A)) & 0xFFFFFF)
787#endif /* HAVE_purify && !_WIN32 */
788#define uint4korr(A) (*((uint32 *) (A)))
789#define uint5korr(A) ((ulonglong)(((uint32) ((uchar) (A)[0])) +\
790 (((uint32) ((uchar) (A)[1])) << 8) +\
791 (((uint32) ((uchar) (A)[2])) << 16) +\
792 (((uint32) ((uchar) (A)[3])) << 24)) +\
793 (((ulonglong) ((uchar) (A)[4])) << 32))
794#define uint6korr(A) ((ulonglong)(((uint32) ((uchar) (A)[0])) + \
795 (((uint32) ((uchar) (A)[1])) << 8) + \
796 (((uint32) ((uchar) (A)[2])) << 16) + \
797 (((uint32) ((uchar) (A)[3])) << 24)) + \
798 (((ulonglong) ((uchar) (A)[4])) << 32) + \
799 (((ulonglong) ((uchar) (A)[5])) << 40))
800#define uint8korr(A) (*((ulonglong *) (A)))
801#define sint8korr(A) (*((longlong *) (A)))
802#define int2store(T,A) *((uint16*) (T))= (uint16) (A)
803#define int3store(T,A) do { *(T)= (uchar) ((A));\
804 *(T+1)=(uchar) (((uint) (A) >> 8));\
805 *(T+2)=(uchar) (((A) >> 16)); } while (0)
806#define int4store(T,A) *((long *) (T))= (long) (A)
807#define int5store(T,A) do { *(T)= (uchar)((A));\
808 *((T)+1)=(uchar) (((A) >> 8));\
809 *((T)+2)=(uchar) (((A) >> 16));\
810 *((T)+3)=(uchar) (((A) >> 24)); \
811 *((T)+4)=(uchar) (((A) >> 32)); } while(0)
812#define int6store(T,A) do { *(T)= (uchar)((A)); \
813 *((T)+1)=(uchar) (((A) >> 8)); \
814 *((T)+2)=(uchar) (((A) >> 16)); \
815 *((T)+3)=(uchar) (((A) >> 24)); \
816 *((T)+4)=(uchar) (((A) >> 32)); \
817 *((T)+5)=(uchar) (((A) >> 40)); } while(0)
818#define int8store(T,A) *((ulonglong *) (T))= (ulonglong) (A)
819
820typedef union {
821 double v;
822 long m[2];
823} doubleget_union;
824#define doubleget(V,M) \
825do { doubleget_union _tmp; \
826 _tmp.m[0] = *((long*)(M)); \
827 _tmp.m[1] = *(((long*) (M))+1); \
828 (V) = _tmp.v; } while(0)
829#define doublestore(T,V) do { *((long *) T) = ((doubleget_union *)&V)->m[0]; \
830 *(((long *) T)+1) = ((doubleget_union *)&V)->m[1]; \
831 } while (0)
832#define float4get(V,M) do { *((float *) &(V)) = *((float*) (M)); } while(0)
833#define float8get(V,M) doubleget((V),(M))
834#define float4store(V,M) memcpy((uchar*) V,(uchar*) (&M),sizeof(float))
835#define floatstore(T,V) memcpy((uchar*)(T), (uchar*)(&V),sizeof(float))
836#define floatget(V,M) memcpy((uchar*) &V,(uchar*) (M),sizeof(float))
837#define float8store(V,M) doublestore((V),(M))
838#else
839
840/*
841 We're here if it's not a IA-32 architecture (Win32 and UNIX IA-32 defines
842 were done before)
843*/
844#define sint2korr(A) (int16) (((int16) ((uchar) (A)[0])) +\
845 ((int16) ((int16) (A)[1]) << 8))
846#define sint3korr(A) ((int32) ((((uchar) (A)[2]) & 128) ? \
847 (((uint32) 255L << 24) | \
848 (((uint32) (uchar) (A)[2]) << 16) |\
849 (((uint32) (uchar) (A)[1]) << 8) | \
850 ((uint32) (uchar) (A)[0])) : \
851 (((uint32) (uchar) (A)[2]) << 16) |\
852 (((uint32) (uchar) (A)[1]) << 8) | \
853 ((uint32) (uchar) (A)[0])))
854#define sint4korr(A) (int32) (((int32) ((uchar) (A)[0])) +\
855 (((int32) ((uchar) (A)[1]) << 8)) +\
856 (((int32) ((uchar) (A)[2]) << 16)) +\
857 (((int32) ((int16) (A)[3]) << 24)))
858#define sint8korr(A) (longlong) uint8korr(A)
859#define uint2korr(A) (uint16) (((uint16) ((uchar) (A)[0])) +\
860 ((uint16) ((uchar) (A)[1]) << 8))
861#define uint3korr(A) (uint32) (((uint32) ((uchar) (A)[0])) +\
862 (((uint32) ((uchar) (A)[1])) << 8) +\
863 (((uint32) ((uchar) (A)[2])) << 16))
864#define uint4korr(A) (uint32) (((uint32) ((uchar) (A)[0])) +\
865 (((uint32) ((uchar) (A)[1])) << 8) +\
866 (((uint32) ((uchar) (A)[2])) << 16) +\
867 (((uint32) ((uchar) (A)[3])) << 24))
868#define uint5korr(A) ((ulonglong)(((uint32) ((uchar) (A)[0])) +\
869 (((uint32) ((uchar) (A)[1])) << 8) +\
870 (((uint32) ((uchar) (A)[2])) << 16) +\
871 (((uint32) ((uchar) (A)[3])) << 24)) +\
872 (((ulonglong) ((uchar) (A)[4])) << 32))
873#define uint6korr(A) ((ulonglong)(((uint32) ((uchar) (A)[0])) + \
874 (((uint32) ((uchar) (A)[1])) << 8) + \
875 (((uint32) ((uchar) (A)[2])) << 16) + \
876 (((uint32) ((uchar) (A)[3])) << 24)) + \
877 (((ulonglong) ((uchar) (A)[4])) << 32) + \
878 (((ulonglong) ((uchar) (A)[5])) << 40))
879#define uint8korr(A) ((ulonglong)(((uint32) ((uchar) (A)[0])) +\
880 (((uint32) ((uchar) (A)[1])) << 8) +\
881 (((uint32) ((uchar) (A)[2])) << 16) +\
882 (((uint32) ((uchar) (A)[3])) << 24)) +\
883 (((ulonglong) (((uint32) ((uchar) (A)[4])) +\
884 (((uint32) ((uchar) (A)[5])) << 8) +\
885 (((uint32) ((uchar) (A)[6])) << 16) +\
886 (((uint32) ((uchar) (A)[7])) << 24))) <<\
887 32))
888#define int2store(T,A) do { uint def_temp= (uint) (A) ;\
889 *((uchar*) (T))= (uchar)(def_temp); \
890 *((uchar*) (T)+1)=(uchar)((def_temp >> 8)); \
891 } while(0)
892#define int3store(T,A) do { /*lint -save -e734 */\
893 *((uchar*)(T))=(uchar) ((A));\
894 *((uchar*) (T)+1)=(uchar) (((A) >> 8));\
895 *((uchar*)(T)+2)=(uchar) (((A) >> 16)); \
896 /*lint -restore */} while(0)
897#define int4store(T,A) do { *((char *)(T))=(char) ((A));\
898 *(((char *)(T))+1)=(char) (((A) >> 8));\
899 *(((char *)(T))+2)=(char) (((A) >> 16));\
900 *(((char *)(T))+3)=(char) (((A) >> 24)); } while(0)
901#define int5store(T,A) do { *((char *)(T))= (char)((A)); \
902 *(((char *)(T))+1)= (char)(((A) >> 8)); \
903 *(((char *)(T))+2)= (char)(((A) >> 16)); \
904 *(((char *)(T))+3)= (char)(((A) >> 24)); \
905 *(((char *)(T))+4)= (char)(((A) >> 32)); \
906 } while(0)
907#define int6store(T,A) do { *((char *)(T))= (char)((A)); \
908 *(((char *)(T))+1)= (char)(((A) >> 8)); \
909 *(((char *)(T))+2)= (char)(((A) >> 16)); \
910 *(((char *)(T))+3)= (char)(((A) >> 24)); \
911 *(((char *)(T))+4)= (char)(((A) >> 32)); \
912 *(((char *)(T))+5)= (char)(((A) >> 40)); \
913 } while(0)
914#define int8store(T,A) do { uint def_temp= (uint) (A), def_temp2= (uint) ((A) >> 32); \
915 int4store((T),def_temp); \
916 int4store((T+4),def_temp2); } while(0)
917#ifdef HAVE_BIGENDIAN
918#define float4store(T,A) do { *(T)= ((uchar *) &A)[3];\
919 *((T)+1)=(char) ((uchar *) &A)[2];\
920 *((T)+2)=(char) ((uchar *) &A)[1];\
921 *((T)+3)=(char) ((uchar *) &A)[0]; } while(0)
922
923#define float4get(V,M) do { float def_temp;\
924 ((uchar*) &def_temp)[0]=(M)[3];\
925 ((uchar*) &def_temp)[1]=(M)[2];\
926 ((uchar*) &def_temp)[2]=(M)[1];\
927 ((uchar*) &def_temp)[3]=(M)[0];\
928 (V)=def_temp; } while(0)
929#define float8store(T,V) do { *(T)= ((uchar *) &V)[7];\
930 *((T)+1)=(char) ((uchar *) &V)[6];\
931 *((T)+2)=(char) ((uchar *) &V)[5];\
932 *((T)+3)=(char) ((uchar *) &V)[4];\
933 *((T)+4)=(char) ((uchar *) &V)[3];\
934 *((T)+5)=(char) ((uchar *) &V)[2];\
935 *((T)+6)=(char) ((uchar *) &V)[1];\
936 *((T)+7)=(char) ((uchar *) &V)[0]; } while(0)
937
938#define float8get(V,M) do { double def_temp;\
939 ((uchar*) &def_temp)[0]=(M)[7];\
940 ((uchar*) &def_temp)[1]=(M)[6];\
941 ((uchar*) &def_temp)[2]=(M)[5];\
942 ((uchar*) &def_temp)[3]=(M)[4];\
943 ((uchar*) &def_temp)[4]=(M)[3];\
944 ((uchar*) &def_temp)[5]=(M)[2];\
945 ((uchar*) &def_temp)[6]=(M)[1];\
946 ((uchar*) &def_temp)[7]=(M)[0];\
947 (V) = def_temp; } while(0)
948#else
949#define float4get(V,M) memcpy(&V, (M), sizeof(float))
950#define float4store(V,M) memcpy(V, (&M), sizeof(float))
951
952#if defined(__FLOAT_WORD_ORDER) && (__FLOAT_WORD_ORDER == __BIG_ENDIAN)
953#define doublestore(T,V) do { *(((char*)T)+0)=(char) ((uchar *) &V)[4];\
954 *(((char*)T)+1)=(char) ((uchar *) &V)[5];\
955 *(((char*)T)+2)=(char) ((uchar *) &V)[6];\
956 *(((char*)T)+3)=(char) ((uchar *) &V)[7];\
957 *(((char*)T)+4)=(char) ((uchar *) &V)[0];\
958 *(((char*)T)+5)=(char) ((uchar *) &V)[1];\
959 *(((char*)T)+6)=(char) ((uchar *) &V)[2];\
960 *(((char*)T)+7)=(char) ((uchar *) &V)[3]; }\
961 while(0)
962#define doubleget(V,M) do { double def_temp;\
963 ((uchar*) &def_temp)[0]=(M)[4];\
964 ((uchar*) &def_temp)[1]=(M)[5];\
965 ((uchar*) &def_temp)[2]=(M)[6];\
966 ((uchar*) &def_temp)[3]=(M)[7];\
967 ((uchar*) &def_temp)[4]=(M)[0];\
968 ((uchar*) &def_temp)[5]=(M)[1];\
969 ((uchar*) &def_temp)[6]=(M)[2];\
970 ((uchar*) &def_temp)[7]=(M)[3];\
971 (V) = def_temp; } while(0)
972#endif /* __FLOAT_WORD_ORDER */
973
974#define float8get(V,M) doubleget((V),(M))
975#define float8store(V,M) doublestore((V),(M))
976#endif /* WORDS_BIGENDIAN */
977
978#endif /* __i386__ OR _WIN32 */
979
980/*
981 Macro for reading 32-bit integer from network byte order (big-endian)
982 from unaligned memory location.
983*/
984#define int4net(A) (int32) (((uint32) ((uchar) (A)[3])) |\
985 (((uint32) ((uchar) (A)[2])) << 8) |\
986 (((uint32) ((uchar) (A)[1])) << 16) |\
987 (((uint32) ((uchar) (A)[0])) << 24))
988/*
989 Define-funktions for reading and storing in machine format from/to
990 short/long to/from some place in memory V should be a (not
991 register) variable, M is a pointer to byte
992*/
993
994#ifdef HAVE_BIGENDIAN
995
996#define ushortget(V,M) do { V = (uint16) (((uint16) ((uchar) (M)[1]))+\
997 ((uint16) ((uint16) (M)[0]) << 8)); } while(0)
998#define shortget(V,M) do { V = (short) (((short) ((uchar) (M)[1]))+\
999 ((short) ((short) (M)[0]) << 8)); } while(0)
1000#define longget(V,M) do { int32 def_temp;\
1001 ((uchar*) &def_temp)[0]=(M)[0];\
1002 ((uchar*) &def_temp)[1]=(M)[1];\
1003 ((uchar*) &def_temp)[2]=(M)[2];\
1004 ((uchar*) &def_temp)[3]=(M)[3];\
1005 (V)=def_temp; } while(0)
1006#define ulongget(V,M) do { uint32 def_temp;\
1007 ((uchar*) &def_temp)[0]=(M)[0];\
1008 ((uchar*) &def_temp)[1]=(M)[1];\
1009 ((uchar*) &def_temp)[2]=(M)[2];\
1010 ((uchar*) &def_temp)[3]=(M)[3];\
1011 (V)=def_temp; } while(0)
1012#define shortstore(T,A) do { uint def_temp=(uint) (A) ;\
1013 *(((char*)T)+1)=(char)(def_temp); \
1014 *(((char*)T)+0)=(char)(def_temp >> 8); } while(0)
1015#define longstore(T,A) do { *(((char*)T)+3)=((A));\
1016 *(((char*)T)+2)=(((A) >> 8));\
1017 *(((char*)T)+1)=(((A) >> 16));\
1018 *(((char*)T)+0)=(((A) >> 24)); } while(0)
1019
1020#define floatget(V,M) memcpy(&V, (M), sizeof(float))
1021#define floatstore(T,V) memcpy((T), (void*) (&V), sizeof(float))
1022#define doubleget(V,M) memcpy(&V, (M), sizeof(double))
1023#define doublestore(T,V) memcpy((T), (void *) &V, sizeof(double))
1024#define longlongget(V,M) memcpy(&V, (M), sizeof(ulonglong))
1025#define longlongstore(T,V) memcpy((T), &V, sizeof(ulonglong))
1026
1027#else
1028
1029#define ushortget(V,M) do { V = uint2korr(M); } while(0)
1030#define shortget(V,M) do { V = sint2korr(M); } while(0)
1031#define longget(V,M) do { V = sint4korr(M); } while(0)
1032#define ulongget(V,M) do { V = uint4korr(M); } while(0)
1033#define shortstore(T,V) int2store(T,V)
1034#define longstore(T,V) int4store(T,V)
1035#ifndef floatstore
1036#define floatstore(T,V) memcpy((T), (void *) (&V), sizeof(float))
1037#define floatget(V,M) memcpy(&V, (M), sizeof(float))
1038#endif
1039#ifndef doubleget
1040#define doubleget(V,M) memcpy(&V, (M), sizeof(double))
1041#define doublestore(T,V) memcpy((T), (void *) &V, sizeof(double))
1042#endif /* doubleget */
1043#define longlongget(V,M) memcpy(&V, (M), sizeof(ulonglong))
1044#define longlongstore(T,V) memcpy((T), &V, sizeof(ulonglong))
1045
1046#endif /* WORDS_BIGENDIAN */
1047
1048#ifndef THREAD
1049#define thread_safe_increment(V,L) (V)++
1050#define thread_safe_add(V,C,L) (V)+=(C)
1051#define thread_safe_sub(V,C,L) (V)-=(C)
1052#define statistic_increment(V,L) (V)++
1053#define statistic_add(V,C,L) (V)+=(C)
1054#endif
1055
1056#ifdef _WIN32
1057#define SO_EXT ".dll"
1058#else
1059#define SO_EXT ".so"
1060#endif
1061
1062#ifndef DBUG_OFF
1063#define dbug_assert(A) assert(A)
1064#define DBUG_ASSERT(A) assert(A)
1065#else
1066#define dbug_assert(A)
1067#define DBUG_ASSERT(A)
1068#endif
1069
1070#ifdef HAVE_DLOPEN
1071#ifdef _WIN32
1072#define dlsym(lib, name) GetProcAddress((HMODULE)lib, name)
1073#define dlopen(libname, unused) LoadLibraryEx(libname, NULL, 0)
1074#define dlclose(lib) FreeLibrary((HMODULE)lib)
1075#elif defined(HAVE_DLFCN_H)
1076#include <dlfcn.h>
1077#endif
1078#ifndef HAVE_DLERROR
1079#define dlerror() ""
1080#endif
1081#endif
1082
1083#if SIZEOF_CHARP == SIZEOF_INT
1084typedef unsigned int intptr;
1085#elif SIZEOF_CHARP == SIZEOF_LONG
1086typedef unsigned long intptr;
1087#elif SIZEOF_CHARP == SIZEOF_LONG_LONG
1088typedef unsigned long long intptr;
1089#else
1090#error sizeof(void *) is not sizeof(int, long or long long)
1091#endif
1092
1093#ifdef _WIN32
1094#define IF_WIN(A,B) A
1095#else
1096#define IF_WIN(A,B) B
1097#endif
1098
1099#ifndef RTLD_NOW
1100#define RTLD_NOW 1
1101#endif
1102
1103#endif /* _global_h */
1104