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