2 * Copyright 2016 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
24 #include <folly/portability/Config.h>
26 #if FOLLY_HAVE_FEATURES_H
30 #include <folly/CPortability.h>
33 # include <malloc/malloc.h>
36 #if FOLLY_HAVE_SCHED_H
38 #ifndef FOLLY_HAVE_PTHREAD_YIELD
39 #define pthread_yield sched_yield
43 // Unaligned loads and stores
45 #if FOLLY_HAVE_UNALIGNED_ACCESS
46 constexpr bool kHasUnalignedAccess = true;
48 constexpr bool kHasUnalignedAccess = false;
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__)
58 #define MAP_ANONYMOUS MAP_ANON
63 // compiler specific attribute translation
64 // msvc should come first, so if clang is in msvc mode it gets the right defines
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))
71 # error Cannot define FOLLY_ALIGNED on this platform
73 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
75 // NOTE: this will only do checking in msvc with versions that support /analyze
77 # ifdef _USE_ATTRIBUTES_FOR_SAL
78 # undef _USE_ATTRIBUTES_FOR_SAL
81 # define _USE_ATTRIBUTES_FOR_SAL 1
83 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
84 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
86 # define FOLLY_PRINTF_FORMAT /**/
87 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
88 __attribute__((__format__(__printf__, format_param, dots_param)))
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))
97 # define FOLLY_DEPRECATED(msg)
101 #if defined(_MSC_VER)
102 # define FOLLY_NORETURN __declspec(noreturn)
103 #elif defined(__clang__) || defined(__GNUC__)
104 # define FOLLY_NORETURN __attribute__((__noreturn__))
106 # define FOLLY_NORETURN
111 # define FOLLY_NOINLINE __declspec(noinline)
112 #elif defined(__clang__) || defined(__GNUC__)
113 # define FOLLY_NOINLINE __attribute__((__noinline__))
115 # define FOLLY_NOINLINE
120 # define FOLLY_ALWAYS_INLINE __forceinline
121 #elif defined(__clang__) || defined(__GNUC__)
122 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
124 # define FOLLY_ALWAYS_INLINE inline
127 // detection for 64 bit
128 #if defined(__x86_64__) || defined(_M_X64)
134 #if defined(__aarch64__)
140 #if defined (__powerpc64__)
141 # define FOLLY_PPC64 1
143 # define FOLLY_PPC64 0
146 // packing is very ugly in msvc
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 /**/
156 # define FOLLY_PACK_ATTR /**/
157 # define FOLLY_PACK_PUSH /**/
158 # define FOLLY_PACK_POP /**/
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)
180 # define FOLLY_PUSH_WARNING
181 # define FOLLY_POP_WARNING
182 # define FOLLY_GCC_DISABLE_WARNING(warningName)
183 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
186 // portable version check
187 #ifndef __GNUC_PREREQ
188 # if defined __GNUC__ && defined __GNUC_MINOR__
190 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
191 ((maj) << 16) + (min))
194 # define __GNUC_PREREQ(maj, min) 0
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; }
205 // portable version check for clang
206 #ifndef __CLANG_PREREQ
207 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
209 # define __CLANG_PREREQ(maj, min) \
210 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
213 # define __CLANG_PREREQ(maj, min) 0
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))
223 #if defined(_MSC_VER)
224 # define FOLLY_TLS __declspec(thread)
225 #elif defined(__GNUC__) || defined(__clang__)
226 # define FOLLY_TLS __thread
228 # error cannot define platform specific thread local storage
231 #if defined(__APPLE__) && (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
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
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.
250 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
251 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
253 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
254 #define FOLLY_NAMESPACE_STD_END }
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
263 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
264 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
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
272 #include <folly/detail/Clock.h>
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>
279 #include <folly/detail/FunctionalExcept.h>
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>.
288 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
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)
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)
302 #endif // __cplusplus
304 // MSVC specific defines
305 // mainly for posix compat
307 #include <folly/portability/SysTypes.h>
309 // sprintf semantics are not exactly identical
310 // but current usage is not a problem
311 # define snprintf _snprintf
313 // semantics here are identical
314 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
316 // compiler specific to compiler specific
318 # define __PRETTY_FUNCTION__ __FUNCSIG__
320 // Hide a GCC specific thing that breaks MSVC if left alone.
321 # define __extension__
324 # define FOLLY_SSE _M_IX86_FP
325 # define FOLLY_SSE_MINOR 0
333 constexpr auto kIsDebug = false;
335 constexpr auto kIsDebug = true;
342 // It's MSVC, so we just have to guess ... and allow an override
343 #ifdef FOLLY_ENDIAN_BE
344 constexpr auto kIsLittleEndian = false;
346 constexpr auto kIsLittleEndian = true;
349 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
351 constexpr auto kIsBigEndian = !kIsLittleEndian;
355 # if defined(__SSE4_2__)
357 # define FOLLY_SSE_MINOR 2
358 # elif defined(__SSE4_1__)
360 # define FOLLY_SSE_MINOR 1
361 # elif defined(__SSE4__)
363 # define FOLLY_SSE_MINOR 0
364 # elif defined(__SSE3__)
366 # define FOLLY_SSE_MINOR 0
367 # elif defined(__SSE2__)
369 # define FOLLY_SSE_MINOR 0
370 # elif defined(__SSE__)
372 # define FOLLY_SSE_MINOR 0
375 # define FOLLY_SSE_MINOR 0
379 #define FOLLY_SSE_PREREQ(major, minor) \
380 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
382 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
383 namespace FOLLY_GFLAGS_NAMESPACE { }
385 using namespace FOLLY_GFLAGS_NAMESPACE;
386 } // namespace gflags
389 // for TARGET_OS_IPHONE
391 #include <TargetConditionals.h>
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);
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
413 inline void asm_volatile_memory() {
414 #if defined(__clang__) || defined(__GNUC__)
415 asm volatile("" : : : "memory");
416 #elif defined(_MSC_VER)
417 ::_ReadWriteBarrier();
421 inline void asm_volatile_pause() {
422 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
424 #elif defined(__i386__) || FOLLY_X64
425 asm volatile ("pause");
426 #elif FOLLY_A64 || defined(__arm__)
427 asm volatile ("yield");
429 asm volatile("or 27,27,27");
432 inline void asm_pause() {
433 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
435 #elif defined(__i386__) || FOLLY_X64
437 #elif FOLLY_A64 || defined(__arm__)
444 #if defined(__APPLE__) || defined(_MSC_VER)
445 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
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)))
454 #endif // FOLLY_PORTABILITY_H_