2 * Copyright 2017-present 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.
25 // Implemented this way because of a bug in Clang for ARMv7, which gives the
26 // wrong result for `alignof` a `union` with a field of each scalar type.
27 constexpr size_t max_align_(std::size_t a) {
30 template <typename... Es>
31 constexpr std::size_t max_align_(std::size_t a, std::size_t e, Es... es) {
32 return !(a < e) ? a : max_align_(e, es...);
34 template <typename... Ts>
36 static constexpr std::size_t value = max_align_(0u, alignof(Ts)...);
38 using max_align_v_ = max_align_t_<
56 // max_align_v is the alignment of max_align_t.
58 // max_align_t is a type which is aligned at least as strictly as the
59 // most-aligned basic type (see the specification of std::max_align_t). This
60 // implementation exists because 32-bit iOS platforms have a broken
61 // std::max_align_t (see below).
63 // You should refer to this as `::folly::max_align_t` in portable code, even if
64 // you have `using namespace folly;` because C11 defines a global namespace
65 // `max_align_t` type.
67 // To be certain, we consider every non-void fundamental type specified by the
68 // standard. On most platforms `long double` would be enough, but iOS 32-bit
69 // has an 8-byte aligned `double` and `long long int` and a 4-byte aligned
72 // So far we've covered locals and other non-allocated storage, but we also need
73 // confidence that allocated storage from `malloc`, `new`, etc will also be
74 // suitable for objects with this alignment requirement.
76 // Apple document that their implementation of malloc will issue 16-byte
77 // granularity chunks for small allocations (large allocations are page-size
78 // granularity and page-aligned). We think that allocated storage will be
79 // suitable for these objects based on the following assumptions:
81 // 1. 16-byte granularity also means 16-byte aligned.
82 // 2. `new` and other allocators follow the `malloc` rules.
84 // We also have some anecdotal evidence: we don't see lots of misaligned-storage
85 // crashes on 32-bit iOS apps that use `double`.
87 // Apple's allocation reference: http://bit.ly/malloc-small
88 constexpr std::size_t max_align_v = detail::max_align_v_::value;
89 struct alignas(max_align_v) max_align_t {};