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