Fix #includes
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2014 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef FOLLY_PORTABILITY_H_
18 #define FOLLY_PORTABILITY_H_
19
20 #ifndef FOLLY_NO_CONFIG
21 #include <folly/folly-config.h>
22 #endif
23
24 #ifdef FOLLY_PLATFORM_CONFIG
25 #include FOLLY_PLATFORM_CONFIG
26 #endif
27
28 #if FOLLY_HAVE_FEATURES_H
29 #include <features.h>
30 #endif
31
32 #include <folly/CPortability.h>
33
34 #if FOLLY_HAVE_SCHED_H
35  #include <sched.h>
36  #ifndef FOLLY_HAVE_PTHREAD_YIELD
37   #define pthread_yield sched_yield
38  #endif
39 #endif
40
41 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
42 // MAP_ANON on OSX/BSD.
43 #if defined(__APPLE__) || defined(__FreeBSD__)
44   #include <sys/mman.h>
45   #ifndef MAP_ANONYMOUS
46     #ifdef MAP_ANON
47       #define MAP_ANONYMOUS MAP_ANON
48     #endif
49   #endif
50 #endif
51
52 // MaxAlign: max_align_t isn't supported by gcc
53 #ifdef __GNUC__
54 struct MaxAlign { char c; } __attribute__((__aligned__));
55 #else /* !__GNUC__ */
56 # error Cannot define MaxAlign on this platform
57 #endif
58
59 // compiler specific attribute translation
60 // msvc should come first, so if clang is in msvc mode it gets the right defines
61
62 // NOTE: this will only do checking in msvc with versions that support /analyze
63 #if _MSC_VER
64 # ifdef _USE_ATTRIBUTES_FOR_SAL
65 #    undef _USE_ATTRIBUTES_FOR_SAL
66 # endif
67 # define _USE_ATTRIBUTES_FOR_SAL 1
68 # include <sal.h>
69 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
70 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
71 #else
72 # define FOLLY_PRINTF_FORMAT /**/
73 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
74   __attribute__((__format__(__printf__, format_param, dots_param)))
75 #endif
76
77 // noreturn
78 #if defined(_MSC_VER)
79 # define FOLLY_NORETURN __declspec(noreturn)
80 #elif defined(__clang__) || defined(__GNUC__)
81 # define FOLLY_NORETURN __attribute__((__noreturn__))
82 #else
83 # define FOLLY_NORETURN
84 #endif
85
86 // noinline
87 #ifdef _MSC_VER
88 # define FOLLY_NOINLINE __declspec(noinline)
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_NOINLINE __attribute__((__noinline__))
91 #else
92 # define FOLLY_NOINLINE
93 #endif
94
95 // always inline
96 #ifdef _MSC_VER
97 # define FOLLY_ALWAYS_INLINE __forceinline
98 #elif defined(__clang__) || defined(__GNUC__)
99 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
100 #else
101 # define FOLLY_ALWAYS_INLINE
102 #endif
103
104 // detection for 64 bit
105 #if defined(__x86_64__) || defined(_M_X64)
106 # define FOLLY_X64 1
107 #else
108 # define FOLLY_X64 0
109 #endif
110
111 // packing is very ugly in msvc
112 #ifdef _MSC_VER
113 # define FOLLY_PACK_ATTR /**/
114 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
115 # define FOLLY_PACK_POP __pragma(pack(pop))
116 #elif defined(__clang__) || defined(__GNUC__)
117 # define FOLLY_PACK_ATTR __attribute__((__packed__))
118 # define FOLLY_PACK_PUSH /**/
119 # define FOLLY_PACK_POP /**/
120 #else
121 # define FOLLY_PACK_ATTR /**/
122 # define FOLLY_PACK_PUSH /**/
123 # define FOLLY_PACK_POP /**/
124 #endif
125
126 // portable version check
127 #ifndef __GNUC_PREREQ
128 # if defined __GNUC__ && defined __GNUC_MINOR__
129 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
130                                    ((maj) << 16) + (min))
131 # else
132 #  define __GNUC_PREREQ(maj, min) 0
133 # endif
134 #endif
135
136
137 /* Define macro wrappers for C++11's "final" and "override" keywords, which
138  * are supported in gcc 4.7 but not gcc 4.6. */
139 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
140 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
141 #  define FOLLY_FINAL final
142 #  define FOLLY_OVERRIDE override
143 # else
144 #  define FOLLY_FINAL /**/
145 #  define FOLLY_OVERRIDE /**/
146 # endif
147 #endif
148
149 /* Platform specific TLS support
150  * gcc implements __thread
151  * msvc implements __declspec(thread)
152  * the semantics are the same (but remember __thread is broken on apple)
153  */
154 #if defined(_MSC_VER)
155 # define FOLLY_TLS __declspec(thread)
156 #elif defined(__GNUC__) || defined(__clang__)
157 # define FOLLY_TLS __thread
158 #else
159 # error cannot define platform specific thread local storage
160 #endif
161
162 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
163 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
164 # if defined(__GLIBC_PREREQ)
165 #  if __GLIBC_PREREQ(2, 10)
166 #   define FOLLY_HAVE_PREADV 1
167 #   define FOLLY_HAVE_PWRITEV 1
168 #  endif
169 # endif
170 #endif
171
172 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
173 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
174 // up in a macro to make forward-declarations easier.
175 #if FOLLY_USE_LIBCPP
176 #include <__config>
177 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
178 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
179 #else
180 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
181 #define FOLLY_NAMESPACE_STD_END       }
182 #endif
183
184 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
185 // versions of these into the global namespace.
186 #if FOLLY_HAVE_CLOCK_GETTIME
187 #include <time.h>
188 #else
189 #include <folly/detail/Clock.h>
190 #endif
191
192 // Provide our own std::__throw_* wrappers for platforms that don't have them
193 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
194 #include <bits/functexcept.h>
195 #else
196 #include <folly/detail/FunctionalExcept.h>
197 #endif
198
199 #if defined(__cplusplus)
200 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
201 // usage of __has_trivial_copy(), so we can't use it as a
202 // least-common-denominator for C++11 implementations that don't support
203 // std::is_trivially_copyable<T>.
204 //
205 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
206 //
207 // As a result, use std::is_trivially_copyable() where it exists, and fall back
208 // to Boost otherwise.
209 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
210 #include <type_traits>
211 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
212   (std::is_trivially_copyable<T>::value)
213 #else
214 #include <boost/type_traits.hpp>
215 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
216   (boost::has_trivial_copy<T>::value &&                  \
217    boost::has_trivial_destructor<T>::value)
218 #endif
219 #endif // __cplusplus
220
221 // MSVC specific defines
222 // mainly for posix compat
223 #ifdef _MSC_VER
224
225 // this definition is in a really silly place with a silly name
226 // and ifdefing it every time we want it is painful
227 #include <basetsd.h>
228 typedef SSIZE_T ssize_t;
229
230 // sprintf semantics are not exactly identical
231 // but current usage is not a problem
232 # define snprintf _snprintf
233
234 // semantics here are identical
235 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
236
237 // compiler specific to compiler specific
238 # define __PRETTY_FUNCTION__ __FUNCSIG__
239 #endif
240
241 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
242 namespace FOLLY_GFLAGS_NAMESPACE { }
243 namespace gflags {
244 using namespace FOLLY_GFLAGS_NAMESPACE;
245 }  // namespace gflags
246 #endif
247
248 #endif // FOLLY_PORTABILITY_H_