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
40 // Unaligned loads and stores
42 #if FOLLY_HAVE_UNALIGNED_ACCESS
43 constexpr bool kHasUnalignedAccess = true;
45 constexpr bool kHasUnalignedAccess = false;
49 // compiler specific attribute translation
50 // msvc should come first, so if clang is in msvc mode it gets the right defines
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))
57 # error Cannot define FOLLY_ALIGNED on this platform
59 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
61 // NOTE: this will only do checking in msvc with versions that support /analyze
63 # ifdef _USE_ATTRIBUTES_FOR_SAL
64 # undef _USE_ATTRIBUTES_FOR_SAL
67 # define _USE_ATTRIBUTES_FOR_SAL 1
69 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
70 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
72 # define FOLLY_PRINTF_FORMAT /**/
73 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
74 __attribute__((__format__(__printf__, format_param, dots_param)))
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))
83 # define FOLLY_DEPRECATED(msg)
88 # define FOLLY_NORETURN __declspec(noreturn)
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_NORETURN __attribute__((__noreturn__))
92 # define FOLLY_NORETURN
97 # define FOLLY_NOINLINE __declspec(noinline)
98 #elif defined(__clang__) || defined(__GNUC__)
99 # define FOLLY_NOINLINE __attribute__((__noinline__))
101 # define FOLLY_NOINLINE
106 # define FOLLY_ALWAYS_INLINE __forceinline
107 #elif defined(__clang__) || defined(__GNUC__)
108 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
110 # define FOLLY_ALWAYS_INLINE inline
113 // detection for 64 bit
114 #if defined(__x86_64__) || defined(_M_X64)
120 #if defined(__aarch64__)
126 #if defined (__powerpc64__)
127 # define FOLLY_PPC64 1
129 # define FOLLY_PPC64 0
132 // packing is very ugly in msvc
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 /**/
142 # define FOLLY_PACK_ATTR /**/
143 # define FOLLY_PACK_PUSH /**/
144 # define FOLLY_PACK_POP /**/
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)
166 # define FOLLY_PUSH_WARNING
167 # define FOLLY_POP_WARNING
168 # define FOLLY_GCC_DISABLE_WARNING(warningName)
169 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
172 // portable version check
173 #ifndef __GNUC_PREREQ
174 # if defined __GNUC__ && defined __GNUC_MINOR__
176 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
177 ((maj) << 16) + (min))
180 # define __GNUC_PREREQ(maj, min) 0
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; }
191 // portable version check for clang
192 #ifndef __CLANG_PREREQ
193 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
195 # define __CLANG_PREREQ(maj, min) \
196 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
199 # define __CLANG_PREREQ(maj, min) 0
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))
209 #if defined(_MSC_VER)
210 # define FOLLY_TLS __declspec(thread)
211 #elif defined(__GNUC__) || defined(__clang__)
212 # define FOLLY_TLS __thread
214 # error cannot define platform specific thread local storage
217 #if defined(__APPLE__) && (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
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
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.
236 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
237 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
239 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
240 #define FOLLY_NAMESPACE_STD_END }
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
249 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
250 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
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
258 #include <folly/detail/Clock.h>
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>
265 #include <folly/detail/FunctionalExcept.h>
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>.
274 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
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)
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)
288 #endif // __cplusplus
290 // MSVC specific defines
291 // mainly for posix compat
293 #include <folly/portability/SysTypes.h>
295 // sprintf semantics are not exactly identical
296 // but current usage is not a problem
297 # define snprintf _snprintf
299 // semantics here are identical
300 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
302 // compiler specific to compiler specific
304 # define __PRETTY_FUNCTION__ __FUNCSIG__
306 // Hide a GCC specific thing that breaks MSVC if left alone.
307 # define __extension__
310 # define FOLLY_SSE _M_IX86_FP
311 # define FOLLY_SSE_MINOR 0
319 constexpr auto kIsDebug = false;
321 constexpr auto kIsDebug = true;
328 // It's MSVC, so we just have to guess ... and allow an override
329 #ifdef FOLLY_ENDIAN_BE
330 constexpr auto kIsLittleEndian = false;
332 constexpr auto kIsLittleEndian = true;
335 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
337 constexpr auto kIsBigEndian = !kIsLittleEndian;
341 # if defined(__SSE4_2__)
343 # define FOLLY_SSE_MINOR 2
344 # elif defined(__SSE4_1__)
346 # define FOLLY_SSE_MINOR 1
347 # elif defined(__SSE4__)
349 # define FOLLY_SSE_MINOR 0
350 # elif defined(__SSE3__)
352 # define FOLLY_SSE_MINOR 0
353 # elif defined(__SSE2__)
355 # define FOLLY_SSE_MINOR 0
356 # elif defined(__SSE__)
358 # define FOLLY_SSE_MINOR 0
361 # define FOLLY_SSE_MINOR 0
365 #define FOLLY_SSE_PREREQ(major, minor) \
366 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
368 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
369 namespace FOLLY_GFLAGS_NAMESPACE { }
371 using namespace FOLLY_GFLAGS_NAMESPACE;
372 } // namespace gflags
375 // for TARGET_OS_IPHONE
377 #include <TargetConditionals.h>
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);
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
399 inline void asm_volatile_memory() {
400 #if defined(__clang__) || defined(__GNUC__)
401 asm volatile("" : : : "memory");
402 #elif defined(_MSC_VER)
403 ::_ReadWriteBarrier();
407 inline void asm_volatile_pause() {
408 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
410 #elif defined(__i386__) || FOLLY_X64
411 asm volatile ("pause");
412 #elif FOLLY_A64 || defined(__arm__)
413 asm volatile ("yield");
415 asm volatile("or 27,27,27");
418 inline void asm_pause() {
419 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
421 #elif defined(__i386__) || FOLLY_X64
423 #elif FOLLY_A64 || defined(__arm__)
430 #if defined(__APPLE__) || defined(_MSC_VER)
431 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
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)))
440 #endif // FOLLY_PORTABILITY_H_