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_
20 #ifndef FOLLY_NO_CONFIG
21 #include <folly/folly-config.h>
24 #ifdef FOLLY_PLATFORM_CONFIG
25 #include FOLLY_PLATFORM_CONFIG
28 #if FOLLY_HAVE_FEATURES_H
32 #include <folly/CPortability.h>
35 # include <malloc/malloc.h>
38 #if FOLLY_HAVE_SCHED_H
40 #ifndef FOLLY_HAVE_PTHREAD_YIELD
41 #define pthread_yield sched_yield
45 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
46 // MAP_ANON on OSX/BSD.
47 #if defined(__APPLE__) || defined(__FreeBSD__)
51 #define MAP_ANONYMOUS MAP_ANON
56 // MaxAlign: max_align_t isn't supported by gcc
58 struct MaxAlign { char c; } __attribute__((__aligned__));
60 # error Cannot define MaxAlign on this platform
63 // compiler specific attribute translation
64 // msvc should come first, so if clang is in msvc mode it gets the right defines
66 #if defined(__clang__) || defined(__GNUC__)
67 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
68 #elif defined(_MSC_VER)
69 # define FOLLY_ALIGNED(size) __declspec(align(size))
71 # error Cannot define FOLLY_ALIGNED on this platform
73 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(MaxAlign))
75 // NOTE: this will only do checking in msvc with versions that support /analyze
77 # ifdef _USE_ATTRIBUTES_FOR_SAL
78 # undef _USE_ATTRIBUTES_FOR_SAL
81 # define _USE_ATTRIBUTES_FOR_SAL 1
83 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
84 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
86 # define FOLLY_PRINTF_FORMAT /**/
87 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
88 __attribute__((__format__(__printf__, format_param, dots_param)))
92 #if defined(__clang__) || defined(__GNUC__)
93 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
94 #elif defined(_MSC_VER)
95 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
97 # define FOLLY_DEPRECATED(msg)
101 #if defined(_MSC_VER)
102 # define FOLLY_NORETURN __declspec(noreturn)
103 #elif defined(__clang__) || defined(__GNUC__)
104 # define FOLLY_NORETURN __attribute__((__noreturn__))
106 # define FOLLY_NORETURN
111 # define FOLLY_NOINLINE __declspec(noinline)
112 #elif defined(__clang__) || defined(__GNUC__)
113 # define FOLLY_NOINLINE __attribute__((__noinline__))
115 # define FOLLY_NOINLINE
120 # define FOLLY_ALWAYS_INLINE __forceinline
121 #elif defined(__clang__) || defined(__GNUC__)
122 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
124 # define FOLLY_ALWAYS_INLINE
127 // detection for 64 bit
128 #if defined(__x86_64__) || defined(_M_X64)
134 #if defined(__aarch64__)
140 // packing is very ugly in msvc
142 # define FOLLY_PACK_ATTR /**/
143 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
144 # define FOLLY_PACK_POP __pragma(pack(pop))
145 #elif defined(__clang__) || defined(__GNUC__)
146 # define FOLLY_PACK_ATTR __attribute__((__packed__))
147 # define FOLLY_PACK_PUSH /**/
148 # define FOLLY_PACK_POP /**/
150 # define FOLLY_PACK_ATTR /**/
151 # define FOLLY_PACK_PUSH /**/
152 # define FOLLY_PACK_POP /**/
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
168 /* Define macro wrappers for C++11's "final" and "override" keywords, which
169 * are supported in gcc 4.7 but not gcc 4.6. */
170 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
171 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
172 # define FOLLY_FINAL final
173 # define FOLLY_OVERRIDE override
174 # elif defined(_MSC_VER) && _MSC_VER >= 1600
175 # define FOLLY_FINAL final
176 # define FOLLY_OVERRIDE override
178 # define FOLLY_FINAL /**/
179 # define FOLLY_OVERRIDE /**/
183 /* Platform specific TLS support
184 * gcc implements __thread
185 * msvc implements __declspec(thread)
186 * the semantics are the same
187 * (but remember __thread has different semantics when using emutls (ex. apple))
189 #if defined(_MSC_VER)
190 # define FOLLY_TLS __declspec(thread)
191 #elif defined(__GNUC__) || defined(__clang__)
192 # define FOLLY_TLS __thread
194 # error cannot define platform specific thread local storage
197 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
198 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
199 # if defined(__GLIBC_PREREQ)
200 # if __GLIBC_PREREQ(2, 10)
201 # define FOLLY_HAVE_PREADV 1
202 # define FOLLY_HAVE_PWRITEV 1
207 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
208 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
209 // up in a macro to make forward-declarations easier.
212 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
213 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
215 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
216 #define FOLLY_NAMESPACE_STD_END }
219 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
220 // versions of these into the global namespace.
221 #if FOLLY_HAVE_CLOCK_GETTIME
224 #include <folly/detail/Clock.h>
227 // Provide our own std::__throw_* wrappers for platforms that don't have them
228 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
229 #include <bits/functexcept.h>
231 #include <folly/detail/FunctionalExcept.h>
234 #if defined(__cplusplus)
235 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
236 // usage of __has_trivial_copy(), so we can't use it as a
237 // least-common-denominator for C++11 implementations that don't support
238 // std::is_trivially_copyable<T>.
240 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
242 // As a result, use std::is_trivially_copyable() where it exists, and fall back
243 // to Boost otherwise.
244 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
245 #include <type_traits>
246 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
247 (std::is_trivially_copyable<T>::value)
249 #include <boost/type_traits.hpp>
250 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
251 (boost::has_trivial_copy<T>::value && \
252 boost::has_trivial_destructor<T>::value)
254 #endif // __cplusplus
256 // MSVC specific defines
257 // mainly for posix compat
260 // this definition is in a really silly place with a silly name
261 // and ifdefing it every time we want it is painful
263 typedef SSIZE_T ssize_t;
265 // sprintf semantics are not exactly identical
266 // but current usage is not a problem
267 # define snprintf _snprintf
269 // semantics here are identical
270 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
272 // compiler specific to compiler specific
274 # define __PRETTY_FUNCTION__ __FUNCSIG__
276 // Hide a GCC specific thing that breaks MSVC if left alone.
277 # define __extension__
280 # define FOLLY_SSE _M_IX86_FP
281 # define FOLLY_SSE_MINOR 0
287 # if defined(__SSE4_2__)
289 # define FOLLY_SSE_MINOR 2
290 # elif defined(__SSE4_1__)
292 # define FOLLY_SSE_MINOR 1
293 # elif defined(__SSE4__)
295 # define FOLLY_SSE_MINOR 0
296 # elif defined(__SSE3__)
298 # define FOLLY_SSE_MINOR 0
299 # elif defined(__SSE2__)
301 # define FOLLY_SSE_MINOR 0
302 # elif defined(__SSE__)
304 # define FOLLY_SSE_MINOR 0
307 # define FOLLY_SSE_MINOR 0
311 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
312 namespace FOLLY_GFLAGS_NAMESPACE { }
314 using namespace FOLLY_GFLAGS_NAMESPACE;
315 } // namespace gflags
318 // for TARGET_OS_IPHONE
320 #include <TargetConditionals.h>
323 // MacOS doesn't have malloc_usable_size()
324 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
325 inline size_t malloc_usable_size(void* ptr) {
326 return malloc_size(ptr);
330 // RTTI may not be enabled for this compilation unit.
331 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
332 (defined(_MSC_VER) && defined(_CPPRTTI))
333 # define FOLLY_HAS_RTTI 1
342 inline void asm_volatile_memory() {
343 #if defined(__clang__) || defined(__GNUC__)
344 asm volatile("" : : : "memory");
345 #elif defined(_MSC_VER)
346 ::_ReadWriteBarrier();
350 inline void asm_volatile_pause() {
351 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
353 #elif defined(__i386__) || FOLLY_X64
354 asm volatile ("pause");
356 asm volatile ("wfe");
359 inline void asm_pause() {
360 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
362 #elif defined(__i386__) || FOLLY_X64
371 #endif // FOLLY_PORTABILITY_H_