make Range::size() constexpr
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2016 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 #pragma once
18
19 #include <string.h>
20
21 #include <cstddef>
22
23 #include <folly/portability/Config.h>
24
25 #include <folly/CPortability.h>
26
27 #if FOLLY_HAVE_SCHED_H
28  #include <sched.h>
29 #endif
30
31 // Unaligned loads and stores
32 namespace folly {
33 #if FOLLY_HAVE_UNALIGNED_ACCESS
34 constexpr bool kHasUnalignedAccess = true;
35 #else
36 constexpr bool kHasUnalignedAccess = false;
37 #endif
38 }
39
40 // compiler specific attribute translation
41 // msvc should come first, so if clang is in msvc mode it gets the right defines
42
43 #if defined(__clang__) || defined(__GNUC__)
44 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
45 #elif defined(_MSC_VER)
46 # define FOLLY_ALIGNED(size) __declspec(align(size))
47 #else
48 # error Cannot define FOLLY_ALIGNED on this platform
49 #endif
50 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(alignof(std::max_align_t))
51
52 // NOTE: this will only do checking in msvc with versions that support /analyze
53 #if _MSC_VER
54 # ifdef _USE_ATTRIBUTES_FOR_SAL
55 #    undef _USE_ATTRIBUTES_FOR_SAL
56 # endif
57 /* nolint */
58 # define _USE_ATTRIBUTES_FOR_SAL 1
59 # include <sal.h>
60 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
61 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
62 #else
63 # define FOLLY_PRINTF_FORMAT /**/
64 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
65   __attribute__((__format__(__printf__, format_param, dots_param)))
66 #endif
67
68 // deprecated
69 #if defined(__clang__) || defined(__GNUC__)
70 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
71 #elif defined(_MSC_VER)
72 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
73 #else
74 # define FOLLY_DEPRECATED(msg)
75 #endif
76
77 // noinline
78 #ifdef _MSC_VER
79 # define FOLLY_NOINLINE __declspec(noinline)
80 #elif defined(__clang__) || defined(__GNUC__)
81 # define FOLLY_NOINLINE __attribute__((__noinline__))
82 #else
83 # define FOLLY_NOINLINE
84 #endif
85
86 // always inline
87 #ifdef _MSC_VER
88 # define FOLLY_ALWAYS_INLINE __forceinline
89 #elif defined(__clang__) || defined(__GNUC__)
90 # define FOLLY_ALWAYS_INLINE inline __attribute__((__always_inline__))
91 #else
92 # define FOLLY_ALWAYS_INLINE inline
93 #endif
94
95 // detection for 64 bit
96 #if defined(__x86_64__) || defined(_M_X64)
97 # define FOLLY_X64 1
98 #else
99 # define FOLLY_X64 0
100 #endif
101
102 #if defined(__aarch64__)
103 # define FOLLY_A64 1
104 #else
105 # define FOLLY_A64 0
106 #endif
107
108 #if defined (__powerpc64__)
109 # define FOLLY_PPC64 1
110 #else
111 # define FOLLY_PPC64 0
112 #endif
113
114 // packing is very ugly in msvc
115 #ifdef _MSC_VER
116 # define FOLLY_PACK_ATTR /**/
117 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
118 # define FOLLY_PACK_POP __pragma(pack(pop))
119 #elif defined(__clang__) || defined(__GNUC__)
120 # define FOLLY_PACK_ATTR __attribute__((__packed__))
121 # define FOLLY_PACK_PUSH /**/
122 # define FOLLY_PACK_POP /**/
123 #else
124 # define FOLLY_PACK_ATTR /**/
125 # define FOLLY_PACK_PUSH /**/
126 # define FOLLY_PACK_POP /**/
127 #endif
128
129 // Generalize warning push/pop.
130 #if defined(_MSC_VER)
131 # define FOLLY_PUSH_WARNING __pragma(warning(push))
132 # define FOLLY_POP_WARNING __pragma(warning(pop))
133 // Disable the GCC warnings.
134 # define FOLLY_GCC_DISABLE_WARNING(warningName)
135 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
136 #elif defined(__clang__) || defined(__GNUC__)
137 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
138 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
139 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName) #warningName
140 #define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) \
141   FOLLY_GCC_DISABLE_WARNING_INTERNAL3(warningName)
142 #define FOLLY_GCC_DISABLE_WARNING(warningName)                       \
143   _Pragma(FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored \
144           FOLLY_GCC_DISABLE_WARNING_INTERNAL3(-W##warningName)))
145 // Disable the MSVC warnings.
146 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
147 #else
148 # define FOLLY_PUSH_WARNING
149 # define FOLLY_POP_WARNING
150 # define FOLLY_GCC_DISABLE_WARNING(warningName)
151 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
152 #endif
153
154 // portable version check
155 #ifndef __GNUC_PREREQ
156 # if defined __GNUC__ && defined __GNUC_MINOR__
157 /* nolint */
158 #  define __GNUC_PREREQ(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= \
159                                    ((maj) << 16) + (min))
160 # else
161 /* nolint */
162 #  define __GNUC_PREREQ(maj, min) 0
163 # endif
164 #endif
165
166 #if defined(__GNUC__) && !defined(__APPLE__) && !__GNUC_PREREQ(4,9)
167 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56019
168 // gcc 4.8.x incorrectly placed max_align_t in the root namespace
169 // Alias it into std (where it's found in 4.9 and later)
170 namespace std { typedef ::max_align_t max_align_t; }
171 #endif
172
173 // portable version check for clang
174 #ifndef __CLANG_PREREQ
175 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
176 /* nolint */
177 #  define __CLANG_PREREQ(maj, min) \
178     ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
179 # else
180 /* nolint */
181 #  define __CLANG_PREREQ(maj, min) 0
182 # endif
183 #endif
184
185 /* Platform specific TLS support
186  * gcc implements __thread
187  * msvc implements __declspec(thread)
188  * the semantics are the same
189  * (but remember __thread has different semantics when using emutls (ex. apple))
190  */
191 #if defined(_MSC_VER)
192 # define FOLLY_TLS __declspec(thread)
193 #elif defined(__GNUC__) || defined(__clang__)
194 # define FOLLY_TLS __thread
195 #else
196 # error cannot define platform specific thread local storage
197 #endif
198
199 #if FOLLY_MOBILE
200 #undef FOLLY_TLS
201 #endif
202
203 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
204 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
205 // up in a macro to make forward-declarations easier.
206 #if FOLLY_USE_LIBCPP
207 #include <__config>
208 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
209 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
210 #else
211 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
212 #define FOLLY_NAMESPACE_STD_END       }
213 #endif
214
215 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
216 // some types, e.g. std::list.
217 #if _GLIBCXX_USE_CXX11_ABI
218 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN _GLIBCXX_BEGIN_NAMESPACE_CXX11
219 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
220 #else
221 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
222 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
223 #endif
224
225 // Provide our own std::__throw_* wrappers for platforms that don't have them
226 #if FOLLY_HAVE_BITS_FUNCTEXCEPT_H
227 #include <bits/functexcept.h>
228 #else
229 #include <folly/detail/FunctionalExcept.h>
230 #endif
231
232 #if defined(__cplusplus)
233 // Unfortunately, boost::has_trivial_copy<T> is broken in libc++ due to its
234 // usage of __has_trivial_copy(), so we can't use it as a
235 // least-common-denominator for C++11 implementations that don't support
236 // std::is_trivially_copyable<T>.
237 //
238 //      http://stackoverflow.com/questions/12754886/has-trivial-copy-behaves-differently-in-clang-and-gcc-whos-right
239 //
240 // As a result, use std::is_trivially_copyable() where it exists, and fall back
241 // to Boost otherwise.
242 #if FOLLY_HAVE_STD__IS_TRIVIALLY_COPYABLE
243 #include <type_traits>
244 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
245   (std::is_trivially_copyable<T>::value)
246 #else
247 #include <boost/type_traits.hpp>
248 #define FOLLY_IS_TRIVIALLY_COPYABLE(T)                   \
249   (boost::has_trivial_copy<T>::value &&                  \
250    boost::has_trivial_destructor<T>::value)
251 #endif
252 #endif // __cplusplus
253
254 // MSVC specific defines
255 // mainly for posix compat
256 #ifdef _MSC_VER
257 #include <folly/portability/SysTypes.h>
258
259 // compiler specific to compiler specific
260 // nolint
261 # define __PRETTY_FUNCTION__ __FUNCSIG__
262
263 // Hide a GCC specific thing that breaks MSVC if left alone.
264 # define __extension__
265
266 // We have compiler support for the newest of the new, but
267 // MSVC doesn't tell us that.
268 #define __SSE4_2__ 1
269
270 #endif
271
272 // Debug
273 namespace folly {
274 #ifdef NDEBUG
275 constexpr auto kIsDebug = false;
276 #else
277 constexpr auto kIsDebug = true;
278 #endif
279 }
280
281 // Endianness
282 namespace folly {
283 #ifdef _MSC_VER
284 // It's MSVC, so we just have to guess ... and allow an override
285 #ifdef FOLLY_ENDIAN_BE
286 constexpr auto kIsLittleEndian = false;
287 #else
288 constexpr auto kIsLittleEndian = true;
289 #endif
290 #else
291 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
292 #endif
293 constexpr auto kIsBigEndian = !kIsLittleEndian;
294 }
295
296 #ifndef FOLLY_SSE
297 # if defined(__SSE4_2__)
298 #  define FOLLY_SSE 4
299 #  define FOLLY_SSE_MINOR 2
300 # elif defined(__SSE4_1__)
301 #  define FOLLY_SSE 4
302 #  define FOLLY_SSE_MINOR 1
303 # elif defined(__SSE4__)
304 #  define FOLLY_SSE 4
305 #  define FOLLY_SSE_MINOR 0
306 # elif defined(__SSE3__)
307 #  define FOLLY_SSE 3
308 #  define FOLLY_SSE_MINOR 0
309 # elif defined(__SSE2__)
310 #  define FOLLY_SSE 2
311 #  define FOLLY_SSE_MINOR 0
312 # elif defined(__SSE__)
313 #  define FOLLY_SSE 1
314 #  define FOLLY_SSE_MINOR 0
315 # else
316 #  define FOLLY_SSE 0
317 #  define FOLLY_SSE_MINOR 0
318 # endif
319 #endif
320
321 #define FOLLY_SSE_PREREQ(major, minor) \
322   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
323
324 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
325 namespace FOLLY_GFLAGS_NAMESPACE { }
326 namespace gflags {
327 using namespace FOLLY_GFLAGS_NAMESPACE;
328 }  // namespace gflags
329 #endif
330
331 // for TARGET_OS_IPHONE
332 #ifdef __APPLE__
333 #include <TargetConditionals.h>
334 #endif
335
336 // RTTI may not be enabled for this compilation unit.
337 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
338     (defined(_MSC_VER) && defined(_CPPRTTI))
339 # define FOLLY_HAS_RTTI 1
340 #endif
341
342 #if defined(__APPLE__) || defined(_MSC_VER)
343 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
344 #else
345 // 101 is the highest priority allowed by the init_priority attribute.
346 // This priority is already used by JEMalloc and other memory allocators so
347 // we will take the next one.
348 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
349 #endif