my_compiler.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. #ifndef MY_COMPILER_INCLUDED
  2. #define MY_COMPILER_INCLUDED
  3. /* Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License, version 2.0,
  6. as published by the Free Software Foundation.
  7. This program is also distributed with certain software (including
  8. but not limited to OpenSSL) that is licensed under separate terms,
  9. as designated in a particular file or component or in included license
  10. documentation. The authors of MySQL hereby grant you an additional
  11. permission to link the program and your derivative works with the
  12. separately licensed software that they have included with MySQL.
  13. Without limiting anything contained in the foregoing, this file,
  14. which is part of C Driver for MySQL (Connector/C), is also subject to the
  15. Universal FOSS Exception, version 1.0, a copy of which can be found at
  16. http://oss.oracle.com/licenses/universal-foss-exception.
  17. This program is distributed in the hope that it will be useful,
  18. but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. GNU General Public License, version 2.0, for more details.
  21. You should have received a copy of the GNU General Public License
  22. along with this program; if not, write to the Free Software
  23. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  24. /**
  25. Header for compiler-dependent features.
  26. Intended to contain a set of reusable wrappers for preprocessor
  27. macros, attributes, pragmas, and any other features that are
  28. specific to a target compiler.
  29. */
  30. #include <stddef.h> /* size_t */
  31. #if defined __GNUC__
  32. /*
  33. Convenience macro to test the minimum required GCC version.
  34. These should be used with care as Clang also sets __GNUC__ and
  35. __GNUC_MINOR__ (currently to 4.2). Prefer using feature specific
  36. CMake checks in configure.cmake instead.
  37. */
  38. # define MY_GNUC_PREREQ(maj, min) \
  39. ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
  40. # define GCC_VERSION (__GNUC__ * 1000 + __GNUC_MINOR__)
  41. #else
  42. # define MY_GNUC_PREREQ(maj, min) (0)
  43. #endif
  44. /*
  45. The macros below are borrowed from include/linux/compiler.h in the
  46. Linux kernel. Use them to indicate the likelyhood of the truthfulness
  47. of a condition. This serves two purposes - newer versions of gcc will be
  48. able to optimize for branch predication, which could yield siginficant
  49. performance gains in frequently executed sections of the code, and the
  50. other reason to use them is for documentation
  51. */
  52. #ifdef HAVE_BUILTIN_EXPECT
  53. // likely/unlikely are likely to clash with other symbols, do not #define
  54. #if defined(__cplusplus)
  55. inline bool likely(bool expr)
  56. {
  57. return __builtin_expect(expr, true);
  58. }
  59. inline bool unlikely(bool expr)
  60. {
  61. return __builtin_expect(expr, false);
  62. }
  63. #else
  64. # define likely(x) __builtin_expect((x),1)
  65. # define unlikely(x) __builtin_expect((x),0)
  66. #endif
  67. #else /* HAVE_BUILTIN_EXPECT */
  68. #if defined(__cplusplus)
  69. inline bool likely(bool expr)
  70. {
  71. return expr;
  72. }
  73. inline bool unlikely(bool expr)
  74. {
  75. return expr;
  76. }
  77. #else
  78. # define likely(x) (x)
  79. # define unlikely(x) (x)
  80. #endif
  81. #endif /* HAVE_BUILTIN_EXPECT */
  82. /* Comunicate to the compiler the unreachability of the code. */
  83. #ifdef HAVE_BUILTIN_UNREACHABLE
  84. # define MY_ASSERT_UNREACHABLE() __builtin_unreachable()
  85. #else
  86. # define MY_ASSERT_UNREACHABLE() do { assert(0); } while (0)
  87. #endif
  88. #if defined __GNUC__ || defined __SUNPRO_C || defined __SUNPRO_CC
  89. /* Specifies the minimum alignment of a type. */
  90. # define MY_ALIGNOF(type) __alignof__(type)
  91. /* Determine the alignment requirement of a type. */
  92. # define MY_ALIGNED(n) __attribute__((__aligned__((n))))
  93. /* Microsoft Visual C++ */
  94. #elif defined _MSC_VER
  95. # define MY_ALIGNOF(type) __alignof(type)
  96. # define MY_ALIGNED(n) __declspec(align(n))
  97. #else /* Make sure they are defined for other compilers. */
  98. # define MY_ALIGNOF(type)
  99. # define MY_ALIGNED(size)
  100. #endif
  101. /* Visual Studio requires '__inline' for C code */
  102. #if !defined(__cplusplus) && defined(_MSC_VER)
  103. # define inline __inline
  104. #endif
  105. /* Provide __func__ macro definition for Visual Studio. */
  106. #if defined(_MSC_VER)
  107. # define __func__ __FUNCTION__
  108. #endif
  109. /**
  110. C++ Type Traits
  111. */
  112. #ifdef __cplusplus
  113. /**
  114. Opaque storage with a particular alignment.
  115. Partial specialization used due to MSVC++.
  116. */
  117. template<size_t alignment> struct my_alignment_imp;
  118. template<> struct MY_ALIGNED(1) my_alignment_imp<1> {};
  119. template<> struct MY_ALIGNED(2) my_alignment_imp<2> {};
  120. template<> struct MY_ALIGNED(4) my_alignment_imp<4> {};
  121. template<> struct MY_ALIGNED(8) my_alignment_imp<8> {};
  122. template<> struct MY_ALIGNED(16) my_alignment_imp<16> {};
  123. /**
  124. A POD type with a given size and alignment.
  125. @remark If the compiler does not support a alignment attribute
  126. (MY_ALIGN macro), the default alignment of a double is
  127. used instead.
  128. @tparam size The minimum size.
  129. @tparam alignment The desired alignment: 1, 2, 4, 8 or 16.
  130. */
  131. template <size_t size, size_t alignment>
  132. struct my_aligned_storage
  133. {
  134. union
  135. {
  136. char data[size];
  137. my_alignment_imp<alignment> align;
  138. };
  139. };
  140. #endif /* __cplusplus */
  141. /*
  142. Disable MY_ATTRIBUTE for Sun Studio and Visual Studio.
  143. Note that Sun Studio supports some __attribute__ variants,
  144. but not format or unused which we use quite a lot.
  145. */
  146. #ifndef MY_ATTRIBUTE
  147. #if defined(__GNUC__)
  148. # define MY_ATTRIBUTE(A) __attribute__(A)
  149. #else
  150. # define MY_ATTRIBUTE(A)
  151. #endif
  152. #endif
  153. #ifndef __has_attribute
  154. # define __has_attribute(x) 0
  155. #endif
  156. #if __has_attribute(no_sanitize_undefined)
  157. # define SUPPRESS_UBSAN __attribute__((no_sanitize_undefined))
  158. #else
  159. # define SUPPRESS_UBSAN
  160. #endif
  161. #endif /* MY_COMPILER_INCLUDED */