2 * Copyright 2014 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-config.h"
24 #if FOLLY_HAVE_FEATURES_H
28 #include "CPortability.h"
30 #if FOLLY_HAVE_SCHED_H
32 #ifndef FOLLY_HAVE_PTHREAD_YIELD
33 #define pthread_yield sched_yield
38 // MaxAlign: max_align_t isn't supported by gcc
40 struct MaxAlign { char c; } __attribute__((aligned));
42 # error Cannot define MaxAlign on this platform
45 // compiler specific attribute translation
46 // msvc should come first, so if clang is in msvc mode it gets the right defines
50 # define FOLLY_NORETURN __declspec(noreturn)
51 #elif defined(__clang__) || defined(__GNUC__)
52 # define FOLLY_NORETURN __attribute__((noreturn))
54 # define FOLLY_NORETURN
59 # define FOLLY_NOINLINE __declspec(noinline)
60 #elif defined(__clang__) || defined(__GNUC__)
61 # define FOLLY_NOINLINE __attribute__((noinline))
63 # define FOLLY_NOINLINE
68 # define FOLLY_ALWAYS_INLINE __forceinline
69 #elif defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_ALWAYS_INLINE inline __attribute__((always_inline))
72 # define FOLLY_ALWAYS_INLINE
75 // detection for 64 bit
76 #if defined(__x86_64__) || defined(_M_X64)
82 // portable version check
84 # if defined __GNUC__ && defined __GNUC_MINOR__
85 # define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
86 ((maj) << 16) + (min))
88 # define __GNUC_PREREQ(maj, min) 0
93 /* Define macro wrappers for C++11's "final" and "override" keywords, which
94 * are supported in gcc 4.7 but not gcc 4.6. */
95 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
96 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
97 # define FOLLY_FINAL final
98 # define FOLLY_OVERRIDE override
100 # define FOLLY_FINAL /**/
101 # define FOLLY_OVERRIDE /**/
105 /* Platform specific TLS support
106 * gcc implements __thread
107 * msvc implements __declspec(thread)
108 * the semantics are the same (but remember __thread is broken on apple)
110 #if defined(_MSC_VER)
111 # define FOLLY_TLS __declspec(thread)
112 #elif defined(__GNUC__) || defined(__clang__)
113 # define FOLLY_TLS __thread
115 # error cannot define platform specific thread local storage
118 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
119 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
120 # if defined(__GLIBC_PREREQ)
121 # if __GLIBC_PREREQ(2, 10)
122 # define FOLLY_HAVE_PREADV 1
123 # define FOLLY_HAVE_PWRITEV 1
128 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
129 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
130 // up in a macro to make forward-declarations easier.
132 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
133 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
135 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
136 #define FOLLY_NAMESPACE_STD_END }
139 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
140 // versions of these into the global namespace.
141 #if FOLLY_HAVE_CLOCK_GETTIME
144 #include "folly/detail/Clock.h"
147 // Provide our own std::__throw_* wrappers for platforms that don't have them
148 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
149 #include <bits/functexcept.h>
151 #include "folly/detail/FunctionalExcept.h"
154 #if defined(__cplusplus)
155 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
156 // usage of __has_trivial_copy(), so we can't use it as a
157 // least-common-denominator for C++11 implementations that don't support
158 // std::is_trivially_copyable<T>.
160 // http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
162 // As a result, use std::is_trivially_copyable() where it exists, and fall back
163 // to Boost otherwise.
164 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
165 #include <type_traits>
166 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
167 (std::is_trivially_copyable<T>::value)
169 #include <boost/type_traits.hpp>
170 #define FOLLY_IS_TRIVIALLY_COPYABLE(T) \
171 (boost::has_trivial_copy<T>::value && \
172 boost::has_trivial_destructor<T>::value)
174 #endif // __cplusplus
176 #endif // FOLLY_PORTABILITY_H_