87dbe2569314fc239f4ed779c1fd56d66e12d15f
[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 /* nolint */
68 # define _USE_ATTRIBUTES_FOR_SAL 1
69 # include <sal.h>
70 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
71 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
72 #else
73 # define FOLLY_PRINTF_FORMAT /**/
74 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
75   __attribute__((__format__(__printf__, format_param, dots_param)))
76 #endif
77
78 // noreturn
79 #if defined(_MSC_VER)
80 # define FOLLY_NORETURN __declspec(noreturn)
81 #elif defined(__clang__) || defined(__GNUC__)
82 # define FOLLY_NORETURN __attribute__((__noreturn__))
83 #else
84 # define FOLLY_NORETURN
85 #endif
86
87 // noinline
88 #ifdef _MSC_VER
89 # define FOLLY_NOINLINE __declspec(noinline)
90 #elif defined(__clang__) || defined(__GNUC__)
91 # define FOLLY_NOINLINE __attribute__((__noinline__))
92 #else
93 # define FOLLY_NOINLINE
94 #endif
95
96 // always inline
97 #ifdef _MSC_VER
98 # define FOLLY_ALWAYS_INLINE __forceinline
99 #elif defined(__clang__) || defined(__GNUC__)
100 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
101 #else
102 # define FOLLY_ALWAYS_INLINE
103 #endif
104
105 // detection for 64 bit
106 #if defined(__x86_64__) || defined(_M_X64)
107 # define FOLLY_X64 1
108 #else
109 # define FOLLY_X64 0
110 #endif
111
112 // packing is very ugly in msvc
113 #ifdef _MSC_VER
114 # define FOLLY_PACK_ATTR /**/
115 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
116 # define FOLLY_PACK_POP __pragma(pack(pop))
117 #elif defined(__clang__) || defined(__GNUC__)
118 # define FOLLY_PACK_ATTR __attribute__((__packed__))
119 # define FOLLY_PACK_PUSH /**/
120 # define FOLLY_PACK_POP /**/
121 #else
122 # define FOLLY_PACK_ATTR /**/
123 # define FOLLY_PACK_PUSH /**/
124 # define FOLLY_PACK_POP /**/
125 #endif
126
127 // portable version check
128 #ifndef __GNUC_PREREQ
129 # if defined __GNUC__ && defined __GNUC_MINOR__
130 /* nolint */
131 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
132                                    ((maj) << 16) + (min))
133 # else
134 /* nolint */
135 #  define __GNUC_PREREQ(maj, min) 0
136 # endif
137 #endif
138
139
140 /* Define macro wrappers for C++11's "final" and "override" keywords, which
141  * are supported in gcc 4.7 but not gcc 4.6. */
142 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
143 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
144 #  define FOLLY_FINAL final
145 #  define FOLLY_OVERRIDE override
146 # else
147 #  define FOLLY_FINAL /**/
148 #  define FOLLY_OVERRIDE /**/
149 # endif
150 #endif
151
152 /* Platform specific TLS support
153  * gcc implements __thread
154  * msvc implements __declspec(thread)
155  * the semantics are the same
156  * (but remember __thread has different semantics when using emutls (ex. apple))
157  */
158 #if defined(_MSC_VER)
159 # define FOLLY_TLS __declspec(thread)
160 #elif defined(__GNUC__) || defined(__clang__)
161 # define FOLLY_TLS __thread
162 #else
163 # error cannot define platform specific thread local storage
164 #endif
165
166 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
167 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
168 # if defined(__GLIBC_PREREQ)
169 #  if __GLIBC_PREREQ(2, 10)
170 #   define FOLLY_HAVE_PREADV 1
171 #   define FOLLY_HAVE_PWRITEV 1
172 #  endif
173 # endif
174 #endif
175
176 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
177 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
178 // up in a macro to make forward-declarations easier.
179 #if FOLLY_USE_LIBCPP
180 #include <__config>
181 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
182 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
183 #else
184 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
185 #define FOLLY_NAMESPACE_STD_END       }
186 #endif
187
188 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
189 // versions of these into the global namespace.
190 #if FOLLY_HAVE_CLOCK_GETTIME
191 #include <time.h>
192 #else
193 #include <folly/detail/Clock.h>
194 #endif
195
196 // Provide our own std::__throw_* wrappers for platforms that don't have them
197 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
198 #include <bits/functexcept.h>
199 #else
200 #include <folly/detail/FunctionalExcept.h>
201 #endif
202
203 #if defined(__cplusplus)
204 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
205 // usage of __has_trivial_copy(), so we can't use it as a
206 // least-common-denominator for C++11 implementations that don't support
207 // std::is_trivially_copyable<T>.
208 //
209 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
210 //
211 // As a result, use std::is_trivially_copyable() where it exists, and fall back
212 // to Boost otherwise.
213 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
214 #include <type_traits>
215 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
216   (std::is_trivially_copyable<T>::value)
217 #else
218 #include <boost/type_traits.hpp>
219 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
220   (boost::has_trivial_copy<T>::value &&                  \
221    boost::has_trivial_destructor<T>::value)
222 #endif
223 #endif // __cplusplus
224
225 // MSVC specific defines
226 // mainly for posix compat
227 #ifdef _MSC_VER
228
229 // this definition is in a really silly place with a silly name
230 // and ifdefing it every time we want it is painful
231 #include <basetsd.h>
232 typedef SSIZE_T ssize_t;
233
234 // sprintf semantics are not exactly identical
235 // but current usage is not a problem
236 # define snprintf _snprintf
237
238 // semantics here are identical
239 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
240
241 // compiler specific to compiler specific
242 // nolint
243 # define __PRETTY_FUNCTION__ __FUNCSIG__
244 #endif
245
246 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
247 namespace FOLLY_GFLAGS_NAMESPACE { }
248 namespace gflags {
249 using namespace FOLLY_GFLAGS_NAMESPACE;
250 }  // namespace gflags
251 #endif
252
253 // for TARGET_OS_IPHONE
254 #ifdef __APPLE__
255 #include <TargetConditionals.h>
256 #endif
257
258 #endif // FOLLY_PORTABILITY_H_