MySQL  8.0.18
Source Code Documentation
my_compiler.h
Go to the documentation of this file.
1 #ifndef MY_COMPILER_INCLUDED
2 #define MY_COMPILER_INCLUDED
3 
4 /* Copyright (c) 2010, 2019, Oracle and/or its affiliates. All rights reserved.
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
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 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /**
27  @file include/my_compiler.h
28  Header for compiler-dependent features.
29 
30  Intended to contain a set of reusable wrappers for preprocessor
31  macros, attributes, pragmas, and any other features that are
32  specific to a target compiler.
33 */
34 
35 #ifndef MYSQL_ABI_CHECK
36 #include <assert.h>
37 #include <stddef.h> /* size_t */
38 #endif
39 
40 #include "my_config.h"
41 
42 /*
43  The macros below are borrowed from include/linux/compiler.h in the
44  Linux kernel. Use them to indicate the likelyhood of the truthfulness
45  of a condition. This serves two purposes - newer versions of gcc will be
46  able to optimize for branch predication, which could yield siginficant
47  performance gains in frequently executed sections of the code, and the
48  other reason to use them is for documentation
49 */
50 #ifdef HAVE_BUILTIN_EXPECT
51 
52 // likely/unlikely are likely to clash with other symbols, do not #define
53 #if defined(__cplusplus)
54 inline bool likely(bool expr) { return __builtin_expect(expr, true); }
55 inline bool unlikely(bool expr) { return __builtin_expect(expr, false); }
56 #else
57 #define likely(x) __builtin_expect((x), 1)
58 #define unlikely(x) __builtin_expect((x), 0)
59 #endif
60 
61 #else /* HAVE_BUILTIN_EXPECT */
62 
63 #if defined(__cplusplus)
64 inline bool likely(bool expr) { return expr; }
65 inline bool unlikely(bool expr) { return expr; }
66 #else
67 #define likely(x) (x)
68 #define unlikely(x) (x)
69 #endif
70 
71 #endif /* HAVE_BUILTIN_EXPECT */
72 
73 /* Comunicate to the compiler the unreachability of the code. */
74 #ifdef HAVE_BUILTIN_UNREACHABLE
75 #define MY_ASSERT_UNREACHABLE() __builtin_unreachable()
76 #else
77 #define MY_ASSERT_UNREACHABLE() \
78  do { \
79  assert(0); \
80  } while (0)
81 #endif
82 
83 /* Visual Studio requires '__inline' for C code */
84 #if !defined(__cplusplus) && defined(_MSC_VER)
85 #define inline __inline
86 #endif
87 
88 /* Provide __func__ macro definition for Visual Studio. */
89 #if defined(_MSC_VER)
90 #define __func__ __FUNCTION__
91 #endif
92 
93 /*
94  Disable MY_ATTRIBUTE for Sun Studio and Visual Studio.
95  Note that Sun Studio supports some __attribute__ variants,
96  but not format or unused which we use quite a lot.
97 */
98 #ifndef MY_ATTRIBUTE
99 #if defined(__GNUC__) || defined(__clang__)
100 #define MY_ATTRIBUTE(A) __attribute__(A)
101 #else
102 #define MY_ATTRIBUTE(A)
103 #endif
104 #endif
105 
106 #if defined(_MSC_VER)
107 #define ALWAYS_INLINE __forceinline
108 #else
109 #define ALWAYS_INLINE __attribute__((always_inline)) inline
110 #endif
111 
112 #if defined(_MSC_VER)
113 #define NO_INLINE __declspec(noinline)
114 #else
115 #define NO_INLINE __attribute__((noinline))
116 #endif
117 
118 #ifndef __has_attribute
119 #define __has_attribute(x) 0
120 #endif
121 
122 #ifndef SUPPRESS_UBSAN
123 // clang -fsanitize=undefined
124 #if defined(HAVE_UBSAN) && defined(__clang__)
125 #define SUPPRESS_UBSAN MY_ATTRIBUTE((no_sanitize("undefined")))
126 // gcc -fsanitize=undefined
127 #elif defined(HAVE_UBSAN) && __has_attribute(no_sanitize_undefined)
128 #define SUPPRESS_UBSAN MY_ATTRIBUTE((no_sanitize_undefined))
129 #else
130 #define SUPPRESS_UBSAN
131 #endif
132 #endif /* SUPPRESS_UBSAN */
133 
134 #ifndef SUPPRESS_TSAN
135 #if defined(HAVE_TSAN) && defined(__clang__)
136 #define SUPPRESS_TSAN MY_ATTRIBUTE((no_sanitize("thread")))
137 #elif defined(HAVE_TSAN) && __has_attribute(no_sanitize_thread)
138 #define SUPPRESS_TSAN MY_ATTRIBUTE((no_sanitize_thread))
139 #else
140 #define SUPPRESS_TSAN
141 #endif
142 #endif /* SUPPRESS_TSAN */
143 
144 #ifdef _WIN32
145 #define STDCALL __stdcall
146 #else
147 #define STDCALL
148 #endif
149 
150 #endif /* MY_COMPILER_INCLUDED */
#define __builtin_expect(x, expected_value)
Definition: memcached.h:496
bool unlikely(bool expr)
Definition: my_compiler.h:65
bool likely(bool expr)
Definition: my_compiler.h:64