Clone of PhatAC @ https://github.com/floaterxk/PhatAC

my_global.h 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793
  1. /*
  2. Copyright (c) 2001, 2016, Oracle and/or its affiliates. All rights reserved.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; version 2 of the License.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  13. #ifndef MY_GLOBAL_INCLUDED
  14. #define MY_GLOBAL_INCLUDED
  15. /*
  16. This include file should be included first in every header file.
  17. This makes sure my_config.h is included to get platform specific
  18. symbols defined and it makes sure a lot of platform/compiler
  19. differences are mitigated.
  20. */
  21. #include "my_config.h"
  22. #define __STDC_LIMIT_MACROS /* Enable C99 limit macros */
  23. #define __STDC_FORMAT_MACROS /* Enable C99 printf format macros */
  24. #define _USE_MATH_DEFINES /* Get access to M_PI, M_E, etc. in math.h */
  25. #ifdef _WIN32
  26. /* Include common headers.*/
  27. # include <winsock2.h>
  28. # include <ws2tcpip.h> /* SOCKET */
  29. # include <io.h> /* access(), chmod() */
  30. #endif
  31. #include <stdio.h>
  32. #include <stdarg.h>
  33. #include <stdlib.h>
  34. #include <stddef.h>
  35. #include <math.h>
  36. #include <limits.h>
  37. #include <float.h>
  38. #include <fcntl.h>
  39. #include <time.h>
  40. #include <errno.h> /* Recommended by debian */
  41. #include <sys/types.h>
  42. #ifdef HAVE_SYS_SOCKET_H
  43. #include <sys/socket.h>
  44. #endif
  45. #if !defined(_WIN32)
  46. #include <netdb.h>
  47. #endif
  48. #ifdef MY_MSCRT_DEBUG
  49. #include <crtdbg.h>
  50. #endif
  51. /*
  52. A lot of our programs uses asserts, so better to always include it
  53. This also fixes a problem when people uses DBUG_ASSERT without including
  54. assert.h
  55. */
  56. #include <assert.h>
  57. /* Include standard definitions of operator new and delete. */
  58. #ifdef __cplusplus
  59. # include <new>
  60. #endif
  61. #include "my_compiler.h"
  62. /*
  63. InnoDB depends on some MySQL internals which other plugins should not
  64. need. This is because of InnoDB's foreign key support, "safe" binlog
  65. truncation, and other similar legacy features.
  66. We define accessors for these internals unconditionally, but do not
  67. expose them in mysql/plugin.h. They are declared in ha_innodb.h for
  68. InnoDB's use.
  69. */
  70. #define INNODB_COMPATIBILITY_HOOKS
  71. /* Macros to make switching between C and C++ mode easier */
  72. #ifdef __cplusplus
  73. #define C_MODE_START extern "C" {
  74. #define C_MODE_END }
  75. #else
  76. #define C_MODE_START
  77. #define C_MODE_END
  78. #endif
  79. #ifdef WITH_PERFSCHEMA_STORAGE_ENGINE
  80. #define HAVE_PSI_INTERFACE
  81. #endif /* WITH_PERFSCHEMA_STORAGE_ENGINE */
  82. /* Make it easier to add conditional code in _expressions_ */
  83. #ifdef _WIN32
  84. #define IF_WIN(A,B) A
  85. #else
  86. #define IF_WIN(A,B) B
  87. #endif
  88. #if defined (_WIN32)
  89. /*
  90. off_t is 32 bit long. We do not use C runtime functions
  91. with off_t but native Win32 file IO APIs, that work with
  92. 64 bit offsets.
  93. */
  94. #undef SIZEOF_OFF_T
  95. #define SIZEOF_OFF_T 8
  96. static inline void sleep(unsigned long seconds)
  97. {
  98. Sleep(seconds * 1000);
  99. }
  100. /* Define missing access() modes. */
  101. #define F_OK 0
  102. #define W_OK 2
  103. #define R_OK 4 /* Test for read permission. */
  104. /* Define missing file locking constants. */
  105. #define F_RDLCK 1
  106. #define F_WRLCK 2
  107. #define F_UNLCK 3
  108. #define F_TO_EOF 0x3FFFFFFF
  109. #define O_NONBLOCK 1 /* For emulation of fcntl() */
  110. /*
  111. SHUT_RDWR is called SD_BOTH in windows and
  112. is defined to 2 in winsock2.h
  113. #define SD_BOTH 0x02
  114. */
  115. #define SHUT_RDWR 0x02
  116. /* Shared memory and named pipe connections are supported. */
  117. #define shared_memory_buffer_length 16000
  118. #define default_shared_memory_base_name "MYSQL"
  119. #endif /* _WIN32*/
  120. /**
  121. Cast a member of a structure to the structure that contains it.
  122. @param ptr Pointer to the member.
  123. @param type Type of the structure that contains the member.
  124. @param member Name of the member within the structure.
  125. */
  126. #define my_container_of(ptr, type, member) \
  127. ((type *)((char *)ptr - offsetof(type, member)))
  128. /* an assert that works at compile-time. only for constant expression */
  129. #define compile_time_assert(X) \
  130. do \
  131. { \
  132. typedef char compile_time_assert[(X) ? 1 : -1] MY_ATTRIBUTE((unused)); \
  133. } while(0)
  134. #define QUOTE_ARG(x) #x /* Quote argument (before cpp) */
  135. #define STRINGIFY_ARG(x) QUOTE_ARG(x) /* Quote argument, after cpp */
  136. #ifdef _WIN32
  137. #define SO_EXT ".dll"
  138. #elif defined(__APPLE__)
  139. #define SO_EXT ".dylib"
  140. #else
  141. #define SO_EXT ".so"
  142. #endif
  143. #if !defined(HAVE_UINT)
  144. typedef unsigned int uint;
  145. typedef unsigned short ushort;
  146. #endif
  147. #define swap_variables(t, a, b) { t dummy; dummy= a; a= b; b= dummy; }
  148. #define MY_TEST(a) ((a) ? 1 : 0)
  149. #define set_if_bigger(a,b) do { if ((a) < (b)) (a)=(b); } while(0)
  150. #define set_if_smaller(a,b) do { if ((a) > (b)) (a)=(b); } while(0)
  151. #define test_all_bits(a,b) (((a) & (b)) == (b))
  152. #define array_elements(A) ((uint) (sizeof(A)/sizeof(A[0])))
  153. /* Define some general constants */
  154. #ifndef TRUE
  155. #define TRUE (1) /* Logical true */
  156. #define FALSE (0) /* Logical false */
  157. #endif
  158. /* Some types that is different between systems */
  159. typedef int File; /* File descriptor */
  160. #ifdef _WIN32
  161. typedef SOCKET my_socket;
  162. #else
  163. typedef int my_socket; /* File descriptor for sockets */
  164. #define INVALID_SOCKET -1
  165. #endif
  166. C_MODE_START
  167. typedef void (*sig_return)();/* Returns type from signal */
  168. C_MODE_END
  169. #if defined(__GNUC__)
  170. typedef char pchar; /* Mixed prototypes can take char */
  171. typedef char pbool; /* Mixed prototypes can take char */
  172. #else
  173. typedef int pchar; /* Mixed prototypes can't take char */
  174. typedef int pbool; /* Mixed prototypes can't take char */
  175. #endif
  176. C_MODE_START
  177. typedef int (*qsort_cmp)(const void *,const void *);
  178. typedef int (*qsort_cmp2)(const void*, const void *,const void *);
  179. C_MODE_END
  180. #ifdef _WIN32
  181. typedef int socket_len_t;
  182. typedef int sigset_t;
  183. typedef int mode_t;
  184. typedef SSIZE_T ssize_t;
  185. #else
  186. typedef socklen_t socket_len_t;
  187. #endif
  188. typedef socket_len_t SOCKET_SIZE_TYPE; /* Used by NDB */
  189. /* file create flags */
  190. #ifndef O_SHARE /* Probably not windows */
  191. #define O_SHARE 0 /* Flag to my_open for shared files */
  192. #ifndef O_BINARY
  193. #define O_BINARY 0 /* Flag to my_open for binary files */
  194. #endif
  195. #ifndef FILE_BINARY
  196. #define FILE_BINARY O_BINARY /* Flag to my_fopen for binary streams */
  197. #endif
  198. #ifdef HAVE_FCNTL
  199. #define HAVE_FCNTL_LOCK
  200. #define F_TO_EOF 0L /* Param to lockf() to lock rest of file */
  201. #endif
  202. #endif /* O_SHARE */
  203. #ifndef O_TEMPORARY
  204. #define O_TEMPORARY 0
  205. #endif
  206. #ifndef O_SHORT_LIVED
  207. #define O_SHORT_LIVED 0
  208. #endif
  209. #ifndef O_NOFOLLOW
  210. #define O_NOFOLLOW 0
  211. #endif
  212. /* additional file share flags for win32 */
  213. #ifdef _WIN32
  214. #define _SH_DENYRWD 0x110 /* deny read/write mode & delete */
  215. #define _SH_DENYWRD 0x120 /* deny write mode & delete */
  216. #define _SH_DENYRDD 0x130 /* deny read mode & delete */
  217. #define _SH_DENYDEL 0x140 /* deny delete only */
  218. #endif /* _WIN32 */
  219. /* General constants */
  220. #define FN_LEN 256 /* Max file name len */
  221. #define FN_HEADLEN 253 /* Max length of filepart of file name */
  222. #define FN_EXTLEN 20 /* Max length of extension (part of FN_LEN) */
  223. #define FN_REFLEN 512 /* Max length of full path-name */
  224. #define FN_REFLEN_SE 4000 /* Max length of full path-name in SE */
  225. #define FN_EXTCHAR '.'
  226. #define FN_HOMELIB '~' /* ~/ is used as abbrev for home dir */
  227. #define FN_CURLIB '.' /* ./ is used as abbrev for current dir */
  228. #define FN_PARENTDIR ".." /* Parent directory; Must be a string */
  229. #ifdef _WIN32
  230. #define FN_LIBCHAR '\\'
  231. #define FN_LIBCHAR2 '/'
  232. #define FN_DIRSEP "/\\" /* Valid directory separators */
  233. #define FN_EXEEXT ".exe"
  234. #define FN_SOEXT ".dll"
  235. #define FN_ROOTDIR "\\"
  236. #define FN_DEVCHAR ':'
  237. #define FN_NETWORK_DRIVES /* Uses \\ to indicate network drives */
  238. #else
  239. #define FN_LIBCHAR '/'
  240. /*
  241. FN_LIBCHAR2 is not defined on !Windows. Use is_directory_separator().
  242. */
  243. #define FN_DIRSEP "/" /* Valid directory separators */
  244. #define FN_EXEEXT ""
  245. #define FN_SOEXT ".so"
  246. #define FN_ROOTDIR "/"
  247. #endif
  248. static inline int is_directory_separator(char c)
  249. {
  250. #ifdef _WIN32
  251. return c == FN_LIBCHAR || c == FN_LIBCHAR2;
  252. #else
  253. return c == FN_LIBCHAR;
  254. #endif
  255. }
  256. /*
  257. MY_FILE_MIN is Windows speciality and is used to quickly detect
  258. the mismatch of CRT and mysys file IO usage on Windows at runtime.
  259. CRT file descriptors can be in the range 0-2047, whereas descriptors returned
  260. by my_open() will start with 2048. If a file descriptor with value less then
  261. MY_FILE_MIN is passed to mysys IO function, chances are it stemms from
  262. open()/fileno() and not my_open()/my_fileno.
  263. For Posix, mysys functions are light wrappers around libc, and MY_FILE_MIN
  264. is logically 0.
  265. */
  266. #ifdef _WIN32
  267. #define MY_FILE_MIN 2048
  268. #else
  269. #define MY_FILE_MIN 0
  270. #endif
  271. /*
  272. MY_NFILE is the default size of my_file_info array.
  273. It is larger on Windows, because it all file handles are stored in my_file_info
  274. Default size is 16384 and this should be enough for most cases.If it is not
  275. enough, --max-open-files with larger value can be used.
  276. For Posix , my_file_info array is only used to store filenames for
  277. error reporting and its size is not a limitation for number of open files.
  278. */
  279. #ifdef _WIN32
  280. #define MY_NFILE (16384 + MY_FILE_MIN)
  281. #else
  282. #define MY_NFILE 64
  283. #endif
  284. #define OS_FILE_LIMIT UINT_MAX
  285. /*
  286. Io buffer size; Must be a power of 2 and a multiple of 512. May be
  287. smaller what the disk page size. This influences the speed of the
  288. isam btree library. eg to big to slow.
  289. */
  290. #define IO_SIZE 4096
  291. /*
  292. How much overhead does malloc have. The code often allocates
  293. something like 1024-MALLOC_OVERHEAD bytes
  294. */
  295. #define MALLOC_OVERHEAD 8
  296. /* get memory in huncs */
  297. #define ONCE_ALLOC_INIT (uint) (4096-MALLOC_OVERHEAD)
  298. /* Typical record cash */
  299. #define RECORD_CACHE_SIZE (uint) (64*1024-MALLOC_OVERHEAD)
  300. /* Typical key cash */
  301. #define KEY_CACHE_SIZE (uint) (8*1024*1024)
  302. /* Default size of a key cache block */
  303. #define KEY_CACHE_BLOCK_SIZE (uint) 1024
  304. /* Some defines of functions for portability */
  305. #if (_WIN32)
  306. #if !defined(_WIN64)
  307. inline double my_ulonglong2double(unsigned long long value)
  308. {
  309. long long nr=(long long) value;
  310. if (nr >= 0)
  311. return (double) nr;
  312. return (18446744073709551616.0 + (double) nr);
  313. }
  314. #define ulonglong2double my_ulonglong2double
  315. #define my_off_t2double my_ulonglong2double
  316. #endif /* _WIN64 */
  317. inline unsigned long long my_double2ulonglong(double d)
  318. {
  319. double t= d - (double) 0x8000000000000000ULL;
  320. if (t >= 0)
  321. return ((unsigned long long) t) + 0x8000000000000000ULL;
  322. return (unsigned long long) d;
  323. }
  324. #define double2ulonglong my_double2ulonglong
  325. #endif /* _WIN32 */
  326. #ifndef ulonglong2double
  327. #define ulonglong2double(A) ((double) (ulonglong) (A))
  328. #define my_off_t2double(A) ((double) (my_off_t) (A))
  329. #endif
  330. #ifndef double2ulonglong
  331. #define double2ulonglong(A) ((ulonglong) (double) (A))
  332. #endif
  333. #define INT_MIN64 (~0x7FFFFFFFFFFFFFFFLL)
  334. #define INT_MAX64 0x7FFFFFFFFFFFFFFFLL
  335. #define INT_MIN32 (~0x7FFFFFFFL)
  336. #define INT_MAX32 0x7FFFFFFFL
  337. #define UINT_MAX32 0xFFFFFFFFL
  338. #define INT_MIN24 (~0x007FFFFF)
  339. #define INT_MAX24 0x007FFFFF
  340. #define UINT_MAX24 0x00FFFFFF
  341. #define INT_MIN16 (~0x7FFF)
  342. #define INT_MAX16 0x7FFF
  343. #define UINT_MAX16 0xFFFF
  344. #define INT_MIN8 (~0x7F)
  345. #define INT_MAX8 0x7F
  346. #define UINT_MAX8 0xFF
  347. #ifndef SIZE_T_MAX
  348. #define SIZE_T_MAX (~((size_t) 0))
  349. #endif
  350. // Our ifdef trickery for my_isfinite does not work with gcc/solaris unless we:
  351. #ifdef HAVE_IEEEFP_H
  352. #include <ieeefp.h>
  353. #endif
  354. #if (__cplusplus >= 201103L)
  355. /* For C++11 use the new std functions rather than C99 macros. */
  356. #include <cmath>
  357. #define my_isfinite(X) std::isfinite(X)
  358. #define my_isnan(X) std::isnan(X)
  359. #define my_isinf(X) std::isinf(X)
  360. #else
  361. #ifdef HAVE_LLVM_LIBCPP /* finite is deprecated in libc++ */
  362. #define my_isfinite(X) isfinite(X)
  363. #elif defined _WIN32
  364. #define my_isfinite(X) _finite(X)
  365. #else
  366. #define my_isfinite(X) finite(X)
  367. #endif
  368. #define my_isnan(X) isnan(X)
  369. #ifdef HAVE_ISINF
  370. /* System-provided isinf() is available and safe to use */
  371. #define my_isinf(X) isinf(X)
  372. #else /* !HAVE_ISINF */
  373. #define my_isinf(X) (!my_isfinite(X) && !my_isnan(X))
  374. #endif
  375. #endif /* __cplusplus >= 201103L */
  376. /*
  377. Max size that must be added to a so that we know Size to make
  378. adressable obj.
  379. */
  380. #if SIZEOF_CHARP == 4
  381. typedef long my_ptrdiff_t;
  382. #else
  383. typedef long long my_ptrdiff_t;
  384. #endif
  385. #define MY_ALIGN(A,L) (((A) + (L) - 1) & ~((L) - 1))
  386. #define ALIGN_SIZE(A) MY_ALIGN((A),sizeof(double))
  387. /* Size to make adressable obj. */
  388. #define ADD_TO_PTR(ptr,size,type) (type) ((uchar*) (ptr)+size)
  389. #define PTR_BYTE_DIFF(A,B) (my_ptrdiff_t) ((uchar*) (A) - (uchar*) (B))
  390. /*
  391. Custom version of standard offsetof() macro which can be used to get
  392. offsets of members in class for non-POD types (according to the current
  393. version of C++ standard offsetof() macro can't be used in such cases and
  394. attempt to do so causes warnings to be emitted, OTOH in many cases it is
  395. still OK to assume that all instances of the class has the same offsets
  396. for the same members).
  397. This is temporary solution which should be removed once File_parser class
  398. and related routines are refactored.
  399. */
  400. #define my_offsetof(TYPE, MEMBER) \
  401. ((size_t)((char *)&(((TYPE *)0x10)->MEMBER) - (char*)0x10))
  402. #define NullS (char *) 0
  403. #ifdef _WIN32
  404. #define STDCALL __stdcall
  405. #else
  406. #define STDCALL
  407. #endif
  408. /* Typdefs for easyier portability */
  409. typedef unsigned char uchar; /* Short for unsigned char */
  410. typedef signed char int8; /* Signed integer >= 8 bits */
  411. typedef unsigned char uint8; /* Unsigned integer >= 8 bits */
  412. typedef short int16;
  413. typedef unsigned short uint16;
  414. #if SIZEOF_INT == 4
  415. typedef int int32;
  416. typedef unsigned int uint32;
  417. #elif SIZEOF_LONG == 4
  418. typedef long int32;
  419. typedef unsigned long uint32;
  420. #else
  421. #error Neither int or long is of 4 bytes width
  422. #endif
  423. #if !defined(HAVE_ULONG)
  424. typedef unsigned long ulong; /* Short for unsigned long */
  425. #endif
  426. /*
  427. Using [unsigned] long long is preferable as [u]longlong because we use
  428. [unsigned] long long unconditionally in many places,
  429. for example in constants with [U]LL suffix.
  430. */
  431. typedef unsigned long long int ulonglong; /* ulong or unsigned long long */
  432. typedef long long int longlong;
  433. typedef longlong int64;
  434. typedef ulonglong uint64;
  435. #if defined (_WIN32)
  436. typedef unsigned __int64 my_ulonglong;
  437. #else
  438. typedef unsigned long long my_ulonglong;
  439. #endif
  440. #if SIZEOF_CHARP == SIZEOF_INT
  441. typedef int intptr;
  442. #elif SIZEOF_CHARP == SIZEOF_LONG
  443. typedef long intptr;
  444. #elif SIZEOF_CHARP == SIZEOF_LONG_LONG
  445. typedef long long intptr;
  446. #else
  447. #error sizeof(void *) is neither sizeof(int) nor sizeof(long) nor sizeof(long long)
  448. #endif
  449. #define MY_ERRPTR ((void*)(intptr)1)
  450. #if defined(_WIN32)
  451. typedef unsigned long long my_off_t;
  452. typedef unsigned long long os_off_t;
  453. #else
  454. typedef off_t os_off_t;
  455. #if SIZEOF_OFF_T > 4
  456. typedef ulonglong my_off_t;
  457. #else
  458. typedef unsigned long my_off_t;
  459. #endif
  460. #endif /*_WIN32*/
  461. #define MY_FILEPOS_ERROR (~(my_off_t) 0)
  462. /*
  463. TODO Convert these to use Bitmap class.
  464. */
  465. typedef ulonglong table_map; /* Used for table bits in join */
  466. typedef ulonglong nesting_map; /* Used for flags of nesting constructs */
  467. #if defined(_WIN32)
  468. #define socket_errno WSAGetLastError()
  469. #define SOCKET_EINTR WSAEINTR
  470. #define SOCKET_EAGAIN WSAEINPROGRESS
  471. #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
  472. #define SOCKET_EADDRINUSE WSAEADDRINUSE
  473. #define SOCKET_ETIMEDOUT WSAETIMEDOUT
  474. #define SOCKET_ECONNRESET WSAECONNRESET
  475. #define SOCKET_ENFILE ENFILE
  476. #define SOCKET_EMFILE EMFILE
  477. #else /* Unix */
  478. #define socket_errno errno
  479. #define closesocket(A) close(A)
  480. #define SOCKET_EINTR EINTR
  481. #define SOCKET_EAGAIN EAGAIN
  482. #define SOCKET_EWOULDBLOCK EWOULDBLOCK
  483. #define SOCKET_EADDRINUSE EADDRINUSE
  484. #define SOCKET_ETIMEDOUT ETIMEDOUT
  485. #define SOCKET_ECONNRESET ECONNRESET
  486. #define SOCKET_ENFILE ENFILE
  487. #define SOCKET_EMFILE EMFILE
  488. #endif
  489. typedef int myf; /* Type of MyFlags in my_funcs */
  490. typedef char my_bool; /* Small bool */
  491. /* Macros for converting *constants* to the right type */
  492. #define MYF(v) (myf) (v)
  493. /* Some helper macros */
  494. #define YESNO(X) ((X) ? "yes" : "no")
  495. #define MY_HOW_OFTEN_TO_WRITE 1000 /* How often we want info on screen */
  496. #include <my_byteorder.h>
  497. #ifdef HAVE_CHARSET_utf8
  498. #define MYSQL_UNIVERSAL_CLIENT_CHARSET "utf8"
  499. #else
  500. #define MYSQL_UNIVERSAL_CLIENT_CHARSET MYSQL_DEFAULT_CHARSET_NAME
  501. #endif
  502. #if defined(_WIN32)
  503. #define dlsym(lib, name) (void*)GetProcAddress((HMODULE)lib, name)
  504. #define dlopen(libname, unused) LoadLibraryEx(libname, NULL, 0)
  505. #define dlclose(lib) FreeLibrary((HMODULE)lib)
  506. #ifndef HAVE_DLOPEN
  507. #define HAVE_DLOPEN
  508. #endif
  509. #define DLERROR_GENERATE(errmsg, error_number) \
  510. char win_errormsg[2048]; \
  511. if(FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, \
  512. 0, error_number, 0, win_errormsg, 2048, NULL)) \
  513. { \
  514. char *ptr; \
  515. for (ptr= &win_errormsg[0] + strlen(win_errormsg) - 1; \
  516. ptr >= &win_errormsg[0] && strchr("\r\n\t\0x20", *ptr); \
  517. ptr--) \
  518. *ptr= 0; \
  519. errmsg= win_errormsg; \
  520. } \
  521. else \
  522. errmsg= ""
  523. #define dlerror() ""
  524. #define dlopen_errno GetLastError()
  525. #else /* _WIN32 */
  526. #define DLERROR_GENERATE(errmsg, error_number) errmsg= dlerror()
  527. #define dlopen_errno errno
  528. #endif /* _WIN32 */
  529. /* Length of decimal number represented by INT32. */
  530. #define MY_INT32_NUM_DECIMAL_DIGITS 11U
  531. /* Length of decimal number represented by INT64. */
  532. #define MY_INT64_NUM_DECIMAL_DIGITS 21U
  533. /* Define some useful general macros (should be done after all headers). */
  534. #define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
  535. #define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
  536. #if !defined(__cplusplus) && !defined(bool)
  537. #define bool In_C_you_should_use_my_bool_instead()
  538. #endif
  539. /*
  540. MYSQL_PLUGIN_IMPORT macro is used to export mysqld data
  541. (i.e variables) for usage in storage engine loadable plugins.
  542. Outside of Windows, it is dummy.
  543. */
  544. #if (defined(_WIN32) && defined(MYSQL_DYNAMIC_PLUGIN))
  545. #define MYSQL_PLUGIN_IMPORT __declspec(dllimport)
  546. #else
  547. #define MYSQL_PLUGIN_IMPORT
  548. #endif
  549. #include <my_dbug.h>
  550. #ifdef EMBEDDED_LIBRARY
  551. #define NO_EMBEDDED_ACCESS_CHECKS
  552. /* Things we don't need in the embedded version of MySQL */
  553. #undef HAVE_OPENSSL
  554. #endif /* EMBEDDED_LIBRARY */
  555. enum loglevel {
  556. ERROR_LEVEL= 0,
  557. WARNING_LEVEL= 1,
  558. INFORMATION_LEVEL= 2
  559. };
  560. #ifdef _WIN32
  561. /****************************************************************************
  562. ** Replacements for localtime_r and gmtime_r
  563. ****************************************************************************/
  564. static inline struct tm *localtime_r(const time_t *timep, struct tm *tmp)
  565. {
  566. localtime_s(tmp, timep);
  567. return tmp;
  568. }
  569. static inline struct tm *gmtime_r(const time_t *clock, struct tm *res)
  570. {
  571. gmtime_s(res, clock);
  572. return res;
  573. }
  574. #endif /* _WIN32 */
  575. #ifndef HAVE_STRUCT_TIMESPEC /* Windows before VS2015 */
  576. /*
  577. Declare a union to make sure FILETIME is properly aligned
  578. so it can be used directly as a 64 bit value. The value
  579. stored is in 100ns units.
  580. */
  581. union ft64 {
  582. FILETIME ft;
  583. __int64 i64;
  584. };
  585. struct timespec {
  586. union ft64 tv;
  587. /* The max timeout value in millisecond for native_cond_timedwait */
  588. long max_timeout_msec;
  589. };
  590. #endif /* !HAVE_STRUCT_TIMESPEC */
  591. C_MODE_START
  592. extern ulonglong my_getsystime(void);
  593. C_MODE_END
  594. static inline void set_timespec_nsec(struct timespec *abstime, ulonglong nsec)
  595. {
  596. #ifdef HAVE_STRUCT_TIMESPEC
  597. ulonglong now= my_getsystime() + (nsec / 100);
  598. ulonglong tv_sec= now / 10000000ULL;
  599. #if SIZEOF_TIME_T < SIZEOF_LONG_LONG
  600. /* Ensure that the number of seconds don't overflow. */
  601. tv_sec= MY_MIN(tv_sec, ((ulonglong)INT_MAX32));
  602. #endif
  603. abstime->tv_sec= (time_t)tv_sec;
  604. abstime->tv_nsec= (now % 10000000ULL) * 100 + (nsec % 100);
  605. #else /* !HAVE_STRUCT_TIMESPEC */
  606. ulonglong max_timeout_msec= (nsec / 1000000);
  607. union ft64 tv;
  608. GetSystemTimeAsFileTime(&tv.ft);
  609. abstime->tv.i64= tv.i64 + (__int64)(nsec / 100);
  610. #if SIZEOF_LONG < SIZEOF_LONG_LONG
  611. /* Ensure that the msec value doesn't overflow. */
  612. max_timeout_msec= MY_MIN(max_timeout_msec, ((ulonglong)INT_MAX32));
  613. #endif
  614. abstime->max_timeout_msec= (long)max_timeout_msec;
  615. #endif /* !HAVE_STRUCT_TIMESPEC */
  616. }
  617. static inline void set_timespec(struct timespec *abstime, ulonglong sec)
  618. {
  619. set_timespec_nsec(abstime, sec * 1000000000ULL);
  620. }
  621. /**
  622. Compare two timespec structs.
  623. @retval 1 If ts1 ends after ts2.
  624. @retval -1 If ts1 ends before ts2.
  625. @retval 0 If ts1 is equal to ts2.
  626. */
  627. static inline int cmp_timespec(struct timespec *ts1, struct timespec *ts2)
  628. {
  629. #ifdef HAVE_STRUCT_TIMESPEC
  630. if (ts1->tv_sec > ts2->tv_sec ||
  631. (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec > ts2->tv_nsec))
  632. return 1;
  633. if (ts1->tv_sec < ts2->tv_sec ||
  634. (ts1->tv_sec == ts2->tv_sec && ts1->tv_nsec < ts2->tv_nsec))
  635. return -1;
  636. #else
  637. if (ts1->tv.i64 > ts2->tv.i64)
  638. return 1;
  639. if (ts1->tv.i64 < ts2->tv.i64)
  640. return -1;
  641. #endif
  642. return 0;
  643. }
  644. static inline ulonglong diff_timespec(struct timespec *ts1, struct timespec *ts2)
  645. {
  646. #ifdef HAVE_STRUCT_TIMESPEC
  647. return (ts1->tv_sec - ts2->tv_sec) * 1000000000ULL +
  648. ts1->tv_nsec - ts2->tv_nsec;
  649. #else
  650. return (ts1->tv.i64 - ts2->tv.i64) * 100;
  651. #endif
  652. }
  653. #ifdef _WIN32
  654. typedef int MY_MODE;
  655. #else
  656. typedef mode_t MY_MODE;
  657. #endif /* _WIN32 */
  658. /* File permissions */
  659. #define USER_READ (1L << 0)
  660. #define USER_WRITE (1L << 1)
  661. #define USER_EXECUTE (1L << 2)
  662. #define GROUP_READ (1L << 3)
  663. #define GROUP_WRITE (1L << 4)
  664. #define GROUP_EXECUTE (1L << 5)
  665. #define OTHERS_READ (1L << 6)
  666. #define OTHERS_WRITE (1L << 7)
  667. #define OTHERS_EXECUTE (1L << 8)
  668. #define USER_RWX USER_READ | USER_WRITE | USER_EXECUTE
  669. #define GROUP_RWX GROUP_READ | GROUP_WRITE | GROUP_EXECUTE
  670. #define OTHERS_RWX OTHERS_READ | OTHERS_WRITE | OTHERS_EXECUTE
  671. /* Defaults */
  672. #define DEFAULT_SSL_CA_CERT "ca.pem"
  673. #define DEFAULT_SSL_CA_KEY "ca-key.pem"
  674. #define DEFAULT_SSL_SERVER_CERT "server-cert.pem"
  675. #define DEFAULT_SSL_SERVER_KEY "server-key.pem"
  676. #if defined(_WIN32) || defined(_WIN64)
  677. #define strcasecmp _stricmp
  678. #endif
  679. #endif // MY_GLOBAL_INCLUDED