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 // @nocommit invalidate ccache 20151125 (see #8764509)
26 #ifndef FOLLY_NO_CONFIG
27 #include <folly/folly-config.h>
30 #ifdef FOLLY_PLATFORM_CONFIG
31 #include FOLLY_PLATFORM_CONFIG
34 #if FOLLY_HAVE_FEATURES_H
38 #include <folly/CPortability.h>
41 # include <malloc/malloc.h>
44 #if FOLLY_HAVE_SCHED_H
46 #ifndef FOLLY_HAVE_PTHREAD_YIELD
47 #define pthread_yield sched_yield
51 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
52 // MAP_ANON on OSX/BSD.
53 #if defined(__APPLE__) || defined(__FreeBSD__)
57 #define MAP_ANONYMOUS MAP_ANON
62 // compiler specific attribute translation
63 // msvc should come first, so if clang is in msvc mode it gets the right defines
65 #if defined(__clang__) || defined(__GNUC__)
66 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
67 #elif defined(_MSC_VER)
68 # define FOLLY_ALIGNED(size) __declspec(align(size))
70 # error Cannot define FOLLY_ALIGNED on this platform
72 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
74 // NOTE: this will only do checking in msvc with versions that support /analyze
76 # ifdef _USE_ATTRIBUTES_FOR_SAL
77 # undef _USE_ATTRIBUTES_FOR_SAL
80 # define _USE_ATTRIBUTES_FOR_SAL 1
82 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
83 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
85 # define FOLLY_PRINTF_FORMAT /**/
86 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
87 __attribute__((__format__(__printf__, format_param, dots_param)))
91 #if defined(__clang__) || defined(__GNUC__)
92 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
93 #elif defined(_MSC_VER)
94 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
96 # define FOLLY_DEPRECATED(msg)
100 #if defined(_MSC_VER)
101 # define FOLLY_NORETURN __declspec(noreturn)
102 #elif defined(__clang__) || defined(__GNUC__)
103 # define FOLLY_NORETURN __attribute__((__noreturn__))
105 # define FOLLY_NORETURN
110 # define FOLLY_NOINLINE __declspec(noinline)
111 #elif defined(__clang__) || defined(__GNUC__)
112 # define FOLLY_NOINLINE __attribute__((__noinline__))
114 # define FOLLY_NOINLINE
119 # define FOLLY_ALWAYS_INLINE __forceinline
120 #elif defined(__clang__) || defined(__GNUC__)
121 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
123 # define FOLLY_ALWAYS_INLINE inline
126 // detection for 64 bit
127 #if defined(__x86_64__) || defined(_M_X64)
133 #if defined(__aarch64__)
139 #if defined (__powerpc64__)
140 # define FOLLY_PPC64 1
142 # define FOLLY_PPC64 0
145 // packing is very ugly in msvc
147 # define FOLLY_PACK_ATTR /**/
148 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
149 # define FOLLY_PACK_POP __pragma(pack(pop))
150 #elif defined(__clang__) || defined(__GNUC__)
151 # define FOLLY_PACK_ATTR __attribute__((__packed__))
152 # define FOLLY_PACK_PUSH /**/
153 # define FOLLY_PACK_POP /**/
155 # define FOLLY_PACK_ATTR /**/
156 # define FOLLY_PACK_PUSH /**/
157 # define FOLLY_PACK_POP /**/
160 // portable version check
161 #ifndef __GNUC_PREREQ
162 # if defined __GNUC__ && defined __GNUC_MINOR__
164 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
165 ((maj) << 16) + (min))
168 # define __GNUC_PREREQ(maj, min) 0
172 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
173 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
174 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
175 // Alias it into std (where it's found in 4.9 and later)
176 namespace std { typedef ::max_align_t max_align_t; }
179 // portable version check for clang
180 #ifndef __CLANG_PREREQ
181 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
183 # define __CLANG_PREREQ(maj, min) \
184 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
187 # define __CLANG_PREREQ(maj, min) 0
191 /* Platform specific TLS support
192 * gcc implements __thread
193 * msvc implements __declspec(thread)
194 * the semantics are the same
195 * (but remember __thread has different semantics when using emutls (ex. apple))
197 #if defined(_MSC_VER)
198 # define FOLLY_TLS __declspec(thread)
199 #elif defined(__GNUC__) || defined(__clang__)
200 # define FOLLY_TLS __thread
202 # error cannot define platform specific thread local storage
205 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
206 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
207 # if defined(__GLIBC_PREREQ)
208 # if __GLIBC_PREREQ(2, 10)
209 # define FOLLY_HAVE_PREADV 1
210 # define FOLLY_HAVE_PWRITEV 1
215 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
216 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
217 // up in a macro to make forward-declarations easier.
220 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
221 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
223 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
224 #define FOLLY_NAMESPACE_STD_END }
227 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
228 // some types, e.g. std::list.
229 #if _GLIBCXX_USE_CXX11_ABI
230 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
231 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
233 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
234 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
237 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
238 // versions of these into the global namespace.
239 #if FOLLY_HAVE_CLOCK_GETTIME
242 #include <folly/detail/Clock.h>
245 // Provide our own std::__throw_* wrappers for platforms that don't have them
246 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
247 #include <bits/functexcept.h>
249 #include <folly/detail/FunctionalExcept.h>
252 #if defined(__cplusplus)
253 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
254 // usage of __has_trivial_copy(), so we can't use it as a
255 // least-common-denominator for C++11 implementations that don't support
256 // std::is_trivially_copyable<T>.
258 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
260 // As a result, use std::is_trivially_copyable() where it exists, and fall back
261 // to Boost otherwise.
262 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
263 #include <type_traits>
264 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
265 (std::is_trivially_copyable<T>::value)
267 #include <boost/type_traits.hpp>
268 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
269 (boost::has_trivial_copy<T>::value && \
270 boost::has_trivial_destructor<T>::value)
272 #endif // __cplusplus
274 // MSVC specific defines
275 // mainly for posix compat
278 // this definition is in a really silly place with a silly name
279 // and ifdefing it every time we want it is painful
281 typedef SSIZE_T ssize_t;
283 // sprintf semantics are not exactly identical
284 // but current usage is not a problem
285 # define snprintf _snprintf
287 // semantics here are identical
288 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
290 // compiler specific to compiler specific
292 # define __PRETTY_FUNCTION__ __FUNCSIG__
294 // Hide a GCC specific thing that breaks MSVC if left alone.
295 # define __extension__
298 # define FOLLY_SSE _M_IX86_FP
299 # define FOLLY_SSE_MINOR 0
305 # if defined(__SSE4_2__)
307 # define FOLLY_SSE_MINOR 2
308 # elif defined(__SSE4_1__)
310 # define FOLLY_SSE_MINOR 1
311 # elif defined(__SSE4__)
313 # define FOLLY_SSE_MINOR 0
314 # elif defined(__SSE3__)
316 # define FOLLY_SSE_MINOR 0
317 # elif defined(__SSE2__)
319 # define FOLLY_SSE_MINOR 0
320 # elif defined(__SSE__)
322 # define FOLLY_SSE_MINOR 0
325 # define FOLLY_SSE_MINOR 0
329 #define FOLLY_SSE_PREREQ(major, minor) \
330 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
332 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
333 namespace FOLLY_GFLAGS_NAMESPACE { }
335 using namespace FOLLY_GFLAGS_NAMESPACE;
336 } // namespace gflags
339 // for TARGET_OS_IPHONE
341 #include <TargetConditionals.h>
344 // MacOS doesn't have malloc_usable_size()
345 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
346 inline size_t malloc_usable_size(void* ptr) {
347 return malloc_size(ptr);
351 // RTTI may not be enabled for this compilation unit.
352 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
353 (defined(_MSC_VER) && defined(_CPPRTTI))
354 # define FOLLY_HAS_RTTI 1
363 inline void asm_volatile_memory() {
364 #if defined(__clang__) || defined(__GNUC__)
365 asm volatile("" : : : "memory");
366 #elif defined(_MSC_VER)
367 ::_ReadWriteBarrier();
371 inline void asm_volatile_pause() {
372 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
374 #elif defined(__i386__) || FOLLY_X64
375 asm volatile ("pause");
377 asm volatile ("wfe");
379 asm volatile("or 27,27,27");
382 inline void asm_pause() {
383 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
385 #elif defined(__i386__) || FOLLY_X64
394 constexpr size_t constexpr_strlen(const char* s) {
395 #if defined(__clang__)
396 return __builtin_strlen(s);
402 #if defined(__APPLE__) || defined(_MSC_VER)
403 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
405 // 101 is the highest priority allowed by the init_priority attribute.
406 // This priority is already used by JEMalloc and other memory allocators so
407 // we will take the next one.
408 #define MAX_STATIC_CONSTRUCTOR_PRIORITY __attribute__ ((__init_priority__(102)))
412 #endif // FOLLY_PORTABILITY_H_