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