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 #ifndef FOLLY_NO_CONFIG
25 #include <folly/folly-config.h>
28 #ifdef FOLLY_PLATFORM_CONFIG
29 #include FOLLY_PLATFORM_CONFIG
32 #if FOLLY_HAVE_FEATURES_H
36 #include <folly/CPortability.h>
39 # include <malloc/malloc.h>
42 #if FOLLY_HAVE_SCHED_H
44 #ifndef FOLLY_HAVE_PTHREAD_YIELD
45 #define pthread_yield sched_yield
49 // Unaligned loads and stores
51 #if FOLLY_HAVE_UNALIGNED_ACCESS
52 constexpr bool kHasUnalignedAccess = true;
54 constexpr bool kHasUnalignedAccess = false;
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__)
64 #define MAP_ANONYMOUS MAP_ANON
69 // compiler specific attribute translation
70 // msvc should come first, so if clang is in msvc mode it gets the right defines
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))
77 # error Cannot define FOLLY_ALIGNED on this platform
79 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
81 // NOTE: this will only do checking in msvc with versions that support /analyze
83 # ifdef _USE_ATTRIBUTES_FOR_SAL
84 # undef _USE_ATTRIBUTES_FOR_SAL
87 # define _USE_ATTRIBUTES_FOR_SAL 1
89 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
90 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
92 # define FOLLY_PRINTF_FORMAT /**/
93 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
94 __attribute__((__format__(__printf__, format_param, dots_param)))
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))
103 # define FOLLY_DEPRECATED(msg)
107 #if defined(_MSC_VER)
108 # define FOLLY_NORETURN __declspec(noreturn)
109 #elif defined(__clang__) || defined(__GNUC__)
110 # define FOLLY_NORETURN __attribute__((__noreturn__))
112 # define FOLLY_NORETURN
117 # define FOLLY_NOINLINE __declspec(noinline)
118 #elif defined(__clang__) || defined(__GNUC__)
119 # define FOLLY_NOINLINE __attribute__((__noinline__))
121 # define FOLLY_NOINLINE
126 # define FOLLY_ALWAYS_INLINE __forceinline
127 #elif defined(__clang__) || defined(__GNUC__)
128 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
130 # define FOLLY_ALWAYS_INLINE inline
133 // detection for 64 bit
134 #if defined(__x86_64__) || defined(_M_X64)
140 #if defined(__aarch64__)
146 #if defined (__powerpc64__)
147 # define FOLLY_PPC64 1
149 # define FOLLY_PPC64 0
152 // packing is very ugly in msvc
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 /**/
162 # define FOLLY_PACK_ATTR /**/
163 # define FOLLY_PACK_PUSH /**/
164 # define FOLLY_PACK_POP /**/
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)
186 # define FOLLY_PUSH_WARNING
187 # define FOLLY_POP_WARNING
188 # define FOLLY_GCC_DISABLE_WARNING(warningName)
189 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
192 // portable version check
193 #ifndef __GNUC_PREREQ
194 # if defined __GNUC__ && defined __GNUC_MINOR__
196 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
197 ((maj) << 16) + (min))
200 # define __GNUC_PREREQ(maj, min) 0
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; }
211 // portable version check for clang
212 #ifndef __CLANG_PREREQ
213 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
215 # define __CLANG_PREREQ(maj, min) \
216 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
219 # define __CLANG_PREREQ(maj, min) 0
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))
229 #if defined(_MSC_VER)
230 # define FOLLY_TLS __declspec(thread)
231 #elif defined(__GNUC__) || defined(__clang__)
232 # define FOLLY_TLS __thread
234 # error cannot define platform specific thread local storage
237 #if defined(__APPLE__) && (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
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
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.
256 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
257 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
259 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
260 #define FOLLY_NAMESPACE_STD_END }
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
269 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
270 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
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
278 #include <folly/detail/Clock.h>
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>
285 #include <folly/detail/FunctionalExcept.h>
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>.
294 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
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)
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)
308 #endif // __cplusplus
310 // MSVC specific defines
311 // mainly for posix compat
313 #include <folly/portability/SysTypes.h>
315 // sprintf semantics are not exactly identical
316 // but current usage is not a problem
317 # define snprintf _snprintf
319 // semantics here are identical
320 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
322 // compiler specific to compiler specific
324 # define __PRETTY_FUNCTION__ __FUNCSIG__
326 // Hide a GCC specific thing that breaks MSVC if left alone.
327 # define __extension__
330 # define FOLLY_SSE _M_IX86_FP
331 # define FOLLY_SSE_MINOR 0
339 constexpr auto kIsDebug = false;
341 constexpr auto kIsDebug = true;
348 // It's MSVC, so we just have to guess ... and allow an override
349 #ifdef FOLLY_ENDIAN_BE
350 constexpr auto kIsLittleEndian = false;
352 constexpr auto kIsLittleEndian = true;
355 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
357 constexpr auto kIsBigEndian = !kIsLittleEndian;
361 # if defined(__SSE4_2__)
363 # define FOLLY_SSE_MINOR 2
364 # elif defined(__SSE4_1__)
366 # define FOLLY_SSE_MINOR 1
367 # elif defined(__SSE4__)
369 # define FOLLY_SSE_MINOR 0
370 # elif defined(__SSE3__)
372 # define FOLLY_SSE_MINOR 0
373 # elif defined(__SSE2__)
375 # define FOLLY_SSE_MINOR 0
376 # elif defined(__SSE__)
378 # define FOLLY_SSE_MINOR 0
381 # define FOLLY_SSE_MINOR 0
385 #define FOLLY_SSE_PREREQ(major, minor) \
386 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
388 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
389 namespace FOLLY_GFLAGS_NAMESPACE { }
391 using namespace FOLLY_GFLAGS_NAMESPACE;
392 } // namespace gflags
395 // for TARGET_OS_IPHONE
397 #include <TargetConditionals.h>
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);
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
419 inline void asm_volatile_memory() {
420 #if defined(__clang__) || defined(__GNUC__)
421 asm volatile("" : : : "memory");
422 #elif defined(_MSC_VER)
423 ::_ReadWriteBarrier();
427 inline void asm_volatile_pause() {
428 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
430 #elif defined(__i386__) || FOLLY_X64
431 asm volatile ("pause");
432 #elif FOLLY_A64 || defined(__arm__)
433 asm volatile ("yield");
435 asm volatile("or 27,27,27");
438 inline void asm_pause() {
439 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
441 #elif defined(__i386__) || FOLLY_X64
443 #elif FOLLY_A64 || defined(__arm__)
450 #if defined(__APPLE__) || defined(_MSC_VER)
451 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
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)))
460 #endif // FOLLY_PORTABILITY_H_