Make some AsyncTest methods virtual to allow mocking them using gtest/gmock
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2017 Facebook, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #pragma once
18
19 #include <string.h>
20
21 #include <cstddef>
22
23 #include <folly/portability/Config.h>
24
25 #include <folly/CPortability.h>
26
27 // Unaligned loads and stores
28 namespace folly {
29 #if FOLLY_HAVE_UNALIGNED_ACCESS
30 constexpr bool kHasUnalignedAccess = true;
31 #else
32 constexpr bool kHasUnalignedAccess = false;
33 #endif
34 }
35
36 // compiler specific attribute translation
37 // msvc should come first, so if clang is in msvc mode it gets the right defines
38
39 #if defined(__clang__) || defined(__GNUC__)
40 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
41 #elif defined(_MSC_VER)
42 # define FOLLY_ALIGNED(size) __declspec(align(size))
43 #else
44 # error Cannot define FOLLY_ALIGNED on this platform
45 #endif
46 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
47
48 // NOTE: this will only do checking in msvc with versions that support /analyze
49 #if _MSC_VER
50 # ifdef _USE_ATTRIBUTES_FOR_SAL
51 #    undef _USE_ATTRIBUTES_FOR_SAL
52 # endif
53 /* nolint */
54 # define _USE_ATTRIBUTES_FOR_SAL 1
55 # include <sal.h>
56 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
57 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
58 #else
59 # define FOLLY_PRINTF_FORMAT /**/
60 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
61   __attribute__((__format__(__printf__, format_param, dots_param)))
62 #endif
63
64 // deprecated
65 #if defined(__clang__) || defined(__GNUC__)
66 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
67 #elif defined(_MSC_VER)
68 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
69 #else
70 # define FOLLY_DEPRECATED(msg)
71 #endif
72
73 // warn unused result
74 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
75 #define FOLLY_WARN_UNUSED_RESULT _Check_return_
76 #elif defined(__clang__) || defined(__GNUC__)
77 #define FOLLY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
78 #else
79 #define FOLLY_WARN_UNUSED_RESULT
80 #endif
81
82 // target
83 #ifdef _MSC_VER
84 # define FOLLY_TARGET_ATTRIBUTE(target)
85 #else
86 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
87 #endif
88
89 // detection for 64 bit
90 #if defined(__x86_64__) || defined(_M_X64)
91 # define FOLLY_X64 1
92 #else
93 # define FOLLY_X64 0
94 #endif
95
96 #if defined(__aarch64__)
97 # define FOLLY_A64 1
98 #else
99 # define FOLLY_A64 0
100 #endif
101
102 #if defined (__powerpc64__)
103 # define FOLLY_PPC64 1
104 #else
105 # define FOLLY_PPC64 0
106 #endif
107
108 namespace folly {
109 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
110 constexpr bool kIsArchAArch64 = FOLLY_A64 == 1;
111 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
112 }
113
114 namespace folly {
115
116 #if FOLLY_SANITIZE_ADDRESS
117 constexpr bool kIsSanitizeAddress = true;
118 #else
119 constexpr bool kIsSanitizeAddress = false;
120 #endif
121
122 #if FOLLY_SANITIZE_THREAD
123 constexpr bool kIsSanitizeThread = true;
124 #else
125 constexpr bool kIsSanitizeThread = false;
126 #endif
127 }
128
129 // packing is very ugly in msvc
130 #ifdef _MSC_VER
131 # define FOLLY_PACK_ATTR /**/
132 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
133 # define FOLLY_PACK_POP __pragma(pack(pop))
134 #elif defined(__clang__) || defined(__GNUC__)
135 # define FOLLY_PACK_ATTR __attribute__((__packed__))
136 # define FOLLY_PACK_PUSH /**/
137 # define FOLLY_PACK_POP /**/
138 #else
139 # define FOLLY_PACK_ATTR /**/
140 # define FOLLY_PACK_PUSH /**/
141 # define FOLLY_PACK_POP /**/
142 #endif
143
144 // Generalize warning push/pop.
145 #if defined(_MSC_VER)
146 # define FOLLY_PUSH_WARNING __pragma(warning(push))
147 # define FOLLY_POP_WARNING __pragma(warning(pop))
148 // Disable the GCC warnings.
149 # define FOLLY_GCC_DISABLE_WARNING(warningName)
150 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
151 #elif defined(__clang__) || defined(__GNUC__)
152 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
153 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
154 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
155 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
156   FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
157 #define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
158   _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
159           FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
160 // Disable the MSVC warnings.
161 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
162 #else
163 # define FOLLY_PUSH_WARNING
164 # define FOLLY_POP_WARNING
165 # define FOLLY_GCC_DISABLE_WARNING(warningName)
166 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
167 #endif
168
169 #ifdef HAVE_SHADOW_LOCAL_WARNINGS
170 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS        \
171   FOLLY_GCC_DISABLE_WARNING(shadow-compatible-local) \
172   FOLLY_GCC_DISABLE_WARNING(shadow-local)
173 #else
174 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
175 #endif
176
177 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
178 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
179 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
180 // Alias it into std (where it's found in 4.9 and later)
181 namespace std { typedef ::max_align_t max_align_t; }
182 #endif
183
184 // portable version check for clang
185 #ifndef __CLANG_PREREQ
186 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
187 /* nolint */
188 #  define __CLANG_PREREQ(maj, min) \
189     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
190 # else
191 /* nolint */
192 #  define __CLANG_PREREQ(maj, min) 0
193 # endif
194 #endif
195
196 /* Platform specific TLS support
197  * gcc implements __thread
198  * msvc implements __declspec(thread)
199  * the semantics are the same
200  * (but remember __thread has different semantics when using emutls (ex. apple))
201  */
202 #if defined(_MSC_VER)
203 # define FOLLY_TLS __declspec(thread)
204 #elif defined(__GNUC__) || defined(__clang__)
205 # define FOLLY_TLS __thread
206 #else
207 # error cannot define platform specific thread local storage
208 #endif
209
210 #if FOLLY_MOBILE
211 #undef FOLLY_TLS
212 #endif
213
214 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
215 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
216 // up in a macro to make forward-declarations easier.
217 #if FOLLY_USE_LIBCPP
218 #include <__config>
219 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
220 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
221 #else
222 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
223 #define FOLLY_NAMESPACE_STD_END       }
224 #endif
225
226 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
227 // some types, e.g. std::list.
228 #if _GLIBCXX_USE_CXX11_ABI
229 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
230   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
231 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
232 #else
233 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
234 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
235 #endif
236
237 // MSVC specific defines
238 // mainly for posix compat
239 #ifdef _MSC_VER
240 #include <folly/portability/SysTypes.h>
241
242 // compiler specific to compiler specific
243 // nolint
244 # define __PRETTY_FUNCTION__ __FUNCSIG__
245
246 // Hide a GCC specific thing that breaks MSVC if left alone.
247 # define __extension__
248
249 // We have compiler support for the newest of the new, but
250 // MSVC doesn't tell us that.
251 #define __SSE4_2__ 1
252
253 #endif
254
255 // Debug
256 namespace folly {
257 #ifdef NDEBUG
258 constexpr auto kIsDebug = false;
259 #else
260 constexpr auto kIsDebug = true;
261 #endif
262 }
263
264 // Endianness
265 namespace folly {
266 #ifdef _MSC_VER
267 // It's MSVC, so we just have to guess ... and allow an override
268 #ifdef FOLLY_ENDIAN_BE
269 constexpr auto kIsLittleEndian = false;
270 #else
271 constexpr auto kIsLittleEndian = true;
272 #endif
273 #else
274 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
275 #endif
276 constexpr auto kIsBigEndian = !kIsLittleEndian;
277 }
278
279 #ifndef FOLLY_SSE
280 # if defined(__SSE4_2__)
281 #  define FOLLY_SSE 4
282 #  define FOLLY_SSE_MINOR 2
283 # elif defined(__SSE4_1__)
284 #  define FOLLY_SSE 4
285 #  define FOLLY_SSE_MINOR 1
286 # elif defined(__SSE4__)
287 #  define FOLLY_SSE 4
288 #  define FOLLY_SSE_MINOR 0
289 # elif defined(__SSE3__)
290 #  define FOLLY_SSE 3
291 #  define FOLLY_SSE_MINOR 0
292 # elif defined(__SSE2__)
293 #  define FOLLY_SSE 2
294 #  define FOLLY_SSE_MINOR 0
295 # elif defined(__SSE__)
296 #  define FOLLY_SSE 1
297 #  define FOLLY_SSE_MINOR 0
298 # else
299 #  define FOLLY_SSE 0
300 #  define FOLLY_SSE_MINOR 0
301 # endif
302 #endif
303
304 #define FOLLY_SSE_PREREQ(major, minor) \
305   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
306
307 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
308 namespace FOLLY_GFLAGS_NAMESPACE { }
309 namespace gflags {
310 using namespace FOLLY_GFLAGS_NAMESPACE;
311 }  // namespace gflags
312 #endif
313
314 // for TARGET_OS_IPHONE
315 #ifdef __APPLE__
316 #include <TargetConditionals.h>
317 #endif
318
319 // RTTI may not be enabled for this compilation unit.
320 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
321     (defined(_MSC_VER) && defined(_CPPRTTI))
322 # define FOLLY_HAS_RTTI 1
323 #endif
324
325 #if defined(__APPLE__) || defined(_MSC_VER)
326 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
327 #else
328 // 101 is the highest priority allowed by the init_priority attribute.
329 // This priority is already used by JEMalloc and other memory allocators so
330 // we will take the next one.
331 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
332 #endif
333
334 namespace folly {
335
336 #if defined(__linux__) && !FOLLY_MOBILE
337 constexpr auto kIsLinux = true;
338 #else
339 constexpr auto kIsLinux = false;
340 #endif
341
342 #if defined(_WIN32)
343 constexpr auto kIsWindows = true;
344 #else
345 constexpr auto kIsWindows = false;
346 #endif
347 }