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 // NOTE: this will only do checking in msvc with versions that support /analyze
68 # ifdef _USE_ATTRIBUTES_FOR_SAL
69 # undef _USE_ATTRIBUTES_FOR_SAL
72 # define _USE_ATTRIBUTES_FOR_SAL 1
74 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
75 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
77 # define FOLLY_PRINTF_FORMAT /**/
78 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
79 __attribute__((__format__(__printf__, format_param, dots_param)))
83 #if defined(__clang__) || defined(__GNUC__)
84 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
86 # define FOLLY_DEPRECATED
91 # define FOLLY_NORETURN __declspec(noreturn)
92 #elif defined(__clang__) || defined(__GNUC__)
93 # define FOLLY_NORETURN __attribute__((__noreturn__))
95 # define FOLLY_NORETURN
100 # define FOLLY_NOINLINE __declspec(noinline)
101 #elif defined(__clang__) || defined(__GNUC__)
102 # define FOLLY_NOINLINE __attribute__((__noinline__))
104 # define FOLLY_NOINLINE
109 # define FOLLY_ALWAYS_INLINE __forceinline
110 #elif defined(__clang__) || defined(__GNUC__)
111 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
113 # define FOLLY_ALWAYS_INLINE
116 // detection for 64 bit
117 #if defined(__x86_64__) || defined(_M_X64)
123 #if defined(__aarch64__)
129 // packing is very ugly in msvc
131 # define FOLLY_PACK_ATTR /**/
132 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
133 # define FOLLY_PACK_POP __pragma(pack(pop))
134 #elif defined(__clang__) || defined(__GNUC__)
135 # define FOLLY_PACK_ATTR __attribute__((__packed__))
136 # define FOLLY_PACK_PUSH /**/
137 # define FOLLY_PACK_POP /**/
139 # define FOLLY_PACK_ATTR /**/
140 # define FOLLY_PACK_PUSH /**/
141 # define FOLLY_PACK_POP /**/
144 // portable version check
145 #ifndef __GNUC_PREREQ
146 # if defined __GNUC__ && defined __GNUC_MINOR__
148 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
149 ((maj) << 16) + (min))
152 # define __GNUC_PREREQ(maj, min) 0
157 /* Define macro wrappers for C++11's "final" and "override" keywords, which
158 * are supported in gcc 4.7 but not gcc 4.6. */
159 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
160 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
161 # define FOLLY_FINAL final
162 # define FOLLY_OVERRIDE override
164 # define FOLLY_FINAL /**/
165 # define FOLLY_OVERRIDE /**/
169 /* Platform specific TLS support
170 * gcc implements __thread
171 * msvc implements __declspec(thread)
172 * the semantics are the same
173 * (but remember __thread has different semantics when using emutls (ex. apple))
175 #if defined(_MSC_VER)
176 # define FOLLY_TLS __declspec(thread)
177 #elif defined(__GNUC__) || defined(__clang__)
178 # define FOLLY_TLS __thread
180 # error cannot define platform specific thread local storage
183 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
184 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
185 # if defined(__GLIBC_PREREQ)
186 # if __GLIBC_PREREQ(2, 10)
187 # define FOLLY_HAVE_PREADV 1
188 # define FOLLY_HAVE_PWRITEV 1
193 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
194 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
195 // up in a macro to make forward-declarations easier.
198 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
199 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
201 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
202 #define FOLLY_NAMESPACE_STD_END }
205 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
206 // versions of these into the global namespace.
207 #if FOLLY_HAVE_CLOCK_GETTIME
210 #include <folly/detail/Clock.h>
213 // Provide our own std::__throw_* wrappers for platforms that don't have them
214 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
215 #include <bits/functexcept.h>
217 #include <folly/detail/FunctionalExcept.h>
220 #if defined(__cplusplus)
221 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
222 // usage of __has_trivial_copy(), so we can't use it as a
223 // least-common-denominator for C++11 implementations that don't support
224 // std::is_trivially_copyable<T>.
226 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
228 // As a result, use std::is_trivially_copyable() where it exists, and fall back
229 // to Boost otherwise.
230 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
231 #include <type_traits>
232 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
233 (std::is_trivially_copyable<T>::value)
235 #include <boost/type_traits.hpp>
236 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
237 (boost::has_trivial_copy<T>::value && \
238 boost::has_trivial_destructor<T>::value)
240 #endif // __cplusplus
242 // MSVC specific defines
243 // mainly for posix compat
246 // this definition is in a really silly place with a silly name
247 // and ifdefing it every time we want it is painful
249 typedef SSIZE_T ssize_t;
251 // sprintf semantics are not exactly identical
252 // but current usage is not a problem
253 # define snprintf _snprintf
255 // semantics here are identical
256 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
258 // compiler specific to compiler specific
260 # define __PRETTY_FUNCTION__ __FUNCSIG__
263 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
264 namespace FOLLY_GFLAGS_NAMESPACE { }
266 using namespace FOLLY_GFLAGS_NAMESPACE;
267 } // namespace gflags
270 // for TARGET_OS_IPHONE
272 #include <TargetConditionals.h>
275 // MacOS doesn't have malloc_usable_size()
276 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
277 inline size_t malloc_usable_size(void* ptr) {
278 return malloc_size(ptr);
282 // RTTI may not be enabled for this compilation unit.
283 #if defined(__GXX_RTTI) || defined(__cpp_rtti)
284 # define FOLLY_HAS_RTTI 1
289 inline void asm_volatile_pause() {
290 #if defined(__i386__) || FOLLY_X64
291 asm volatile ("pause");
293 asm volatile ("wfe");
296 inline void asm_pause() {
297 #if defined(__i386__) || FOLLY_X64
306 #endif // FOLLY_PORTABILITY_H_