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