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