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_
24 #ifndef FOLLY_NO_CONFIG
25 #include <folly/folly-config.h>
28 #ifdef FOLLY_PLATFORM_CONFIG
29 #include FOLLY_PLATFORM_CONFIG
32 #if FOLLY_HAVE_FEATURES_H
36 #include <folly/CPortability.h>
39 # include <malloc/malloc.h>
42 #if FOLLY_HAVE_SCHED_H
44 #ifndef FOLLY_HAVE_PTHREAD_YIELD
45 #define pthread_yield sched_yield
49 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
50 // MAP_ANON on OSX/BSD.
51 #if defined(__APPLE__) || defined(__FreeBSD__)
55 #define MAP_ANONYMOUS MAP_ANON
60 // compiler specific attribute translation
61 // msvc should come first, so if clang is in msvc mode it gets the right defines
63 #if defined(__clang__) || defined(__GNUC__)
64 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
65 #elif defined(_MSC_VER)
66 # define FOLLY_ALIGNED(size) __declspec(align(size))
68 # error Cannot define FOLLY_ALIGNED on this platform
70 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
72 // NOTE: this will only do checking in msvc with versions that support /analyze
74 # ifdef _USE_ATTRIBUTES_FOR_SAL
75 # undef _USE_ATTRIBUTES_FOR_SAL
78 # define _USE_ATTRIBUTES_FOR_SAL 1
80 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
81 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
83 # define FOLLY_PRINTF_FORMAT /**/
84 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
85 __attribute__((__format__(__printf__, format_param, dots_param)))
89 #if defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
91 #elif defined(_MSC_VER)
92 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
94 # define FOLLY_DEPRECATED(msg)
99 # define FOLLY_NORETURN __declspec(noreturn)
100 #elif defined(__clang__) || defined(__GNUC__)
101 # define FOLLY_NORETURN __attribute__((__noreturn__))
103 # define FOLLY_NORETURN
108 # define FOLLY_NOINLINE __declspec(noinline)
109 #elif defined(__clang__) || defined(__GNUC__)
110 # define FOLLY_NOINLINE __attribute__((__noinline__))
112 # define FOLLY_NOINLINE
117 # define FOLLY_ALWAYS_INLINE __forceinline
118 #elif defined(__clang__) || defined(__GNUC__)
119 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
121 # define FOLLY_ALWAYS_INLINE inline
124 // detection for 64 bit
125 #if defined(__x86_64__) || defined(_M_X64)
131 #if defined(__aarch64__)
137 #if defined (__powerpc64__)
138 # define FOLLY_PPC64 1
140 # define FOLLY_PPC64 0
143 // packing is very ugly in msvc
145 # define FOLLY_PACK_ATTR /**/
146 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
147 # define FOLLY_PACK_POP __pragma(pack(pop))
148 #elif defined(__clang__) || defined(__GNUC__)
149 # define FOLLY_PACK_ATTR __attribute__((__packed__))
150 # define FOLLY_PACK_PUSH /**/
151 # define FOLLY_PACK_POP /**/
153 # define FOLLY_PACK_ATTR /**/
154 # define FOLLY_PACK_PUSH /**/
155 # define FOLLY_PACK_POP /**/
158 // portable version check
159 #ifndef __GNUC_PREREQ
160 # if defined __GNUC__ && defined __GNUC_MINOR__
162 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
163 ((maj) << 16) + (min))
166 # define __GNUC_PREREQ(maj, min) 0
170 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
171 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
172 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
173 // Alias it into std (where it's found in 4.9 and later)
174 namespace std { typedef ::max_align_t max_align_t; }
177 /* Platform specific TLS support
178 * gcc implements __thread
179 * msvc implements __declspec(thread)
180 * the semantics are the same
181 * (but remember __thread has different semantics when using emutls (ex. apple))
183 #if defined(_MSC_VER)
184 # define FOLLY_TLS __declspec(thread)
185 #elif defined(__GNUC__) || defined(__clang__)
186 # define FOLLY_TLS __thread
188 # error cannot define platform specific thread local storage
191 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
192 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
193 # if defined(__GLIBC_PREREQ)
194 # if __GLIBC_PREREQ(2, 10)
195 # define FOLLY_HAVE_PREADV 1
196 # define FOLLY_HAVE_PWRITEV 1
201 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
202 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
203 // up in a macro to make forward-declarations easier.
206 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
207 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
209 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
210 #define FOLLY_NAMESPACE_STD_END }
213 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
214 // some types, e.g. std::list.
215 #if _GLIBCXX_USE_CXX11_ABI
216 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
217 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
219 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
220 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
223 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
224 // versions of these into the global namespace.
225 #if FOLLY_HAVE_CLOCK_GETTIME
228 #include <folly/detail/Clock.h>
231 // Provide our own std::__throw_* wrappers for platforms that don't have them
232 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
233 #include <bits/functexcept.h>
235 #include <folly/detail/FunctionalExcept.h>
238 #if defined(__cplusplus)
239 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
240 // usage of __has_trivial_copy(), so we can't use it as a
241 // least-common-denominator for C++11 implementations that don't support
242 // std::is_trivially_copyable<T>.
244 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
246 // As a result, use std::is_trivially_copyable() where it exists, and fall back
247 // to Boost otherwise.
248 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
249 #include <type_traits>
250 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
251 (std::is_trivially_copyable<T>::value)
253 #include <boost/type_traits.hpp>
254 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
255 (boost::has_trivial_copy<T>::value && \
256 boost::has_trivial_destructor<T>::value)
258 #endif // __cplusplus
260 // MSVC specific defines
261 // mainly for posix compat
264 // this definition is in a really silly place with a silly name
265 // and ifdefing it every time we want it is painful
267 typedef SSIZE_T ssize_t;
269 // sprintf semantics are not exactly identical
270 // but current usage is not a problem
271 # define snprintf _snprintf
273 // semantics here are identical
274 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
276 // compiler specific to compiler specific
278 # define __PRETTY_FUNCTION__ __FUNCSIG__
280 // Hide a GCC specific thing that breaks MSVC if left alone.
281 # define __extension__
284 # define FOLLY_SSE _M_IX86_FP
285 # define FOLLY_SSE_MINOR 0
291 # if defined(__SSE4_2__)
293 # define FOLLY_SSE_MINOR 2
294 # elif defined(__SSE4_1__)
296 # define FOLLY_SSE_MINOR 1
297 # elif defined(__SSE4__)
299 # define FOLLY_SSE_MINOR 0
300 # elif defined(__SSE3__)
302 # define FOLLY_SSE_MINOR 0
303 # elif defined(__SSE2__)
305 # define FOLLY_SSE_MINOR 0
306 # elif defined(__SSE__)
308 # define FOLLY_SSE_MINOR 0
311 # define FOLLY_SSE_MINOR 0
315 #define FOLLY_SSE_PREREQ(major, minor) \
316 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
318 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
319 namespace FOLLY_GFLAGS_NAMESPACE { }
321 using namespace FOLLY_GFLAGS_NAMESPACE;
322 } // namespace gflags
325 // for TARGET_OS_IPHONE
327 #include <TargetConditionals.h>
330 // MacOS doesn't have malloc_usable_size()
331 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
332 inline size_t malloc_usable_size(void* ptr) {
333 return malloc_size(ptr);
337 // RTTI may not be enabled for this compilation unit.
338 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
339 (defined(_MSC_VER) && defined(_CPPRTTI))
340 # define FOLLY_HAS_RTTI 1
349 inline void asm_volatile_memory() {
350 #if defined(__clang__) || defined(__GNUC__)
351 asm volatile("" : : : "memory");
352 #elif defined(_MSC_VER)
353 ::_ReadWriteBarrier();
357 inline void asm_volatile_pause() {
358 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
360 #elif defined(__i386__) || FOLLY_X64
361 asm volatile ("pause");
363 asm volatile ("wfe");
365 asm volatile("or 27,27,27");
368 inline void asm_pause() {
369 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
371 #elif defined(__i386__) || FOLLY_X64
380 constexpr size_t constexpr_strlen(const char* s) {
381 #if defined(__clang__)
382 return __builtin_strlen(s);
389 #endif // FOLLY_PORTABILITY_H_