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