2 * Copyright 2017 Facebook, Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <type_traits>
24 #include <folly/portability/Config.h>
26 #include <folly/CPortability.h>
28 // Unaligned loads and stores
30 #if FOLLY_HAVE_UNALIGNED_ACCESS
31 constexpr bool kHasUnalignedAccess = true;
33 constexpr bool kHasUnalignedAccess = false;
38 template <typename I, I A, I... Bs>
40 : std::integral_constant<
42 (A > integral_max<I, Bs...>::value) ? A
43 : integral_max<I, Bs...>::value> {
46 template <typename I, size_t A>
47 struct integral_max<I, A> : std::integral_constant<I, A> {};
49 template <typename... Ts>
50 using max_alignment = integral_max<size_t, alignof(Ts)...>;
52 using max_basic_alignment = max_alignment<
69 constexpr size_t max_align_v = detail::max_basic_alignment::value;
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).
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.
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
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.
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:
94 // 1. 16-byte granularity also means 16-byte aligned.
95 // 2. `new` and other allocators follow the `malloc` rules.
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`.
100 // Apple's allocation reference: http://bit.ly/malloc-small
101 struct alignas(max_align_v) max_align_t {};
105 // compiler specific attribute translation
106 // msvc should come first, so if clang is in msvc mode it gets the right defines
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))
113 # error Cannot define FOLLY_ALIGNED on this platform
115 #define FOLLY_ALIGNED_MAX FOLLY_ALIGNED(::folly::max_align_v)
117 // NOTE: this will only do checking in msvc with versions that support /analyze
119 # ifdef _USE_ATTRIBUTES_FOR_SAL
120 # undef _USE_ATTRIBUTES_FOR_SAL
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) /**/
128 # define FOLLY_PRINTF_FORMAT /**/
129 # define FOLLY_PRINTF_FORMAT_ATTR(format_param, dots_param) \
130 __attribute__((__format__(__printf__, format_param, dots_param)))
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))
139 # define FOLLY_DEPRECATED(msg)
142 // warn unused result
143 #if defined(__has_cpp_attribute)
144 #if __has_cpp_attribute(nodiscard)
145 #define FOLLY_NODISCARD [[nodiscard]]
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__))
154 #define FOLLY_NODISCARD
160 # define FOLLY_TARGET_ATTRIBUTE(target)
162 # define FOLLY_TARGET_ATTRIBUTE(target) __attribute__((__target__(target)))
165 // detection for 64 bit
166 #if defined(__x86_64__) || defined(_M_X64)
172 #if defined(__aarch64__)
178 #if defined (__powerpc64__)
179 # define FOLLY_PPC64 1
181 # define FOLLY_PPC64 0
185 constexpr bool kIsArchAmd64 = FOLLY_X64 == 1;
186 constexpr bool kIsArchAArch64 = FOLLY_A64 == 1;
187 constexpr bool kIsArchPPC64 = FOLLY_PPC64 == 1;
192 #if FOLLY_SANITIZE_ADDRESS
193 constexpr bool kIsSanitizeAddress = true;
195 constexpr bool kIsSanitizeAddress = false;
198 #if FOLLY_SANITIZE_THREAD
199 constexpr bool kIsSanitizeThread = true;
201 constexpr bool kIsSanitizeThread = false;
205 // packing is very ugly in msvc
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 /**/
215 # define FOLLY_PACK_ATTR /**/
216 # define FOLLY_PACK_PUSH /**/
217 # define FOLLY_PACK_POP /**/
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) \
233 FOLLY_GCC_DISABLE_WARNING_INTERNAL2(GCC diagnostic ignored warningName))
234 // Disable the MSVC warnings.
235 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
237 # define FOLLY_PUSH_WARNING
238 # define FOLLY_POP_WARNING
239 # define FOLLY_GCC_DISABLE_WARNING(warningName)
240 # define FOLLY_MSVC_DISABLE_WARNING(warningNumber)
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")
248 #define FOLLY_GCC_DISABLE_NEW_SHADOW_WARNINGS /* empty */
251 // portable version check for clang
252 #ifndef __CLANG_PREREQ
253 # if defined __clang__ && defined __clang_major__ && defined __clang_minor__
255 # define __CLANG_PREREQ(maj, min) \
256 ((__clang_major__ << 16) + __clang_minor__ >= ((maj) << 16) + (min))
259 # define __CLANG_PREREQ(maj, min) 0
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))
269 #if defined(_MSC_VER)
270 # define FOLLY_TLS __declspec(thread)
271 #elif defined(__GNUC__) || defined(__clang__)
272 # define FOLLY_TLS __thread
274 # error cannot define platform specific thread local storage
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.
285 #include <__config> // @manual
286 #define FOLLY_NAMESPACE_STD_BEGIN _LIBCPP_BEGIN_NAMESPACE_STD
287 #define FOLLY_NAMESPACE_STD_END _LIBCPP_END_NAMESPACE_STD
289 #define FOLLY_NAMESPACE_STD_BEGIN namespace std {
290 #define FOLLY_NAMESPACE_STD_END }
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
300 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_BEGIN
301 # define FOLLY_GLIBCXX_NAMESPACE_CXX11_END
304 // MSVC specific defines
305 // mainly for posix compat
307 #include <folly/portability/SysTypes.h>
309 // compiler specific to compiler specific
311 # define __PRETTY_FUNCTION__ __FUNCSIG__
313 // Hide a GCC specific thing that breaks MSVC if left alone.
314 # define __extension__
316 // We have compiler support for the newest of the new, but
317 // MSVC doesn't tell us that.
325 constexpr auto kIsDebug = false;
327 constexpr auto kIsDebug = true;
334 // It's MSVC, so we just have to guess ... and allow an override
335 #ifdef FOLLY_ENDIAN_BE
336 constexpr auto kIsLittleEndian = false;
338 constexpr auto kIsLittleEndian = true;
341 constexpr auto kIsLittleEndian = __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__;
343 constexpr auto kIsBigEndian = !kIsLittleEndian;
347 # if defined(__SSE4_2__)
349 # define FOLLY_SSE_MINOR 2
350 # elif defined(__SSE4_1__)
352 # define FOLLY_SSE_MINOR 1
353 # elif defined(__SSE4__)
355 # define FOLLY_SSE_MINOR 0
356 # elif defined(__SSE3__)
358 # define FOLLY_SSE_MINOR 0
359 # elif defined(__SSE2__)
361 # define FOLLY_SSE_MINOR 0
362 # elif defined(__SSE__)
364 # define FOLLY_SSE_MINOR 0
367 # define FOLLY_SSE_MINOR 0
371 #define FOLLY_SSE_PREREQ(major, minor) \
372 (FOLLY_SSE > major || FOLLY_SSE == major && FOLLY_SSE_MINOR >= minor)
374 #if FOLLY_UNUSUAL_GFLAGS_NAMESPACE
375 namespace FOLLY_GFLAGS_NAMESPACE { }
377 using namespace FOLLY_GFLAGS_NAMESPACE;
378 } // namespace gflags
381 // for TARGET_OS_IPHONE
383 #include <TargetConditionals.h> // @manual
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
392 #if defined(__APPLE__) || defined(_MSC_VER)
393 #define FOLLY_STATIC_CTOR_PRIORITY_MAX
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)))
404 constexpr auto kIsObjC = true;
406 constexpr auto kIsObjC = false;
409 #if defined(__linux__) && !FOLLY_MOBILE
410 constexpr auto kIsLinux = true;
412 constexpr auto kIsLinux = false;
416 constexpr auto kIsWindows = true;
417 constexpr auto kMscVer = _MSC_VER;
419 constexpr auto kIsWindows = false;
420 constexpr auto kMscVer = 0;