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.
23 #include <folly/portability/Config.h>
25 #include <folly/CPortability.h>
27 #if FOLLY_HAVE_SCHED_H
31 // Unaligned loads and stores
33 #if FOLLY_HAVE_UNALIGNED_ACCESS
34 constexpr bool kHasUnalignedAccess = true;
36 constexpr bool kHasUnalignedAccess = false;
40 // compiler specific attribute translation
41 // msvc should come first, so if clang is in msvc mode it gets the right defines
43 #if defined(__clang__) || defined(__GNUC__)
44 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
45 #elif defined(_MSC_VER)
46 # define FOLLY_ALIGNED(size) __declspec(align(size))
48 # error Cannot define FOLLY_ALIGNED on this platform
50 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
52 // NOTE: this will only do checking in msvc with versions that support /analyze
54 # ifdef _USE_ATTRIBUTES_FOR_SAL
55 # undef _USE_ATTRIBUTES_FOR_SAL
58 # define _USE_ATTRIBUTES_FOR_SAL 1
60 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
61 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
63 # define FOLLY_PRINTF_FORMAT /**/
64 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
65 __attribute__((__format__(__printf__, format_param, dots_param)))
69 #if defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
71 #elif defined(_MSC_VER)
72 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
74 # define FOLLY_DEPRECATED(msg)
79 # define FOLLY_NOINLINE __declspec(noinline)
80 #elif defined(__clang__) || defined(__GNUC__)
81 # define FOLLY_NOINLINE __attribute__((__noinline__))
83 # define FOLLY_NOINLINE
88 # define FOLLY_ALWAYS_INLINE __forceinline
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
92 # define FOLLY_ALWAYS_INLINE inline
96 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
97 #define FOLLY_WARN_UNUSED_RESULT _Check_return_
98 #elif defined(__clang__) || defined(__GNUC__)
99 #define FOLLY_WARN_UNUSED_RESULT __attribute__((__warn_unused_result__))
101 #define FOLLY_WARN_UNUSED_RESULT
106 # define FOLLY_TARGET_ATTRIBUTE(target)
108 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
111 // detection for 64 bit
112 #if defined(__x86_64__) || defined(_M_X64)
118 #if defined(__aarch64__)
124 #if defined (__powerpc64__)
125 # define FOLLY_PPC64 1
127 # define FOLLY_PPC64 0
130 // packing is very ugly in msvc
132 # define FOLLY_PACK_ATTR /**/
133 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
134 # define FOLLY_PACK_POP __pragma(pack(pop))
135 #elif defined(__clang__) || defined(__GNUC__)
136 # define FOLLY_PACK_ATTR __attribute__((__packed__))
137 # define FOLLY_PACK_PUSH /**/
138 # define FOLLY_PACK_POP /**/
140 # define FOLLY_PACK_ATTR /**/
141 # define FOLLY_PACK_PUSH /**/
142 # define FOLLY_PACK_POP /**/
145 // Generalize warning push/pop.
146 #if defined(_MSC_VER)
147 # define FOLLY_PUSH_WARNING __pragma(warning(push))
148 # define FOLLY_POP_WARNING __pragma(warning(pop))
149 // Disable the GCC warnings.
150 # define FOLLY_GCC_DISABLE_WARNING(warningName)
151 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
152 #elif defined(__clang__) || defined(__GNUC__)
153 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
154 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
155 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
156 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
157 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
158 #define FOLLY_GCC_DISABLE_WARNING(warningName) \
159 _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
160 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
161 // Disable the MSVC warnings.
162 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
164 # define FOLLY_PUSH_WARNING
165 # define FOLLY_POP_WARNING
166 # define FOLLY_GCC_DISABLE_WARNING(warningName)
167 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
170 // portable version check
171 #ifndef __GNUC_PREREQ
172 # if defined __GNUC__ && defined __GNUC_MINOR__
174 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
175 ((maj) << 16) + (min))
178 # define __GNUC_PREREQ(maj, min) 0
182 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
183 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
184 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
185 // Alias it into std (where it's found in 4.9 and later)
186 namespace std { typedef ::max_align_t max_align_t; }
189 // portable version check for clang
190 #ifndef __CLANG_PREREQ
191 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
193 # define __CLANG_PREREQ(maj, min) \
194 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
197 # define __CLANG_PREREQ(maj, min) 0
201 /* Platform specific TLS support
202 * gcc implements __thread
203 * msvc implements __declspec(thread)
204 * the semantics are the same
205 * (but remember __thread has different semantics when using emutls (ex. apple))
207 #if defined(_MSC_VER)
208 # define FOLLY_TLS __declspec(thread)
209 #elif defined(__GNUC__) || defined(__clang__)
210 # define FOLLY_TLS __thread
212 # error cannot define platform specific thread local storage
219 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
220 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
221 // up in a macro to make forward-declarations easier.
224 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
225 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
227 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
228 #define FOLLY_NAMESPACE_STD_END }
231 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
232 // some types, e.g. std::list.
233 #if _GLIBCXX_USE_CXX11_ABI
234 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
235 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
237 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
238 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
241 // Provide our own std::__throw_* wrappers for platforms that don't have them
242 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
243 #include <bits/functexcept.h>
245 #include <folly/detail/FunctionalExcept.h>
248 #if defined(__cplusplus)
249 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
250 // usage of __has_trivial_copy(), so we can't use it as a
251 // least-common-denominator for C++11 implementations that don't support
252 // std::is_trivially_copyable<T>.
254 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
256 // As a result, use std::is_trivially_copyable() where it exists, and fall back
257 // to Boost otherwise.
258 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
259 #include <type_traits>
260 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
261 (std::is_trivially_copyable<T>::value)
263 #include <boost/type_traits.hpp>
264 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
265 (boost::has_trivial_copy<T>::value && \
266 boost::has_trivial_destructor<T>::value)
268 #endif // __cplusplus
270 // MSVC specific defines
271 // mainly for posix compat
273 #include <folly/portability/SysTypes.h>
275 // compiler specific to compiler specific
277 # define __PRETTY_FUNCTION__ __FUNCSIG__
279 // Hide a GCC specific thing that breaks MSVC if left alone.
280 # define __extension__
282 // We have compiler support for the newest of the new, but
283 // MSVC doesn't tell us that.
291 constexpr auto kIsDebug = false;
293 constexpr auto kIsDebug = true;
300 // It's MSVC, so we just have to guess ... and allow an override
301 #ifdef FOLLY_ENDIAN_BE
302 constexpr auto kIsLittleEndian = false;
304 constexpr auto kIsLittleEndian = true;
307 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
309 constexpr auto kIsBigEndian = !kIsLittleEndian;
313 # if defined(__SSE4_2__)
315 # define FOLLY_SSE_MINOR 2
316 # elif defined(__SSE4_1__)
318 # define FOLLY_SSE_MINOR 1
319 # elif defined(__SSE4__)
321 # define FOLLY_SSE_MINOR 0
322 # elif defined(__SSE3__)
324 # define FOLLY_SSE_MINOR 0
325 # elif defined(__SSE2__)
327 # define FOLLY_SSE_MINOR 0
328 # elif defined(__SSE__)
330 # define FOLLY_SSE_MINOR 0
333 # define FOLLY_SSE_MINOR 0
337 #define FOLLY_SSE_PREREQ(major, minor) \
338 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
340 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
341 namespace FOLLY_GFLAGS_NAMESPACE { }
343 using namespace FOLLY_GFLAGS_NAMESPACE;
344 } // namespace gflags
347 // for TARGET_OS_IPHONE
349 #include <TargetConditionals.h>
352 // RTTI may not be enabled for this compilation unit.
353 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
354 (defined(_MSC_VER) && defined(_CPPRTTI))
355 # define FOLLY_HAS_RTTI 1
358 #if defined(__APPLE__) || defined(_MSC_VER)
359 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
361 // 101 is the highest priority allowed by the init_priority attribute.
362 // This priority is already used by JEMalloc and other memory allocators so
363 // we will take the next one.
364 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
369 #if defined(__linux__) && !FOLLY_MOBILE
370 constexpr auto kIsLinux = true;
372 constexpr auto kIsLinux = false;