typo in io/Cursor.h
[folly.git] / folly / Portability.h
1 /*
2  * Copyright 2017 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 #include <type_traits>
23
24 #include <folly/portability/Config.h>
25
26 #include <folly/CPortability.h>
27
28 // Unaligned loads and stores
29 namespace folly {
30 #if FOLLY_HAVE_UNALIGNED_ACCESS
31 constexpr bool kHasUnalignedAccess = true;
32 #else
33 constexpr bool kHasUnalignedAccess = false;
34 #endif
35
36 namespace portability_detail {
37
38 template <typename I, I A, I B>
39 using integral_max = std::integral_constant<I, (A < B) ? B : A>;
40
41 template <typename I, I A, I... Bs>
42 struct integral_sequence_max
43     : integral_max<I, A, integral_sequence_max<I, Bs...>::value> {};
44
45 template <typename I, I A>
46 struct integral_sequence_max<I, A> : std::integral_constant<I, A> {};
47
48 template <typename... Ts>
49 using max_alignment = integral_sequence_max<size_t, alignof(Ts)...>;
50
51 using max_basic_alignment = max_alignment<
52     std::max_align_t,
53     long double,
54     double,
55     float,
56     long long int,
57     long int,
58     int,
59     short int,
60     bool,
61     char,
62     char16_t,
63     char32_t,
64     wchar_t,
65     std::nullptr_t>;
66 } // namespace portability_detail
67
68 constexpr size_t max_align_v = portability_detail::max_basic_alignment::value;
69
70 // max_align_t is a type which is aligned at least as strictly as the
71 // most-aligned basic type (see the specification of std::max_align_t). This
72 // implementation exists because 32-bit iOS platforms have a broken
73 // std::max_align_t (see below).
74 //
75 // You should refer to this as `::folly::max_align_t` in portable code, even if
76 // you have `using namespace folly;` because C11 defines a global namespace
77 // `max_align_t` type.
78 //
79 // To be certain, we consider every non-void fundamental type specified by the
80 // standard. On most platforms `long double` would be enough, but iOS 32-bit
81 // has an 8-byte aligned `double` and `long long int` and a 4-byte aligned
82 // `long double`.
83 //
84 // So far we've covered locals and other non-allocated storage, but we also need
85 // confidence that allocated storage from `malloc`, `new`, etc will also be
86 // suitable for objects with this alignment reuirement.
87 //
88 // Apple document that their implementation of malloc will issue 16-byte
89 // granularity chunks for small allocations (large allocations are page-size
90 // granularity and page-aligned). We think that allocated storage will be
91 // suitable for these objects based on the following assumptions:
92 //
93 // 1. 16-byte granularity also means 16-byte aligned.
94 // 2. `new` and other allocators follow the `malloc` rules.
95 //
96 // We also have some anecdotal evidence: we don't see lots of misaligned-storage
97 // crashes on 32-bit iOS apps that use `double`.
98 //
99 // Apple's allocation reference: http://bit.ly/malloc-small
100 struct alignas(max_align_v) max_align_t {};
101
102 } // namespace folly
103
104 // compiler specific attribute translation
105 // msvc should come first, so if clang is in msvc mode it gets the right defines
106
107 #if defined(__clang__) || defined(__GNUC__)
108 # define FOLLY_ALIGNED(size) __attribute__((__aligned__(size)))
109 #elif defined(_MSC_VER)
110 # define FOLLY_ALIGNED(size) __declspec(align(size))
111 #else
112 # error Cannot define FOLLY_ALIGNED on this platform
113 #endif
114 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(::folly::max_align_v)
115
116 // NOTE: this will only do checking in msvc with versions that support /analyze
117 #if _MSC_VER
118 # ifdef _USE_ATTRIBUTES_FOR_SAL
119 #    undef _USE_ATTRIBUTES_FOR_SAL
120 # endif
121 /* nolint */
122 # define _USE_ATTRIBUTES_FOR_SAL 1
123 # include <sal.h> // @manual
124 # define FOLLY_PRINTF_FORMAT _Printf_format_string_
125 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) /**/
126 #else
127 # define FOLLY_PRINTF_FORMAT /**/
128 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
129   __attribute__((__format__(__printf__, format_param, dots_param)))
130 #endif
131
132 // deprecated
133 #if defined(__clang__) || defined(__GNUC__)
134 # define FOLLY_DEPRECATED(msg) __attribute__((__deprecated__(msg)))
135 #elif defined(_MSC_VER)
136 # define FOLLY_DEPRECATED(msg) __declspec(deprecated(msg))
137 #else
138 # define FOLLY_DEPRECATED(msg)
139 #endif
140
141 // warn unused result
142 #if defined(__has_cpp_attribute)
143 #if __has_cpp_attribute(nodiscard)
144 #define FOLLY_NODISCARD [[nodiscard]]
145 #endif
146 #endif
147 #if !defined FOLLY_NODISCARD
148 #if defined(_MSC_VER) && (_MSC_VER >= 1700)
149 #define FOLLY_NODISCARD _Check_return_
150 #elif defined(__clang__) || defined(__GNUC__)
151 #define FOLLY_NODISCARD __attribute__((__warn_unused_result__))
152 #else
153 #define FOLLY_NODISCARD
154 #endif
155 #endif
156
157 // target
158 #ifdef _MSC_VER
159 # define FOLLY_TARGET_ATTRIBUTE(target)
160 #else
161 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
162 #endif
163
164 // detection for 64 bit
165 #if defined(__x86_64__) || defined(_M_X64)
166 # define FOLLY_X64 1
167 #else
168 # define FOLLY_X64 0
169 #endif
170
171 #if defined(__aarch64__)
172 # define FOLLY_AARCH64 1
173 #else
174 # define FOLLY_AARCH64 0
175 #endif
176
177 #if defined (__powerpc64__)
178 # define FOLLY_PPC64 1
179 #else
180 # define FOLLY_PPC64 0
181 #endif
182
183 namespace folly {
184 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
185 constexpr bool kIsArchAArch64 = FOLLY_AARCH64 == 1;
186 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
187 } // namespace folly
188
189 namespace folly {
190
191 #if FOLLY_SANITIZE_ADDRESS
192 constexpr bool kIsSanitizeAddress = true;
193 #else
194 constexpr bool kIsSanitizeAddress = false;
195 #endif
196
197 #if FOLLY_SANITIZE_THREAD
198 constexpr bool kIsSanitizeThread = true;
199 #else
200 constexpr bool kIsSanitizeThread = false;
201 #endif
202
203 #if FOLLY_SANITIZE
204 constexpr bool kIsSanitize = true;
205 #else
206 constexpr bool kIsSanitize = false;
207 #endif
208 } // namespace folly
209
210 // packing is very ugly in msvc
211 #ifdef _MSC_VER
212 # define FOLLY_PACK_ATTR /**/
213 # define FOLLY_PACK_PUSH __pragma(pack(push, 1))
214 # define FOLLY_PACK_POP __pragma(pack(pop))
215 #elif defined(__clang__) || defined(__GNUC__)
216 # define FOLLY_PACK_ATTR __attribute__((__packed__))
217 # define FOLLY_PACK_PUSH /**/
218 # define FOLLY_PACK_POP /**/
219 #else
220 # define FOLLY_PACK_ATTR /**/
221 # define FOLLY_PACK_PUSH /**/
222 # define FOLLY_PACK_POP /**/
223 #endif
224
225 // Generalize warning push/pop.
226 #if defined(_MSC_VER)
227 # define FOLLY_PUSH_WARNING __pragma(warning(push))
228 # define FOLLY_POP_WARNING __pragma(warning(pop))
229 // Disable the GCC warnings.
230 # define FOLLY_GCC_DISABLE_WARNING(warningName)
231 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber) __pragma(warning(disable: warningNumber))
232 #elif defined(__clang__) || defined(__GNUC__)
233 # define FOLLY_PUSH_WARNING _Pragma("GCC diagnostic push")
234 # define FOLLY_POP_WARNING _Pragma("GCC diagnostic pop")
235 # define FOLLY_GCC_DISABLE_WARNING_INTERNAL2(warningName) #warningName
236 # define FOLLY_GCC_DISABLE_WARNING(warningName) \
237   _Pragma(                                      \
238   FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
239 // Disable the MSVC warnings.
240 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
241 #else
242 # define FOLLY_PUSH_WARNING
243 # define FOLLY_POP_WARNING
244 # define FOLLY_GCC_DISABLE_WARNING(warningName)
245 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
246 #endif
247
248 #ifdef FOLLY_HAVE_SHADOW_LOCAL_WARNINGS
249 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS        \
250   FOLLY_GCC_DISABLE_WARNING("-Wshadow-compatible-local") \
251   FOLLY_GCC_DISABLE_WARNING("-Wshadow-local")
252 #else
253 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
254 #endif
255
256 /* Platform specific TLS support
257  * gcc implements __thread
258  * msvc implements __declspec(thread)
259  * the semantics are the same
260  * (but remember __thread has different semantics when using emutls (ex. apple))
261  */
262 #if defined(_MSC_VER)
263 # define FOLLY_TLS __declspec(thread)
264 #elif defined(__GNUC__) || defined(__clang__)
265 # define FOLLY_TLS __thread
266 #else
267 # error cannot define platform specific thread local storage
268 #endif
269
270 #if FOLLY_MOBILE
271 #undef FOLLY_TLS
272 #endif
273
274 // It turns out that GNU libstdc++ and LLVM libc++ differ on how they implement
275 // the 'std' namespace; the latter uses inline namespaces. Wrap this decision
276 // up in a macro to make forward-declarations easier.
277 #if FOLLY_USE_LIBCPP
278 #include <__config> // @manual
279 #define FOLLY_NAMESPACE_STD_BEGIN     _LIBCPP_BEGIN_NAMESPACE_STD
280 #define FOLLY_NAMESPACE_STD_END       _LIBCPP_END_NAMESPACE_STD
281 #else
282 #define FOLLY_NAMESPACE_STD_BEGIN     namespace std {
283 #define FOLLY_NAMESPACE_STD_END       }
284 #endif
285
286 // If the new c++ ABI is used, __cxx11 inline namespace needs to be added to
287 // some types, e.g. std::list.
288 #if _GLIBCXX_USE_CXX11_ABI
289 #define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN \
290   inline _GLIBCXX_BEGIN_NAMESPACE_CXX11
291 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END   _GLIBCXX_END_NAMESPACE_CXX11
292 #else
293 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
294 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
295 #endif
296
297 // MSVC specific defines
298 // mainly for posix compat
299 #ifdef _MSC_VER
300 #include <folly/portability/SysTypes.h>
301
302 // compiler specific to compiler specific
303 // nolint
304 # define __PRETTY_FUNCTION__ __FUNCSIG__
305
306 // Hide a GCC specific thing that breaks MSVC if left alone.
307 # define __extension__
308
309 // We have compiler support for the newest of the new, but
310 // MSVC doesn't tell us that.
311 #define __SSE4_2__ 1
312
313 #endif
314
315 // Debug
316 namespace folly {
317 #ifdef NDEBUG
318 constexpr auto kIsDebug = false;
319 #else
320 constexpr auto kIsDebug = true;
321 #endif
322 } // namespace folly
323
324 // Endianness
325 namespace folly {
326 #ifdef _MSC_VER
327 // It's MSVC, so we just have to guess ... and allow an override
328 #ifdef FOLLY_ENDIAN_BE
329 constexpr auto kIsLittleEndian = false;
330 #else
331 constexpr auto kIsLittleEndian = true;
332 #endif
333 #else
334 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
335 #endif
336 constexpr auto kIsBigEndian = !kIsLittleEndian;
337 } // namespace folly
338
339 #ifndef FOLLY_SSE
340 # if defined(__SSE4_2__)
341 #  define FOLLY_SSE 4
342 #  define FOLLY_SSE_MINOR 2
343 # elif defined(__SSE4_1__)
344 #  define FOLLY_SSE 4
345 #  define FOLLY_SSE_MINOR 1
346 # elif defined(__SSE4__)
347 #  define FOLLY_SSE 4
348 #  define FOLLY_SSE_MINOR 0
349 # elif defined(__SSE3__)
350 #  define FOLLY_SSE 3
351 #  define FOLLY_SSE_MINOR 0
352 # elif defined(__SSE2__)
353 #  define FOLLY_SSE 2
354 #  define FOLLY_SSE_MINOR 0
355 # elif defined(__SSE__)
356 #  define FOLLY_SSE 1
357 #  define FOLLY_SSE_MINOR 0
358 # else
359 #  define FOLLY_SSE 0
360 #  define FOLLY_SSE_MINOR 0
361 # endif
362 #endif
363
364 #define FOLLY_SSE_PREREQ(major, minor) \
365   (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
366
367 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
368 namespace FOLLY_GFLAGS_NAMESPACE { }
369 namespace gflags {
370 using namespace FOLLY_GFLAGS_NAMESPACE;
371 } // namespace gflags
372 #endif
373
374 // for TARGET_OS_IPHONE
375 #ifdef __APPLE__
376 #include <TargetConditionals.h> // @manual
377 #endif
378
379 // RTTI may not be enabled for this compilation unit.
380 #if defined(__GXX_RTTI) || defined(__cpp_rtti) || \
381     (defined(_MSC_VER) && defined(_CPPRTTI))
382 # define FOLLY_HAS_RTTI 1
383 #endif
384
385 #if defined(__APPLE__) || defined(_MSC_VER)
386 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
387 #else
388 // 101 is the highest priority allowed by the init_priority attribute.
389 // This priority is already used by JEMalloc and other memory allocators so
390 // we will take the next one.
391 #define FOLLY_STATIC_CTOR_PRIORITY_MAX __attribute__((__init_priority__(102)))
392 #endif
393
394 namespace folly {
395
396 #if __OBJC__
397 constexpr auto kIsObjC = true;
398 #else
399 constexpr auto kIsObjC = false;
400 #endif
401
402 #if FOLLY_MOBILE
403 constexpr auto kIsMobile = true;
404 #else
405 constexpr auto kIsMobile = false;
406 #endif
407
408 #if defined(__linux__) && !FOLLY_MOBILE
409 constexpr auto kIsLinux = true;
410 #else
411 constexpr auto kIsLinux = false;
412 #endif
413
414 #if defined(_WIN32)
415 constexpr auto kIsWindows = true;
416 constexpr auto kMscVer = _MSC_VER;
417 #else
418 constexpr auto kIsWindows = false;
419 constexpr auto kMscVer = 0;
420 #endif
421 } // namespace folly
422
423 // Define FOLLY_USE_CPP14_CONSTEXPR to be true if the compiler's C++14
424 // constexpr support is "good enough".
425 #ifndef FOLLY_USE_CPP14_CONSTEXPR
426 #if defined(__clang__)
427 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201300L
428 #elif defined(__GNUC__)
429 #define FOLLY_USE_CPP14_CONSTEXPR __cplusplus >= 201304L
430 #else
431 #define FOLLY_USE_CPP14_CONSTEXPR 0 // MSVC?
432 #endif
433 #endif
434
435 #if FOLLY_USE_CPP14_CONSTEXPR
436 #define FOLLY_CPP14_CONSTEXPR constexpr
437 #else
438 #define FOLLY_CPP14_CONSTEXPR inline
439 #endif
440
441 #if __cpp_coroutines >= 201703L || (_MSC_VER && _RESUMABLE_FUNCTIONS_SUPPORTED)
442 #define FOLLY_HAS_COROUTINES 1
443 #endif
444
445 // MSVC 2017.5
446 #if __cpp_noexcept_function_type >= 201510 || _MSC_FULL_VER >= 191225816
447 #define FOLLY_HAVE_NOEXCEPT_FUNCTION_TYPE 1
448 #endif