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