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 // Break caches 20160401 (#8764509) @nocommit
24 #include <folly/portability/Config.h>
26 #include <folly/CPortability.h>
28 #if FOLLY_HAVE_SCHED_H
32 // Unaligned loads and stores
34 #if FOLLY_HAVE_UNALIGNED_ACCESS
35 constexpr bool kHasUnalignedAccess = true;
37 constexpr bool kHasUnalignedAccess = false;
41 // compiler specific attribute translation
42 // msvc should come first, so if clang is in msvc mode it gets the right defines
44 #if defined(__clang__) || defined(__GNUC__)
45 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
46 #elif defined(_MSC_VER)
47 # define FOLLY_ALIGNED(size) __declspec(align(size))
49 # error Cannot define FOLLY_ALIGNED on this platform
51 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
53 // NOTE: this will only do checking in msvc with versions that support /analyze
55 # ifdef _USE_ATTRIBUTES_FOR_SAL
56 # undef _USE_ATTRIBUTES_FOR_SAL
59 # define _USE_ATTRIBUTES_FOR_SAL 1
61 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
62 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
64 # define FOLLY_PRINTF_FORMAT /**/
65 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
66 __attribute__((__format__(__printf__, format_param, dots_param)))
70 #if defined(__clang__) || defined(__GNUC__)
71 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
72 #elif defined(_MSC_VER)
73 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
75 # define FOLLY_DEPRECATED(msg)
80 # define FOLLY_NOINLINE __declspec(noinline)
81 #elif defined(__clang__) || defined(__GNUC__)
82 # define FOLLY_NOINLINE __attribute__((__noinline__))
84 # define FOLLY_NOINLINE
89 # define FOLLY_ALWAYS_INLINE __forceinline
90 #elif defined(__clang__) || defined(__GNUC__)
91 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
93 # define FOLLY_ALWAYS_INLINE inline
96 // detection for 64 bit
97 #if defined(__x86_64__) || defined(_M_X64)
103 #if defined(__aarch64__)
109 #if defined (__powerpc64__)
110 # define FOLLY_PPC64 1
112 # define FOLLY_PPC64 0
115 // packing is very ugly in msvc
117 # define FOLLY_PACK_ATTR /**/
118 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
119 # define FOLLY_PACK_POP __pragma(pack(pop))
120 #elif defined(__clang__) || defined(__GNUC__)
121 # define FOLLY_PACK_ATTR __attribute__((__packed__))
122 # define FOLLY_PACK_PUSH /**/
123 # define FOLLY_PACK_POP /**/
125 # define FOLLY_PACK_ATTR /**/
126 # define FOLLY_PACK_PUSH /**/
127 # define FOLLY_PACK_POP /**/
130 // Generalize warning push/pop.
131 #if defined(_MSC_VER)
132 # define FOLLY_PUSH_WARNING __pragma(warning(push))
133 # define FOLLY_POP_WARNING __pragma(warning(pop))
134 // Disable the GCC warnings.
135 # define FOLLY_GCC_DISABLE_WARNING(warningName)
136 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
137 #elif defined(__clang__) || defined(__GNUC__)
138 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
139 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
140 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
141 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
142 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
143 #define FOLLY_GCC_DISABLE_WARNING(warningName) \
144 _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
145 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
146 // Disable the MSVC warnings.
147 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
149 # define FOLLY_PUSH_WARNING
150 # define FOLLY_POP_WARNING
151 # define FOLLY_GCC_DISABLE_WARNING(warningName)
152 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
155 // portable version check
156 #ifndef __GNUC_PREREQ
157 # if defined __GNUC__ && defined __GNUC_MINOR__
159 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
160 ((maj) << 16) + (min))
163 # define __GNUC_PREREQ(maj, min) 0
167 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
168 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
169 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
170 // Alias it into std (where it's found in 4.9 and later)
171 namespace std { typedef ::max_align_t max_align_t; }
174 // portable version check for clang
175 #ifndef __CLANG_PREREQ
176 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
178 # define __CLANG_PREREQ(maj, min) \
179 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
182 # define __CLANG_PREREQ(maj, min) 0
186 /* Platform specific TLS support
187 * gcc implements __thread
188 * msvc implements __declspec(thread)
189 * the semantics are the same
190 * (but remember __thread has different semantics when using emutls (ex. apple))
192 #if defined(_MSC_VER)
193 # define FOLLY_TLS __declspec(thread)
194 #elif defined(__GNUC__) || defined(__clang__)
195 # define FOLLY_TLS __thread
197 # error cannot define platform specific thread local storage
204 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
205 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
206 # if defined(__GLIBC_PREREQ)
207 # if __GLIBC_PREREQ(2, 10)
208 # define FOLLY_HAVE_PREADV 1
209 # define FOLLY_HAVE_PWRITEV 1
214 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
215 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
216 // up in a macro to make forward-declarations easier.
219 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
220 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
222 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
223 #define FOLLY_NAMESPACE_STD_END }
226 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
227 // some types, e.g. std::list.
228 #if _GLIBCXX_USE_CXX11_ABI
229 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
230 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
232 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
233 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
236 // Provide our own std::__throw_* wrappers for platforms that don't have them
237 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
238 #include <bits/functexcept.h>
240 #include <folly/detail/FunctionalExcept.h>
243 #if defined(__cplusplus)
244 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
245 // usage of __has_trivial_copy(), so we can't use it as a
246 // least-common-denominator for C++11 implementations that don't support
247 // std::is_trivially_copyable<T>.
249 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
251 // As a result, use std::is_trivially_copyable() where it exists, and fall back
252 // to Boost otherwise.
253 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
254 #include <type_traits>
255 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
256 (std::is_trivially_copyable<T>::value)
258 #include <boost/type_traits.hpp>
259 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
260 (boost::has_trivial_copy<T>::value && \
261 boost::has_trivial_destructor<T>::value)
263 #endif // __cplusplus
265 // MSVC specific defines
266 // mainly for posix compat
268 #include <folly/portability/SysTypes.h>
270 // sprintf semantics are not exactly identical
271 // but current usage is not a problem
272 # define snprintf _snprintf
274 // semantics here are identical
275 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
277 // compiler specific to compiler specific
279 # define __PRETTY_FUNCTION__ __FUNCSIG__
281 // Hide a GCC specific thing that breaks MSVC if left alone.
282 # define __extension__
284 // We have compiler support for the newest of the new, but
285 // MSVC doesn't tell us that.
293 constexpr auto kIsDebug = false;
295 constexpr auto kIsDebug = true;
302 // It's MSVC, so we just have to guess ... and allow an override
303 #ifdef FOLLY_ENDIAN_BE
304 constexpr auto kIsLittleEndian = false;
306 constexpr auto kIsLittleEndian = true;
309 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
311 constexpr auto kIsBigEndian = !kIsLittleEndian;
315 # if defined(__SSE4_2__)
317 # define FOLLY_SSE_MINOR 2
318 # elif defined(__SSE4_1__)
320 # define FOLLY_SSE_MINOR 1
321 # elif defined(__SSE4__)
323 # define FOLLY_SSE_MINOR 0
324 # elif defined(__SSE3__)
326 # define FOLLY_SSE_MINOR 0
327 # elif defined(__SSE2__)
329 # define FOLLY_SSE_MINOR 0
330 # elif defined(__SSE__)
332 # define FOLLY_SSE_MINOR 0
335 # define FOLLY_SSE_MINOR 0
339 #define FOLLY_SSE_PREREQ(major, minor) \
340 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
342 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
343 namespace FOLLY_GFLAGS_NAMESPACE { }
345 using namespace FOLLY_GFLAGS_NAMESPACE;
346 } // namespace gflags
349 // for TARGET_OS_IPHONE
351 #include <TargetConditionals.h>
354 // RTTI may not be enabled for this compilation unit.
355 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
356 (defined(_MSC_VER) && defined(_CPPRTTI))
357 # define FOLLY_HAS_RTTI 1
360 #if defined(__APPLE__) || defined(_MSC_VER)
361 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
363 // 101 is the highest priority allowed by the init_priority attribute.
364 // This priority is already used by JEMalloc and other memory allocators so
365 // we will take the next one.
366 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))