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 #ifndef FOLLY_HAVE_UNALIGNED_READS
52 #define FOLLY_HAVE_UNALIGNED_READS 0
55 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
56 // MAP_ANON on OSX/BSD.
57 #if defined(__APPLE__) || defined(__FreeBSD__)
61 #define MAP_ANONYMOUS MAP_ANON
66 // compiler specific attribute translation
67 // msvc should come first, so if clang is in msvc mode it gets the right defines
69 #if defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
71 #elif defined(_MSC_VER)
72 # define FOLLY_ALIGNED(size) __declspec(align(size))
74 # error Cannot define FOLLY_ALIGNED on this platform
76 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
78 // NOTE: this will only do checking in msvc with versions that support /analyze
80 # ifdef _USE_ATTRIBUTES_FOR_SAL
81 # undef _USE_ATTRIBUTES_FOR_SAL
84 # define _USE_ATTRIBUTES_FOR_SAL 1
86 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
87 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
89 # define FOLLY_PRINTF_FORMAT /**/
90 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
91 __attribute__((__format__(__printf__, format_param, dots_param)))
95 #if defined(__clang__) || defined(__GNUC__)
96 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
97 #elif defined(_MSC_VER)
98 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
100 # define FOLLY_DEPRECATED(msg)
104 #if defined(_MSC_VER)
105 # define FOLLY_NORETURN __declspec(noreturn)
106 #elif defined(__clang__) || defined(__GNUC__)
107 # define FOLLY_NORETURN __attribute__((__noreturn__))
109 # define FOLLY_NORETURN
114 # define FOLLY_NOINLINE __declspec(noinline)
115 #elif defined(__clang__) || defined(__GNUC__)
116 # define FOLLY_NOINLINE __attribute__((__noinline__))
118 # define FOLLY_NOINLINE
123 # define FOLLY_ALWAYS_INLINE __forceinline
124 #elif defined(__clang__) || defined(__GNUC__)
125 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
127 # define FOLLY_ALWAYS_INLINE inline
130 // detection for 64 bit
131 #if defined(__x86_64__) || defined(_M_X64)
137 #if defined(__aarch64__)
143 #if defined (__powerpc64__)
144 # define FOLLY_PPC64 1
146 # define FOLLY_PPC64 0
149 // packing is very ugly in msvc
151 # define FOLLY_PACK_ATTR /**/
152 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
153 # define FOLLY_PACK_POP __pragma(pack(pop))
154 #elif defined(__clang__) || defined(__GNUC__)
155 # define FOLLY_PACK_ATTR __attribute__((__packed__))
156 # define FOLLY_PACK_PUSH /**/
157 # define FOLLY_PACK_POP /**/
159 # define FOLLY_PACK_ATTR /**/
160 # define FOLLY_PACK_PUSH /**/
161 # define FOLLY_PACK_POP /**/
164 // portable version check
165 #ifndef __GNUC_PREREQ
166 # if defined __GNUC__ && defined __GNUC_MINOR__
168 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
169 ((maj) << 16) + (min))
172 # define __GNUC_PREREQ(maj, min) 0
176 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
177 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
178 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
179 // Alias it into std (where it's found in 4.9 and later)
180 namespace std { typedef ::max_align_t max_align_t; }
183 // portable version check for clang
184 #ifndef __CLANG_PREREQ
185 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
187 # define __CLANG_PREREQ(maj, min) \
188 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
191 # define __CLANG_PREREQ(maj, min) 0
195 /* Platform specific TLS support
196 * gcc implements __thread
197 * msvc implements __declspec(thread)
198 * the semantics are the same
199 * (but remember __thread has different semantics when using emutls (ex. apple))
201 #if defined(_MSC_VER)
202 # define FOLLY_TLS __declspec(thread)
203 #elif defined(__GNUC__) || defined(__clang__)
204 # define FOLLY_TLS __thread
206 # error cannot define platform specific thread local storage
209 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
210 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
211 # if defined(__GLIBC_PREREQ)
212 # if __GLIBC_PREREQ(2, 10)
213 # define FOLLY_HAVE_PREADV 1
214 # define FOLLY_HAVE_PWRITEV 1
219 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
220 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
221 // up in a macro to make forward-declarations easier.
224 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
225 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
227 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
228 #define FOLLY_NAMESPACE_STD_END }
231 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
232 // some types, e.g. std::list.
233 #if _GLIBCXX_USE_CXX11_ABI
234 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
235 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END _GLIBCXX_END_NAMESPACE_CXX11
237 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
238 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
241 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
242 // versions of these into the global namespace.
243 #if FOLLY_HAVE_CLOCK_GETTIME
246 #include <folly/detail/Clock.h>
249 // Provide our own std::__throw_* wrappers for platforms that don't have them
250 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
251 #include <bits/functexcept.h>
253 #include <folly/detail/FunctionalExcept.h>
256 #if defined(__cplusplus)
257 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
258 // usage of __has_trivial_copy(), so we can't use it as a
259 // least-common-denominator for C++11 implementations that don't support
260 // std::is_trivially_copyable<T>.
262 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
264 // As a result, use std::is_trivially_copyable() where it exists, and fall back
265 // to Boost otherwise.
266 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
267 #include <type_traits>
268 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
269 (std::is_trivially_copyable<T>::value)
271 #include <boost/type_traits.hpp>
272 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
273 (boost::has_trivial_copy<T>::value && \
274 boost::has_trivial_destructor<T>::value)
276 #endif // __cplusplus
278 // MSVC specific defines
279 // mainly for posix compat
282 // this definition is in a really silly place with a silly name
283 // and ifdefing it every time we want it is painful
285 typedef SSIZE_T ssize_t;
287 // sprintf semantics are not exactly identical
288 // but current usage is not a problem
289 # define snprintf _snprintf
291 // semantics here are identical
292 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
294 // compiler specific to compiler specific
296 # define __PRETTY_FUNCTION__ __FUNCSIG__
298 // Hide a GCC specific thing that breaks MSVC if left alone.
299 # define __extension__
302 # define FOLLY_SSE _M_IX86_FP
303 # define FOLLY_SSE_MINOR 0
311 // It's MSVC, so we just have to guess ... and allow an override
312 #ifdef FOLLY_ENDIAN_BE
313 constexpr auto kIsLittleEndian = false;
315 constexpr auto kIsLittleEndian = true;
318 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
320 constexpr auto kIsBigEndian = !kIsLittleEndian;
324 # if defined(__SSE4_2__)
326 # define FOLLY_SSE_MINOR 2
327 # elif defined(__SSE4_1__)
329 # define FOLLY_SSE_MINOR 1
330 # elif defined(__SSE4__)
332 # define FOLLY_SSE_MINOR 0
333 # elif defined(__SSE3__)
335 # define FOLLY_SSE_MINOR 0
336 # elif defined(__SSE2__)
338 # define FOLLY_SSE_MINOR 0
339 # elif defined(__SSE__)
341 # define FOLLY_SSE_MINOR 0
344 # define FOLLY_SSE_MINOR 0
348 #define FOLLY_SSE_PREREQ(major, minor) \
349 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
351 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
352 namespace FOLLY_GFLAGS_NAMESPACE { }
354 using namespace FOLLY_GFLAGS_NAMESPACE;
355 } // namespace gflags
358 // for TARGET_OS_IPHONE
360 #include <TargetConditionals.h>
363 // MacOS doesn't have malloc_usable_size()
364 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
365 inline size_t malloc_usable_size(void* ptr) {
366 return malloc_size(ptr);
370 // RTTI may not be enabled for this compilation unit.
371 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
372 (defined(_MSC_VER) && defined(_CPPRTTI))
373 # define FOLLY_HAS_RTTI 1
382 inline void asm_volatile_memory() {
383 #if defined(__clang__) || defined(__GNUC__)
384 asm volatile("" : : : "memory");
385 #elif defined(_MSC_VER)
386 ::_ReadWriteBarrier();
390 inline void asm_volatile_pause() {
391 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
393 #elif defined(__i386__) || FOLLY_X64
394 asm volatile ("pause");
396 asm volatile ("wfe");
398 asm volatile("or 27,27,27");
401 inline void asm_pause() {
402 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
404 #elif defined(__i386__) || FOLLY_X64
413 constexpr size_t constexpr_strlen(const char* s) {
414 #if defined(__clang__)
415 return __builtin_strlen(s);
421 #if defined(__APPLE__) || defined(_MSC_VER)
422 #define MAX_STATIC_CONSTRUCTOR_PRIORITY
424 // 101 is the highest priority allowed by the init_priority attribute.
425 // This priority is already used by JEMalloc and other memory allocators so
426 // we will take the next one.
427 #define MAX_STATIC_CONSTRUCTOR_PRIORITY __attribute__ ((__init_priority__(102)))
431 #endif // FOLLY_PORTABILITY_H_