my_global.h 23 KB

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