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