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