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