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 #include <folly/CPortability.h>
29 # include <malloc/malloc.h>
32 #if FOLLY_HAVE_SCHED_H
36 // Unaligned loads and stores
38 #if FOLLY_HAVE_UNALIGNED_ACCESS
39 constexpr bool kHasUnalignedAccess = true;
41 constexpr bool kHasUnalignedAccess = false;
45 // compiler specific attribute translation
46 // msvc should come first, so if clang is in msvc mode it gets the right defines
48 #if defined(__clang__) || defined(__GNUC__)
49 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
50 #elif defined(_MSC_VER)
51 # define FOLLY_ALIGNED(size) __declspec(align(size))
53 # error Cannot define FOLLY_ALIGNED on this platform
55 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
57 // NOTE: this will only do checking in msvc with versions that support /analyze
59 # ifdef _USE_ATTRIBUTES_FOR_SAL
60 # undef _USE_ATTRIBUTES_FOR_SAL
63 # define _USE_ATTRIBUTES_FOR_SAL 1
65 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
66 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
68 # define FOLLY_PRINTF_FORMAT /**/
69 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
70 __attribute__((__format__(__printf__, format_param, dots_param)))
74 #if defined(__clang__) || defined(__GNUC__)
75 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
76 #elif defined(_MSC_VER)
77 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
79 # define FOLLY_DEPRECATED(msg)
84 # define FOLLY_NOINLINE __declspec(noinline)
85 #elif defined(__clang__) || defined(__GNUC__)
86 # define FOLLY_NOINLINE __attribute__((__noinline__))
88 # define FOLLY_NOINLINE
93 # define FOLLY_ALWAYS_INLINE __forceinline
94 #elif defined(__clang__) || defined(__GNUC__)
95 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
97 # define FOLLY_ALWAYS_INLINE inline
100 // detection for 64 bit
101 #if defined(__x86_64__) || defined(_M_X64)
107 #if defined(__aarch64__)
113 #if defined (__powerpc64__)
114 # define FOLLY_PPC64 1
116 # define FOLLY_PPC64 0
119 // packing is very ugly in msvc
121 # define FOLLY_PACK_ATTR /**/
122 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
123 # define FOLLY_PACK_POP __pragma(pack(pop))
124 #elif defined(__clang__) || defined(__GNUC__)
125 # define FOLLY_PACK_ATTR __attribute__((__packed__))
126 # define FOLLY_PACK_PUSH /**/
127 # define FOLLY_PACK_POP /**/
129 # define FOLLY_PACK_ATTR /**/
130 # define FOLLY_PACK_PUSH /**/
131 # define FOLLY_PACK_POP /**/
134 // Generalize warning push/pop.
135 #if defined(_MSC_VER)
136 # define FOLLY_PUSH_WARNING __pragma(warning(push))
137 # define FOLLY_POP_WARNING __pragma(warning(pop))
138 // Disable the GCC warnings.
139 # define FOLLY_GCC_DISABLE_WARNING(warningName)
140 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
141 #elif defined(__clang__) || defined(__GNUC__)
142 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
143 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
144 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
145 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
146 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
147 #define FOLLY_GCC_DISABLE_WARNING(warningName) \
148 _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
149 FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
150 // Disable the MSVC warnings.
151 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
153 # define FOLLY_PUSH_WARNING
154 # define FOLLY_POP_WARNING
155 # define FOLLY_GCC_DISABLE_WARNING(warningName)
156 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
159 // portable version check
160 #ifndef __GNUC_PREREQ
161 # if defined __GNUC__ && defined __GNUC_MINOR__
163 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
164 ((maj) << 16) + (min))
167 # define __GNUC_PREREQ(maj, min) 0
171 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
172 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
173 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
174 // Alias it into std (where it's found in 4.9 and later)
175 namespace std { typedef ::max_align_t max_align_t; }
178 // portable version check for clang
179 #ifndef __CLANG_PREREQ
180 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
182 # define __CLANG_PREREQ(maj, min) \
183 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
186 # define __CLANG_PREREQ(maj, min) 0
190 /* Platform specific TLS support
191 * gcc implements __thread
192 * msvc implements __declspec(thread)
193 * the semantics are the same
194 * (but remember __thread has different semantics when using emutls (ex. apple))
196 #if defined(_MSC_VER)
197 # define FOLLY_TLS __declspec(thread)
198 #elif defined(__GNUC__) || defined(__clang__)
199 # define FOLLY_TLS __thread
201 # error cannot define platform specific thread local storage
208 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
209 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
210 # if defined(__GLIBC_PREREQ)
211 # if __GLIBC_PREREQ(2, 10)
212 # define FOLLY_HAVE_PREADV 1
213 # define FOLLY_HAVE_PWRITEV 1
218 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
219 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
220 // up in a macro to make forward-declarations easier.
223 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
224 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
226 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
227 #define FOLLY_NAMESPACE_STD_END }
230 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
231 // some types, e.g. std::list.
232 #if _GLIBCXX_USE_CXX11_ABI
233 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
234 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
236 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
237 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
240 // Provide our own std::__throw_* wrappers for platforms that don't have them
241 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
242 #include <bits/functexcept.h>
244 #include <folly/detail/FunctionalExcept.h>
247 #if defined(__cplusplus)
248 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
249 // usage of __has_trivial_copy(), so we can't use it as a
250 // least-common-denominator for C++11 implementations that don't support
251 // std::is_trivially_copyable<T>.
253 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
255 // As a result, use std::is_trivially_copyable() where it exists, and fall back
256 // to Boost otherwise.
257 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
258 #include <type_traits>
259 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
260 (std::is_trivially_copyable<T>::value)
262 #include <boost/type_traits.hpp>
263 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
264 (boost::has_trivial_copy<T>::value && \
265 boost::has_trivial_destructor<T>::value)
267 #endif // __cplusplus
269 // MSVC specific defines
270 // mainly for posix compat
272 #include <folly/portability/SysTypes.h>
274 // sprintf semantics are not exactly identical
275 // but current usage is not a problem
276 # define snprintf _snprintf
278 // semantics here are identical
279 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
281 // compiler specific to compiler specific
283 # define __PRETTY_FUNCTION__ __FUNCSIG__
285 // Hide a GCC specific thing that breaks MSVC if left alone.
286 # define __extension__
289 # define FOLLY_SSE _M_IX86_FP
290 # define FOLLY_SSE_MINOR 0
298 constexpr auto kIsDebug = false;
300 constexpr auto kIsDebug = true;
307 // It's MSVC, so we just have to guess ... and allow an override
308 #ifdef FOLLY_ENDIAN_BE
309 constexpr auto kIsLittleEndian = false;
311 constexpr auto kIsLittleEndian = true;
314 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
316 constexpr auto kIsBigEndian = !kIsLittleEndian;
320 # if defined(__SSE4_2__)
322 # define FOLLY_SSE_MINOR 2
323 # elif defined(__SSE4_1__)
325 # define FOLLY_SSE_MINOR 1
326 # elif defined(__SSE4__)
328 # define FOLLY_SSE_MINOR 0
329 # elif defined(__SSE3__)
331 # define FOLLY_SSE_MINOR 0
332 # elif defined(__SSE2__)
334 # define FOLLY_SSE_MINOR 0
335 # elif defined(__SSE__)
337 # define FOLLY_SSE_MINOR 0
340 # define FOLLY_SSE_MINOR 0
344 #define FOLLY_SSE_PREREQ(major, minor) \
345 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
347 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
348 namespace FOLLY_GFLAGS_NAMESPACE { }
350 using namespace FOLLY_GFLAGS_NAMESPACE;
351 } // namespace gflags
354 // for TARGET_OS_IPHONE
356 #include <TargetConditionals.h>
359 // MacOS doesn't have malloc_usable_size()
360 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
361 inline size_t malloc_usable_size(void* ptr) {
362 return malloc_size(ptr);
366 // RTTI may not be enabled for this compilation unit.
367 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
368 (defined(_MSC_VER) && defined(_CPPRTTI))
369 # define FOLLY_HAS_RTTI 1
372 #if defined(__APPLE__) || defined(_MSC_VER)
373 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
375 // 101 is the highest priority allowed by the init_priority attribute.
376 // This priority is already used by JEMalloc and other memory allocators so
377 // we will take the next one.
378 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
381 #endif // FOLLY_PORTABILITY_H_