Add MSVC support for FOLLY_DEPRECATED
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2015 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 #ifdef __APPLE__
35 # include <malloc/malloc.h>
36 #endif
37
38 #if FOLLY_HAVE_SCHED_H
39  #include <sched.h>
40  #ifndef FOLLY_HAVE_PTHREAD_YIELD
41   #define pthread_yield sched_yield
42  #endif
43 #endif
44
45 // A change in folly/MemoryMapping.cpp uses MAP_ANONYMOUS, which is named
46 // MAP_ANON on OSX/BSD.
47 #if defined(__APPLE__) || defined(__FreeBSD__)
48   #include <sys/mman.h>
49   #ifndef MAP_ANONYMOUS
50     #ifdef MAP_ANON
51       #define MAP_ANONYMOUS MAP_ANON
52     #endif
53   #endif
54 #endif
55
56 // MaxAlign: max_align_t isn't supported by gcc
57 #ifdef __GNUC__
58 struct MaxAlign { char c; } __attribute__((__aligned__));
59 #else /* !__GNUC__ */
60 # error Cannot define MaxAlign on this platform
61 #endif
62
63 // compiler specific attribute translation
64 // msvc should come first, so if clang is in msvc mode it gets the right defines
65
66 // NOTE: this will only do checking in msvc with versions that support /analyze
67 #if _MSC_VER
68 # ifdef _USE_ATTRIBUTES_FOR_SAL
69 #    undef _USE_ATTRIBUTES_FOR_SAL
70 # endif
71 /* nolint */
72 # define _USE_ATTRIBUTES_FOR_SAL 1
73 # include <sal.h>
74 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
75 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
76 #else
77 # define FOLLY_PRINTF_FORMAT /**/
78 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
79   __attribute__((__format__(__printf__, format_param, dots_param)))
80 #endif
81
82 // deprecated
83 #if defined(__clang__) || defined(__GNUC__)
84 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
85 #elif defined(_MSC_VER)
86 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
87 #else
88 # define FOLLY_DEPRECATED
89 #endif
90
91 // noreturn
92 #if defined(_MSC_VER)
93 # define FOLLY_NORETURN __declspec(noreturn)
94 #elif defined(__clang__) || defined(__GNUC__)
95 # define FOLLY_NORETURN __attribute__((__noreturn__))
96 #else
97 # define FOLLY_NORETURN
98 #endif
99
100 // noinline
101 #ifdef _MSC_VER
102 # define FOLLY_NOINLINE __declspec(noinline)
103 #elif defined(__clang__) || defined(__GNUC__)
104 # define FOLLY_NOINLINE __attribute__((__noinline__))
105 #else
106 # define FOLLY_NOINLINE
107 #endif
108
109 // always inline
110 #ifdef _MSC_VER
111 # define FOLLY_ALWAYS_INLINE __forceinline
112 #elif defined(__clang__) || defined(__GNUC__)
113 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
114 #else
115 # define FOLLY_ALWAYS_INLINE
116 #endif
117
118 // detection for 64 bit
119 #if defined(__x86_64__) || defined(_M_X64)
120 # define FOLLY_X64 1
121 #else
122 # define FOLLY_X64 0
123 #endif
124
125 #if defined(__aarch64__)
126 # define FOLLY_A64 1
127 #else
128 # define FOLLY_A64 0
129 #endif
130
131 // packing is very ugly in msvc
132 #ifdef _MSC_VER
133 # define FOLLY_PACK_ATTR /**/
134 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
135 # define FOLLY_PACK_POP __pragma(pack(pop))
136 #elif defined(__clang__) || defined(__GNUC__)
137 # define FOLLY_PACK_ATTR __attribute__((__packed__))
138 # define FOLLY_PACK_PUSH /**/
139 # define FOLLY_PACK_POP /**/
140 #else
141 # define FOLLY_PACK_ATTR /**/
142 # define FOLLY_PACK_PUSH /**/
143 # define FOLLY_PACK_POP /**/
144 #endif
145
146 // portable version check
147 #ifndef __GNUC_PREREQ
148 # if defined __GNUC__ && defined __GNUC_MINOR__
149 /* nolint */
150 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
151                                    ((maj) << 16) + (min))
152 # else
153 /* nolint */
154 #  define __GNUC_PREREQ(maj, min) 0
155 # endif
156 #endif
157
158
159 /* Define macro wrappers for C++11's "final" and "override" keywords, which
160  * are supported in gcc 4.7 but not gcc 4.6. */
161 #if !defined(FOLLY_FINAL) && !defined(FOLLY_OVERRIDE)
162 # if defined(__clang__) || __GNUC_PREREQ(4, 7)
163 #  define FOLLY_FINAL final
164 #  define FOLLY_OVERRIDE override
165 # elif defined(_MSC_VER) && _MSC_VER >= 1600
166 #  define FOLLY_FINAL final
167 #  define FOLLY_OVERRIDE override
168 # else
169 #  define FOLLY_FINAL /**/
170 #  define FOLLY_OVERRIDE /**/
171 # endif
172 #endif
173
174 /* Platform specific TLS support
175  * gcc implements __thread
176  * msvc implements __declspec(thread)
177  * the semantics are the same
178  * (but remember __thread has different semantics when using emutls (ex. apple))
179  */
180 #if defined(_MSC_VER)
181 # define FOLLY_TLS __declspec(thread)
182 #elif defined(__GNUC__) || defined(__clang__)
183 # define FOLLY_TLS __thread
184 #else
185 # error cannot define platform specific thread local storage
186 #endif
187
188 // Define to 1 if you have the `preadv' and `pwritev' functions, respectively
189 #if !defined(FOLLY_HAVE_PREADV) && !defined(FOLLY_HAVE_PWRITEV)
190 # if defined(__GLIBC_PREREQ)
191 #  if __GLIBC_PREREQ(2, 10)
192 #   define FOLLY_HAVE_PREADV 1
193 #   define FOLLY_HAVE_PWRITEV 1
194 #  endif
195 # endif
196 #endif
197
198 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
199 // the 'std' namespace; the latter uses inline namepsaces. Wrap this decision
200 // up in a macro to make forward-declarations easier.
201 #if FOLLY_USE_LIBCPP
202 #include <__config>
203 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
204 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
205 #else
206 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
207 #define FOLLY_NAMESPACE_STD_END       }
208 #endif
209
210 // Some platforms lack clock_gettime(2) and clock_getres(2). Inject our own
211 // versions of these into the global namespace.
212 #if FOLLY_HAVE_CLOCK_GETTIME
213 #include <time.h>
214 #else
215 #include <folly/detail/Clock.h>
216 #endif
217
218 // Provide our own std::__throw_* wrappers for platforms that don't have them
219 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
220 #include <bits/functexcept.h>
221 #else
222 #include <folly/detail/FunctionalExcept.h>
223 #endif
224
225 #if defined(__cplusplus)
226 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
227 // usage of __has_trivial_copy(), so we can't use it as a
228 // least-common-denominator for C++11 implementations that don't support
229 // std::is_trivially_copyable<T>.
230 //
231 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
232 //
233 // As a result, use std::is_trivially_copyable() where it exists, and fall back
234 // to Boost otherwise.
235 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
236 #include <type_traits>
237 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
238   (std::is_trivially_copyable<T>::value)
239 #else
240 #include <boost/type_traits.hpp>
241 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
242   (boost::has_trivial_copy<T>::value &&                  \
243    boost::has_trivial_destructor<T>::value)
244 #endif
245 #endif // __cplusplus
246
247 // MSVC specific defines
248 // mainly for posix compat
249 #ifdef _MSC_VER
250
251 // this definition is in a really silly place with a silly name
252 // and ifdefing it every time we want it is painful
253 #include <basetsd.h>
254 typedef SSIZE_T ssize_t;
255
256 // sprintf semantics are not exactly identical
257 // but current usage is not a problem
258 # define snprintf _snprintf
259
260 // semantics here are identical
261 # define strerror_r(errno,buf,len) strerror_s(buf,len,errno)
262
263 // compiler specific to compiler specific
264 // nolint
265 # define __PRETTY_FUNCTION__ __FUNCSIG__
266
267 // Hide a GCC specific thing that breaks MSVC if left alone.
268 # define __extension__
269
270 #ifdef _M_IX86_FP
271 # define FOLLY_SSE _M_IX86_FP
272 #endif
273
274 #endif
275
276 #ifndef FOLLY_SSE
277 # if defined(__SSE4_2__)
278 #  define FOLLY_SSE 4.2
279 # elif defined(__SSE4_1__)
280 #  define FOLLY_SSE 4.1
281 # elif defined(__SSE4__)
282 #  define FOLLY_SSE 4
283 # elif defined(__SSE3__)
284 #  define FOLLY_SSE 3
285 # elif defined(__SSE2__)
286 #  define FOLLY_SSE 2
287 # elif defined(__SSE__)
288 #  define FOLLY_SSE 1
289 # else
290 #  define FOLLY_SSE 0
291 # endif
292 #endif
293
294 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
295 namespace FOLLY_GFLAGS_NAMESPACE { }
296 namespace gflags {
297 using namespace FOLLY_GFLAGS_NAMESPACE;
298 }  // namespace gflags
299 #endif
300
301 // for TARGET_OS_IPHONE
302 #ifdef __APPLE__
303 #include <TargetConditionals.h>
304 #endif
305
306 // MacOS doesn't have malloc_usable_size()
307 #if defined(__APPLE__) && !defined(FOLLY_HAVE_MALLOC_USABLE_SIZE)
308 inline size_t malloc_usable_size(void* ptr) {
309   return malloc_size(ptr);
310 }
311 #endif
312
313 // RTTI may not be enabled for this compilation unit.
314 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
315     (defined(_MSC_VER) && defined(_CPPRTTI))
316 # define FOLLY_HAS_RTTI 1
317 #endif
318
319 #ifdef _MSC_VER
320 # include <intrin.h>
321 #endif
322
323 namespace folly {
324
325 inline void asm_volatile_pause() {
326 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
327   ::_mm_pause();
328 #elif defined(__i386__) || FOLLY_X64
329   asm volatile ("pause");
330 #elif FOLLY_A64
331   asm volatile ("wfe");
332 #endif
333 }
334 inline void asm_pause() {
335 #if defined(_MSC_VER) && (defined(_M_IX86) || defined(_M_X64))
336   ::_mm_pause();
337 #elif defined(__i386__) || FOLLY_X64
338   asm ("pause");
339 #elif FOLLY_A64
340   asm ("wfe");
341 #endif
342 }
343
344 }
345
346 #endif // FOLLY_PORTABILITY_H_