Fix xlog docs
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2017-present 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 <cstddef>
20
21 #include <folly/portability/Config.h>
22
23 #include <folly/CPortability.h>
24
25 // Unaligned loads and stores
26 namespace folly {
27 #if FOLLY_HAVE_UNALIGNED_ACCESS
28 constexpr bool kHasUnalignedAccess = true;
29 #else
30 constexpr bool kHasUnalignedAccess = false;
31 #endif
32 } // namespace folly
33
34 // compiler specific attribute translation
35 // msvc should come first, so if clang is in msvc mode it gets the right defines
36
37 #if defined(__clang__) || defined(__GNUC__)
38 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
39 #elif defined(_MSC_VER)
40 # define FOLLY_ALIGNED(size) __declspec(align(size))
41 #else
42 # error Cannot define FOLLY_ALIGNED on this platform
43 #endif
44
45 // NOTE: this will only do checking in msvc with versions that support /analyze
46 #if _MSC_VER
47 # ifdef _USE_ATTRIBUTES_FOR_SAL
48 #    undef _USE_ATTRIBUTES_FOR_SAL
49 # endif
50 /* nolint */
51 # define _USE_ATTRIBUTES_FOR_SAL 1
52 # include <sal.h> // @manual
53 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
54 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
55 #else
56 # define FOLLY_PRINTF_FORMAT /**/
57 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
58   __attribute__((__format__(__printf__, format_param, dots_param)))
59 #endif
60
61 // deprecated
62 #if defined(__clang__) || defined(__GNUC__)
63 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
64 #elif defined(_MSC_VER)
65 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
66 #else
67 # define FOLLY_DEPRECATED(msg)
68 #endif
69
70 // warn unused result
71 #if defined(__has_cpp_attribute)
72 #if __has_cpp_attribute(nodiscard)
73 #define FOLLY_NODISCARD [[nodiscard]]
74 #endif
75 #endif
76 #if !defined FOLLY_NODISCARD
77 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
78 #define FOLLY_NODISCARD _Check_return_
79 #elif defined(__clang__) || defined(__GNUC__)
80 #define FOLLY_NODISCARD __attribute__((__warn_unused_result__))
81 #else
82 #define FOLLY_NODISCARD
83 #endif
84 #endif
85
86 // target
87 #ifdef _MSC_VER
88 # define FOLLY_TARGET_ATTRIBUTE(target)
89 #else
90 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
91 #endif
92
93 // detection for 64 bit
94 #if defined(__x86_64__) || defined(_M_X64)
95 # define FOLLY_X64 1
96 #else
97 # define FOLLY_X64 0
98 #endif
99
100 #if defined(__aarch64__)
101 # define FOLLY_AARCH64 1
102 #else
103 # define FOLLY_AARCH64 0
104 #endif
105
106 #if defined (__powerpc64__)
107 # define FOLLY_PPC64 1
108 #else
109 # define FOLLY_PPC64 0
110 #endif
111
112 namespace folly {
113 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
114 constexpr bool kIsArchAArch64 = FOLLY_AARCH64 == 1;
115 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
116 } // namespace folly
117
118 namespace folly {
119
120 #if FOLLY_SANITIZE_ADDRESS
121 constexpr bool kIsSanitizeAddress = true;
122 #else
123 constexpr bool kIsSanitizeAddress = false;
124 #endif
125
126 #if FOLLY_SANITIZE_THREAD
127 constexpr bool kIsSanitizeThread = true;
128 #else
129 constexpr bool kIsSanitizeThread = false;
130 #endif
131
132 #if FOLLY_SANITIZE
133 constexpr bool kIsSanitize = true;
134 #else
135 constexpr bool kIsSanitize = false;
136 #endif
137 } // namespace folly
138
139 // packing is very ugly in msvc
140 #ifdef _MSC_VER
141 # define FOLLY_PACK_ATTR /**/
142 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
143 # define FOLLY_PACK_POP __pragma(pack(pop))
144 #elif defined(__clang__) || defined(__GNUC__)
145 # define FOLLY_PACK_ATTR __attribute__((__packed__))
146 # define FOLLY_PACK_PUSH /**/
147 # define FOLLY_PACK_POP /**/
148 #else
149 # define FOLLY_PACK_ATTR /**/
150 # define FOLLY_PACK_PUSH /**/
151 # define FOLLY_PACK_POP /**/
152 #endif
153
154 // Generalize warning push/pop.
155 #if defined(_MSC_VER)
156 # define FOLLY_PUSH_WARNING __pragma(warning(push))
157 # define FOLLY_POP_WARNING __pragma(warning(pop))
158 // Disable the GCC warnings.
159 # define FOLLY_GCC_DISABLE_WARNING(warningName)
160 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
161 #elif defined(__clang__) || defined(__GNUC__)
162 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
163 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
164 # define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) #warningName
165 # define FOLLY_GCC_DISABLE_WARNING(warningName) \
166   _Pragma(                                      \
167   FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
168 // Disable the MSVC warnings.
169 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
170 #else
171 # define FOLLY_PUSH_WARNING
172 # define FOLLY_POP_WARNING
173 # define FOLLY_GCC_DISABLE_WARNING(warningName)
174 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
175 #endif
176
177 #ifdef FOLLY_HAVE_SHADOW_LOCAL_WARNINGS
178 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS        \
179   FOLLY_GCC_DISABLE_WARNING("-Wshadow-compatible-local") \
180   FOLLY_GCC_DISABLE_WARNING("-Wshadow-local")
181 #else
182 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
183 #endif
184
185 /* Platform specific TLS support
186  * gcc implements __thread
187  * msvc implements __declspec(thread)
188  * the semantics are the same
189  * (but remember __thread has different semantics when using emutls (ex. apple))
190  */
191 #if defined(_MSC_VER)
192 # define FOLLY_TLS __declspec(thread)
193 #elif defined(__GNUC__) || defined(__clang__)
194 # define FOLLY_TLS __thread
195 #else
196 # error cannot define platform specific thread local storage
197 #endif
198
199 #if FOLLY_MOBILE
200 #undef FOLLY_TLS
201 #endif
202
203 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
204 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
205 // up in a macro to make forward-declarations easier.
206 #if FOLLY_USE_LIBCPP
207 #include <__config> // @manual
208 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
209 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
210 #else
211 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
212 #define FOLLY_NAMESPACE_STD_END       }
213 #endif
214
215 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
216 // some types, e.g. std::list.
217 #if _GLIBCXX_USE_CXX11_ABI
218 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
219   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
220 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
221 #else
222 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
223 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
224 #endif
225
226 // MSVC specific defines
227 // mainly for posix compat
228 #ifdef _MSC_VER
229 #include <folly/portability/SysTypes.h>
230
231 // compiler specific to compiler specific
232 // nolint
233 # define __PRETTY_FUNCTION__ __FUNCSIG__
234
235 // Hide a GCC specific thing that breaks MSVC if left alone.
236 # define __extension__
237
238 // We have compiler support for the newest of the new, but
239 // MSVC doesn't tell us that.
240 #define __SSE4_2__ 1
241
242 #endif
243
244 // Debug
245 namespace folly {
246 #ifdef NDEBUG
247 constexpr auto kIsDebug = false;
248 #else
249 constexpr auto kIsDebug = true;
250 #endif
251 } // namespace folly
252
253 // Endianness
254 namespace folly {
255 #ifdef _MSC_VER
256 // It's MSVC, so we just have to guess ... and allow an override
257 #ifdef FOLLY_ENDIAN_BE
258 constexpr auto kIsLittleEndian = false;
259 #else
260 constexpr auto kIsLittleEndian = true;
261 #endif
262 #else
263 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
264 #endif
265 constexpr auto kIsBigEndian = !kIsLittleEndian;
266 } // namespace folly
267
268 #ifndef FOLLY_SSE
269 # if defined(__SSE4_2__)
270 #  define FOLLY_SSE 4
271 #  define FOLLY_SSE_MINOR 2
272 # elif defined(__SSE4_1__)
273 #  define FOLLY_SSE 4
274 #  define FOLLY_SSE_MINOR 1
275 # elif defined(__SSE4__)
276 #  define FOLLY_SSE 4
277 #  define FOLLY_SSE_MINOR 0
278 # elif defined(__SSE3__)
279 #  define FOLLY_SSE 3
280 #  define FOLLY_SSE_MINOR 0
281 # elif defined(__SSE2__)
282 #  define FOLLY_SSE 2
283 #  define FOLLY_SSE_MINOR 0
284 # elif defined(__SSE__)
285 #  define FOLLY_SSE 1
286 #  define FOLLY_SSE_MINOR 0
287 # else
288 #  define FOLLY_SSE 0
289 #  define FOLLY_SSE_MINOR 0
290 # endif
291 #endif
292
293 #define FOLLY_SSE_PREREQ(major, minor) \
294   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
295
296 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
297 namespace FOLLY_GFLAGS_NAMESPACE { }
298 namespace gflags {
299 using namespace FOLLY_GFLAGS_NAMESPACE;
300 } // namespace gflags
301 #endif
302
303 // for TARGET_OS_IPHONE
304 #ifdef __APPLE__
305 #include <TargetConditionals.h> // @manual
306 #endif
307
308 // RTTI may not be enabled for this compilation unit.
309 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
310     (defined(_MSC_VER) && defined(_CPPRTTI))
311 # define FOLLY_HAS_RTTI 1
312 #endif
313
314 #if defined(__APPLE__) || defined(_MSC_VER)
315 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
316 #else
317 // 101 is the highest priority allowed by the init_priority attribute.
318 // This priority is already used by JEMalloc and other memory allocators so
319 // we will take the next one.
320 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
321 #endif
322
323 namespace folly {
324
325 #if __OBJC__
326 constexpr auto kIsObjC = true;
327 #else
328 constexpr auto kIsObjC = false;
329 #endif
330
331 #if FOLLY_MOBILE
332 constexpr auto kIsMobile = true;
333 #else
334 constexpr auto kIsMobile = false;
335 #endif
336
337 #if defined(__linux__) && !FOLLY_MOBILE
338 constexpr auto kIsLinux = true;
339 #else
340 constexpr auto kIsLinux = false;
341 #endif
342
343 #if defined(_WIN32)
344 constexpr auto kIsWindows = true;
345 constexpr auto kMscVer = _MSC_VER;
346 #else
347 constexpr auto kIsWindows = false;
348 constexpr auto kMscVer = 0;
349 #endif
350 } // namespace folly
351
352 // Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14
353 // constexpr support is "good enough".
354 #ifndef FOLLY_USE_CPP14_CONSTEXPR
355 #if defined(__clang__)
356 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L
357 #elif defined(__GNUC__)
358 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L
359 #else
360 #define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC?
361 #endif
362 #endif
363
364 #if FOLLY_USE_CPP14_CONSTEXPR
365 #define FOLLY_CPP14_CONSTEXPR constexpr
366 #else
367 #define FOLLY_CPP14_CONSTEXPR inline
368 #endif
369
370 #if __cpp_coroutines >= 201703L || (_MSC_VER && _RESUMABLE_FUNCTIONS_SUPPORTED)
371 #define FOLLY_HAS_COROUTINES 1
372 #endif
373
374 // MSVC 2017.5
375 #if __cpp_noexcept_function_type >= 201510 || _MSC_FULL_VER >= 191225816
376 #define FOLLY_HAVE_NOEXCEPT_FUNCTION_TYPE 1
377 #endif