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