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 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
50 // MAP_ANON on OSX/BSD.
51 #if defined(__APPLE__) || defined(__FreeBSD__)
55 #define MAP_ANONYMOUS MAP_ANON
60 // compiler specific attribute translation
61 // msvc should come first, so if clang is in msvc mode it gets the right defines
63 #if defined(__clang__) || defined(__GNUC__)
64 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
65 #elif defined(_MSC_VER)
66 # define FOLLY_ALIGNED(size) __declspec(align(size))
68 # error Cannot define FOLLY_ALIGNED on this platform
70 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
72 // NOTE: this will only do checking in msvc with versions that support /analyze
74 # ifdef _USE_ATTRIBUTES_FOR_SAL
75 # undef _USE_ATTRIBUTES_FOR_SAL
78 # define _USE_ATTRIBUTES_FOR_SAL 1
80 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
81 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
83 # define FOLLY_PRINTF_FORMAT /**/
84 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
85 __attribute__((__format__(__printf__, format_param, dots_param)))
89 #if defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
91 #elif defined(_MSC_VER)
92 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
94 # define FOLLY_DEPRECATED(msg)
99 # define FOLLY_NORETURN __declspec(noreturn)
100 #elif defined(__clang__) || defined(__GNUC__)
101 # define FOLLY_NORETURN __attribute__((__noreturn__))
103 # define FOLLY_NORETURN
108 # define FOLLY_NOINLINE __declspec(noinline)
109 #elif defined(__clang__) || defined(__GNUC__)
110 # define FOLLY_NOINLINE __attribute__((__noinline__))
112 # define FOLLY_NOINLINE
117 # define FOLLY_ALWAYS_INLINE __forceinline
118 #elif defined(__clang__) || defined(__GNUC__)
119 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
121 # define FOLLY_ALWAYS_INLINE inline
124 // detection for 64 bit
125 #if defined(__x86_64__) || defined(_M_X64)
131 #if defined(__aarch64__)
137 #if defined (__powerpc64__)
138 # define FOLLY_PPC64 1
140 # define FOLLY_PPC64 0
143 // packing is very ugly in msvc
145 # define FOLLY_PACK_ATTR /**/
146 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
147 # define FOLLY_PACK_POP __pragma(pack(pop))
148 #elif defined(__clang__) || defined(__GNUC__)
149 # define FOLLY_PACK_ATTR __attribute__((__packed__))
150 # define FOLLY_PACK_PUSH /**/
151 # define FOLLY_PACK_POP /**/
153 # define FOLLY_PACK_ATTR /**/
154 # define FOLLY_PACK_PUSH /**/
155 # define FOLLY_PACK_POP /**/
158 // Generalize warning push/pop.
159 #if defined(_MSC_VER)
160 # define FOLLY_PUSH_WARNING __pragma(warning(push))
161 # define FOLLY_POP_WARNING __pragma(warning(pop))
162 // Disable the GCC warnings.
163 # define FOLLY_GCC_DISABLE_WARNING(warningName)
164 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
165 #elif defined(__clang__) || defined(__GNUC__)
166 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
167 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
168 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
169 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
170 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
171 #define FOLLY_GCC_DISABLE_WARNING(warningName) \
172 _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
173 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
174 // Disable the MSVC warnings.
175 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
177 # define FOLLY_PUSH_WARNING
178 # define FOLLY_POP_WARNING
179 # define FOLLY_GCC_DISABLE_WARNING(warningName)
180 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
183 // portable version check
184 #ifndef __GNUC_PREREQ
185 # if defined __GNUC__ && defined __GNUC_MINOR__
187 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
188 ((maj) << 16) + (min))
191 # define __GNUC_PREREQ(maj, min) 0
195 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
196 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
197 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
198 // Alias it into std (where it's found in 4.9 and later)
199 namespace std { typedef ::max_align_t max_align_t; }
202 // portable version check for clang
203 #ifndef __CLANG_PREREQ
204 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
206 # define __CLANG_PREREQ(maj, min) \
207 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
210 # define __CLANG_PREREQ(maj, min) 0
214 /* Platform specific TLS support
215 * gcc implements __thread
216 * msvc implements __declspec(thread)
217 * the semantics are the same
218 * (but remember __thread has different semantics when using emutls (ex. apple))
220 #if defined(_MSC_VER)
221 # define FOLLY_TLS __declspec(thread)
222 #elif defined(__GNUC__) || defined(__clang__)
223 # define FOLLY_TLS __thread
225 # error cannot define platform specific thread local storage
228 #if defined(__APPLE__) && (TARGET_IPHONE_SIMULATOR || TARGET_OS_IPHONE)
232 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
233 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
234 # if defined(__GLIBC_PREREQ)
235 # if __GLIBC_PREREQ(2, 10)
236 # define FOLLY_HAVE_PREADV 1
237 # define FOLLY_HAVE_PWRITEV 1
242 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
243 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
244 // up in a macro to make forward-declarations easier.
247 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
248 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
250 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
251 #define FOLLY_NAMESPACE_STD_END }
254 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
255 // some types, e.g. std::list.
256 #if _GLIBCXX_USE_CXX11_ABI
257 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
258 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
260 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
261 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
264 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
265 // versions of these into the global namespace.
266 #if FOLLY_HAVE_CLOCK_GETTIME
269 #include <folly/detail/Clock.h>
272 // Provide our own std::__throw_* wrappers for platforms that don't have them
273 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
274 #include <bits/functexcept.h>
276 #include <folly/detail/FunctionalExcept.h>
279 #if defined(__cplusplus)
280 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
281 // usage of __has_trivial_copy(), so we can't use it as a
282 // least-common-denominator for C++11 implementations that don't support
283 // std::is_trivially_copyable<T>.
285 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
287 // As a result, use std::is_trivially_copyable() where it exists, and fall back
288 // to Boost otherwise.
289 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
290 #include <type_traits>
291 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
292 (std::is_trivially_copyable<T>::value)
294 #include <boost/type_traits.hpp>
295 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
296 (boost::has_trivial_copy<T>::value && \
297 boost::has_trivial_destructor<T>::value)
299 #endif // __cplusplus
301 // MSVC specific defines
302 // mainly for posix compat
304 #include <folly/portability/SysTypes.h>
306 // sprintf semantics are not exactly identical
307 // but current usage is not a problem
308 # define snprintf _snprintf
310 // semantics here are identical
311 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
313 // compiler specific to compiler specific
315 # define __PRETTY_FUNCTION__ __FUNCSIG__
317 // Hide a GCC specific thing that breaks MSVC if left alone.
318 # define __extension__
321 # define FOLLY_SSE _M_IX86_FP
322 # define FOLLY_SSE_MINOR 0
330 constexpr auto kIsDebug = false;
332 constexpr auto kIsDebug = true;
339 // It's MSVC, so we just have to guess ... and allow an override
340 #ifdef FOLLY_ENDIAN_BE
341 constexpr auto kIsLittleEndian = false;
343 constexpr auto kIsLittleEndian = true;
346 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
348 constexpr auto kIsBigEndian = !kIsLittleEndian;
352 # if defined(__SSE4_2__)
354 # define FOLLY_SSE_MINOR 2
355 # elif defined(__SSE4_1__)
357 # define FOLLY_SSE_MINOR 1
358 # elif defined(__SSE4__)
360 # define FOLLY_SSE_MINOR 0
361 # elif defined(__SSE3__)
363 # define FOLLY_SSE_MINOR 0
364 # elif defined(__SSE2__)
366 # define FOLLY_SSE_MINOR 0
367 # elif defined(__SSE__)
369 # define FOLLY_SSE_MINOR 0
372 # define FOLLY_SSE_MINOR 0
376 #define FOLLY_SSE_PREREQ(major, minor) \
377 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
379 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
380 namespace FOLLY_GFLAGS_NAMESPACE { }
382 using namespace FOLLY_GFLAGS_NAMESPACE;
383 } // namespace gflags
386 // for TARGET_OS_IPHONE
388 #include <TargetConditionals.h>
391 // MacOS doesn't have malloc_usable_size()
392 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
393 inline size_t malloc_usable_size(void* ptr) {
394 return malloc_size(ptr);
398 // RTTI may not be enabled for this compilation unit.
399 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
400 (defined(_MSC_VER) && defined(_CPPRTTI))
401 # define FOLLY_HAS_RTTI 1
410 inline void asm_volatile_memory() {
411 #if defined(__clang__) || defined(__GNUC__)
412 asm volatile("" : : : "memory");
413 #elif defined(_MSC_VER)
414 ::_ReadWriteBarrier();
418 inline void asm_volatile_pause() {
419 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
421 #elif defined(__i386__) || FOLLY_X64
422 asm volatile ("pause");
423 #elif FOLLY_A64 || defined(__arm__)
424 asm volatile ("yield");
426 asm volatile("or 27,27,27");
429 inline void asm_pause() {
430 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
432 #elif defined(__i386__) || FOLLY_X64
434 #elif FOLLY_A64 || defined(__arm__)
441 #if defined(__APPLE__) || defined(_MSC_VER)
442 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
444 // 101 is the highest priority allowed by the init_priority attribute.
445 // This priority is already used by JEMalloc and other memory allocators so
446 // we will take the next one.
447 #define MAX_STATIC_CONSTRUCTOR_PRIORITY __attribute__ ((__init_priority__(102)))
451 #endif // FOLLY_PORTABILITY_H_