Use intrinsics rather than inline assembly where possible
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2016 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 // noinline
78 #ifdef _MSC_VER
79 # define FOLLY_NOINLINE __declspec(noinline)
80 #elif defined(__clang__) || defined(__GNUC__)
81 # define FOLLY_NOINLINE __attribute__((__noinline__))
82 #else
83 # define FOLLY_NOINLINE
84 #endif
85
86 // always inline
87 #ifdef _MSC_VER
88 # define FOLLY_ALWAYS_INLINE __forceinline
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
91 #else
92 # define FOLLY_ALWAYS_INLINE inline
93 #endif
94
95 // target
96 #ifdef _MSC_VER
97 # define FOLLY_TARGET_ATTRIBUTE(target)
98 #else
99 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
100 #endif
101
102 // detection for 64 bit
103 #if defined(__x86_64__) || defined(_M_X64)
104 # define FOLLY_X64 1
105 #else
106 # define FOLLY_X64 0
107 #endif
108
109 #if defined(__aarch64__)
110 # define FOLLY_A64 1
111 #else
112 # define FOLLY_A64 0
113 #endif
114
115 #if defined (__powerpc64__)
116 # define FOLLY_PPC64 1
117 #else
118 # define FOLLY_PPC64 0
119 #endif
120
121 // packing is very ugly in msvc
122 #ifdef _MSC_VER
123 # define FOLLY_PACK_ATTR /**/
124 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
125 # define FOLLY_PACK_POP __pragma(pack(pop))
126 #elif defined(__clang__) || defined(__GNUC__)
127 # define FOLLY_PACK_ATTR __attribute__((__packed__))
128 # define FOLLY_PACK_PUSH /**/
129 # define FOLLY_PACK_POP /**/
130 #else
131 # define FOLLY_PACK_ATTR /**/
132 # define FOLLY_PACK_PUSH /**/
133 # define FOLLY_PACK_POP /**/
134 #endif
135
136 // Generalize warning push/pop.
137 #if defined(_MSC_VER)
138 # define FOLLY_PUSH_WARNING __pragma(warning(push))
139 # define FOLLY_POP_WARNING __pragma(warning(pop))
140 // Disable the GCC warnings.
141 # define FOLLY_GCC_DISABLE_WARNING(warningName)
142 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
143 #elif defined(__clang__) || defined(__GNUC__)
144 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
145 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
146 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
147 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
148   FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
149 #define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
150   _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
151           FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
152 // Disable the MSVC warnings.
153 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
154 #else
155 # define FOLLY_PUSH_WARNING
156 # define FOLLY_POP_WARNING
157 # define FOLLY_GCC_DISABLE_WARNING(warningName)
158 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
159 #endif
160
161 // portable version check
162 #ifndef __GNUC_PREREQ
163 # if defined __GNUC__ && defined __GNUC_MINOR__
164 /* nolint */
165 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
166                                    ((maj) << 16) + (min))
167 # else
168 /* nolint */
169 #  define __GNUC_PREREQ(maj, min) 0
170 # endif
171 #endif
172
173 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
174 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
175 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
176 // Alias it into std (where it's found in 4.9 and later)
177 namespace std { typedef ::max_align_t max_align_t; }
178 #endif
179
180 // portable version check for clang
181 #ifndef __CLANG_PREREQ
182 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
183 /* nolint */
184 #  define __CLANG_PREREQ(maj, min) \
185     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
186 # else
187 /* nolint */
188 #  define __CLANG_PREREQ(maj, min) 0
189 # endif
190 #endif
191
192 /* Platform specific TLS support
193  * gcc implements __thread
194  * msvc implements __declspec(thread)
195  * the semantics are the same
196  * (but remember __thread has different semantics when using emutls (ex. apple))
197  */
198 #if defined(_MSC_VER)
199 # define FOLLY_TLS __declspec(thread)
200 #elif defined(__GNUC__) || defined(__clang__)
201 # define FOLLY_TLS __thread
202 #else
203 # error cannot define platform specific thread local storage
204 #endif
205
206 #if FOLLY_MOBILE
207 #undef FOLLY_TLS
208 #endif
209
210 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
211 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
212 # if defined(__GLIBC_PREREQ)
213 #  if __GLIBC_PREREQ(2, 10)
214 #   define FOLLY_HAVE_PREADV 1
215 #   define FOLLY_HAVE_PWRITEV 1
216 #  endif
217 # endif
218 #endif
219
220 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
221 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
222 // up in a macro to make forward-declarations easier.
223 #if FOLLY_USE_LIBCPP
224 #include <__config>
225 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
226 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
227 #else
228 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
229 #define FOLLY_NAMESPACE_STD_END       }
230 #endif
231
232 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
233 // some types, e.g. std::list.
234 #if _GLIBCXX_USE_CXX11_ABI
235 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
236 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
237 #else
238 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
239 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
240 #endif
241
242 // Provide our own std::__throw_* wrappers for platforms that don't have them
243 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
244 #include <bits/functexcept.h>
245 #else
246 #include <folly/detail/FunctionalExcept.h>
247 #endif
248
249 #if defined(__cplusplus)
250 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
251 // usage of __has_trivial_copy(), so we can't use it as a
252 // least-common-denominator for C++11 implementations that don't support
253 // std::is_trivially_copyable<T>.
254 //
255 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
256 //
257 // As a result, use std::is_trivially_copyable() where it exists, and fall back
258 // to Boost otherwise.
259 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
260 #include <type_traits>
261 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
262   (std::is_trivially_copyable<T>::value)
263 #else
264 #include <boost/type_traits.hpp>
265 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
266   (boost::has_trivial_copy<T>::value &&                  \
267    boost::has_trivial_destructor<T>::value)
268 #endif
269 #endif // __cplusplus
270
271 // MSVC specific defines
272 // mainly for posix compat
273 #ifdef _MSC_VER
274 #include <folly/portability/SysTypes.h>
275
276 // sprintf semantics are not exactly identical
277 // but current usage is not a problem
278 # define snprintf _snprintf
279
280 // semantics here are identical
281 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
282
283 // compiler specific to compiler specific
284 // nolint
285 # define __PRETTY_FUNCTION__ __FUNCSIG__
286
287 // Hide a GCC specific thing that breaks MSVC if left alone.
288 # define __extension__
289
290 // We have compiler support for the newest of the new, but
291 // MSVC doesn't tell us that.
292 #define __SSE4_2__ 1
293
294 #endif
295
296 // Debug
297 namespace folly {
298 #ifdef NDEBUG
299 constexpr auto kIsDebug = false;
300 #else
301 constexpr auto kIsDebug = true;
302 #endif
303 }
304
305 // Endianness
306 namespace folly {
307 #ifdef _MSC_VER
308 // It's MSVC, so we just have to guess ... and allow an override
309 #ifdef FOLLY_ENDIAN_BE
310 constexpr auto kIsLittleEndian = false;
311 #else
312 constexpr auto kIsLittleEndian = true;
313 #endif
314 #else
315 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
316 #endif
317 constexpr auto kIsBigEndian = !kIsLittleEndian;
318 }
319
320 #ifndef FOLLY_SSE
321 # if defined(__SSE4_2__)
322 #  define FOLLY_SSE 4
323 #  define FOLLY_SSE_MINOR 2
324 # elif defined(__SSE4_1__)
325 #  define FOLLY_SSE 4
326 #  define FOLLY_SSE_MINOR 1
327 # elif defined(__SSE4__)
328 #  define FOLLY_SSE 4
329 #  define FOLLY_SSE_MINOR 0
330 # elif defined(__SSE3__)
331 #  define FOLLY_SSE 3
332 #  define FOLLY_SSE_MINOR 0
333 # elif defined(__SSE2__)
334 #  define FOLLY_SSE 2
335 #  define FOLLY_SSE_MINOR 0
336 # elif defined(__SSE__)
337 #  define FOLLY_SSE 1
338 #  define FOLLY_SSE_MINOR 0
339 # else
340 #  define FOLLY_SSE 0
341 #  define FOLLY_SSE_MINOR 0
342 # endif
343 #endif
344
345 #define FOLLY_SSE_PREREQ(major, minor) \
346   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
347
348 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
349 namespace FOLLY_GFLAGS_NAMESPACE { }
350 namespace gflags {
351 using namespace FOLLY_GFLAGS_NAMESPACE;
352 }  // namespace gflags
353 #endif
354
355 // for TARGET_OS_IPHONE
356 #ifdef __APPLE__
357 #include <TargetConditionals.h>
358 #endif
359
360 // RTTI may not be enabled for this compilation unit.
361 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
362     (defined(_MSC_VER) && defined(_CPPRTTI))
363 # define FOLLY_HAS_RTTI 1
364 #endif
365
366 #if defined(__APPLE__) || defined(_MSC_VER)
367 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
368 #else
369 // 101 is the highest priority allowed by the init_priority attribute.
370 // This priority is already used by JEMalloc and other memory allocators so
371 // we will take the next one.
372 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
373 #endif