2 * Copyright 2015 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_
22 #ifndef FOLLY_NO_CONFIG
23 #include <folly/folly-config.h>
26 #ifdef FOLLY_PLATFORM_CONFIG
27 #include FOLLY_PLATFORM_CONFIG
30 #if FOLLY_HAVE_FEATURES_H
34 #include <folly/CPortability.h>
37 # include <malloc/malloc.h>
40 #if FOLLY_HAVE_SCHED_H
42 #ifndef FOLLY_HAVE_PTHREAD_YIELD
43 #define pthread_yield sched_yield
47 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
48 // MAP_ANON on OSX/BSD.
49 #if defined(__APPLE__) || defined(__FreeBSD__)
53 #define MAP_ANONYMOUS MAP_ANON
58 // compiler specific attribute translation
59 // msvc should come first, so if clang is in msvc mode it gets the right defines
61 #if defined(__clang__) || defined(__GNUC__)
62 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
63 #elif defined(_MSC_VER)
64 # define FOLLY_ALIGNED(size) __declspec(align(size))
66 # error Cannot define FOLLY_ALIGNED on this platform
68 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
70 // NOTE: this will only do checking in msvc with versions that support /analyze
72 # ifdef _USE_ATTRIBUTES_FOR_SAL
73 # undef _USE_ATTRIBUTES_FOR_SAL
76 # define _USE_ATTRIBUTES_FOR_SAL 1
78 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
79 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
81 # define FOLLY_PRINTF_FORMAT /**/
82 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
83 __attribute__((__format__(__printf__, format_param, dots_param)))
87 #if defined(__clang__) || defined(__GNUC__)
88 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
89 #elif defined(_MSC_VER)
90 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
92 # define FOLLY_DEPRECATED(msg)
97 # define FOLLY_NORETURN __declspec(noreturn)
98 #elif defined(__clang__) || defined(__GNUC__)
99 # define FOLLY_NORETURN __attribute__((__noreturn__))
101 # define FOLLY_NORETURN
106 # define FOLLY_NOINLINE __declspec(noinline)
107 #elif defined(__clang__) || defined(__GNUC__)
108 # define FOLLY_NOINLINE __attribute__((__noinline__))
110 # define FOLLY_NOINLINE
115 # define FOLLY_ALWAYS_INLINE __forceinline
116 #elif defined(__clang__) || defined(__GNUC__)
117 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
119 # define FOLLY_ALWAYS_INLINE inline
122 // detection for 64 bit
123 #if defined(__x86_64__) || defined(_M_X64)
129 #if defined(__aarch64__)
135 #if defined (__powerpc64__)
136 # define FOLLY_PPC64 1
138 # define FOLLY_PPC64 0
141 // packing is very ugly in msvc
143 # define FOLLY_PACK_ATTR /**/
144 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
145 # define FOLLY_PACK_POP __pragma(pack(pop))
146 #elif defined(__clang__) || defined(__GNUC__)
147 # define FOLLY_PACK_ATTR __attribute__((__packed__))
148 # define FOLLY_PACK_PUSH /**/
149 # define FOLLY_PACK_POP /**/
151 # define FOLLY_PACK_ATTR /**/
152 # define FOLLY_PACK_PUSH /**/
153 # define FOLLY_PACK_POP /**/
156 // portable version check
157 #ifndef __GNUC_PREREQ
158 # if defined __GNUC__ && defined __GNUC_MINOR__
160 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
161 ((maj) << 16) + (min))
164 # define __GNUC_PREREQ(maj, min) 0
168 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
169 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
170 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
171 // Alias it into std (where it's found in 4.9 and later)
172 namespace std { typedef ::max_align_t max_align_t; }
175 /* Platform specific TLS support
176 * gcc implements __thread
177 * msvc implements __declspec(thread)
178 * the semantics are the same
179 * (but remember __thread has different semantics when using emutls (ex. apple))
181 #if defined(_MSC_VER)
182 # define FOLLY_TLS __declspec(thread)
183 #elif defined(__GNUC__) || defined(__clang__)
184 # define FOLLY_TLS __thread
186 # error cannot define platform specific thread local storage
189 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
190 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
191 # if defined(__GLIBC_PREREQ)
192 # if __GLIBC_PREREQ(2, 10)
193 # define FOLLY_HAVE_PREADV 1
194 # define FOLLY_HAVE_PWRITEV 1
199 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
200 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
201 // up in a macro to make forward-declarations easier.
204 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
205 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
207 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
208 #define FOLLY_NAMESPACE_STD_END }
211 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
212 // some types, e.g. std::list.
213 #if _GLIBCXX_USE_CXX11_ABI
214 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
215 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
217 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
218 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
221 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
222 // versions of these into the global namespace.
223 #if FOLLY_HAVE_CLOCK_GETTIME
226 #include <folly/detail/Clock.h>
229 // Provide our own std::__throw_* wrappers for platforms that don't have them
230 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
231 #include <bits/functexcept.h>
233 #include <folly/detail/FunctionalExcept.h>
236 #if defined(__cplusplus)
237 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
238 // usage of __has_trivial_copy(), so we can't use it as a
239 // least-common-denominator for C++11 implementations that don't support
240 // std::is_trivially_copyable<T>.
242 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
244 // As a result, use std::is_trivially_copyable() where it exists, and fall back
245 // to Boost otherwise.
246 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
247 #include <type_traits>
248 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
249 (std::is_trivially_copyable<T>::value)
251 #include <boost/type_traits.hpp>
252 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
253 (boost::has_trivial_copy<T>::value && \
254 boost::has_trivial_destructor<T>::value)
256 #endif // __cplusplus
258 // MSVC specific defines
259 // mainly for posix compat
262 // this definition is in a really silly place with a silly name
263 // and ifdefing it every time we want it is painful
265 typedef SSIZE_T ssize_t;
267 // sprintf semantics are not exactly identical
268 // but current usage is not a problem
269 # define snprintf _snprintf
271 // semantics here are identical
272 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
274 // compiler specific to compiler specific
276 # define __PRETTY_FUNCTION__ __FUNCSIG__
278 // Hide a GCC specific thing that breaks MSVC if left alone.
279 # define __extension__
282 # define FOLLY_SSE _M_IX86_FP
283 # define FOLLY_SSE_MINOR 0
289 # if defined(__SSE4_2__)
291 # define FOLLY_SSE_MINOR 2
292 # elif defined(__SSE4_1__)
294 # define FOLLY_SSE_MINOR 1
295 # elif defined(__SSE4__)
297 # define FOLLY_SSE_MINOR 0
298 # elif defined(__SSE3__)
300 # define FOLLY_SSE_MINOR 0
301 # elif defined(__SSE2__)
303 # define FOLLY_SSE_MINOR 0
304 # elif defined(__SSE__)
306 # define FOLLY_SSE_MINOR 0
309 # define FOLLY_SSE_MINOR 0
313 #define FOLLY_SSE_PREREQ(major, minor) \
314 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
316 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
317 namespace FOLLY_GFLAGS_NAMESPACE { }
319 using namespace FOLLY_GFLAGS_NAMESPACE;
320 } // namespace gflags
323 // for TARGET_OS_IPHONE
325 #include <TargetConditionals.h>
328 // MacOS doesn't have malloc_usable_size()
329 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
330 inline size_t malloc_usable_size(void* ptr) {
331 return malloc_size(ptr);
335 // RTTI may not be enabled for this compilation unit.
336 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
337 (defined(_MSC_VER) && defined(_CPPRTTI))
338 # define FOLLY_HAS_RTTI 1
347 inline void asm_volatile_memory() {
348 #if defined(__clang__) || defined(__GNUC__)
349 asm volatile("" : : : "memory");
350 #elif defined(_MSC_VER)
351 ::_ReadWriteBarrier();
355 inline void asm_volatile_pause() {
356 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
358 #elif defined(__i386__) || FOLLY_X64
359 asm volatile ("pause");
361 asm volatile ("wfe");
363 asm volatile("or 27,27,27");
366 inline void asm_pause() {
367 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
369 #elif defined(__i386__) || FOLLY_X64
380 #endif // FOLLY_PORTABILITY_H_