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