4f0564dccd62fbd7d821efcdb95079468f697b4d
[folly.git] / folly / lang / Align.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
21 namespace folly {
22
23 namespace detail {
24
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) {
28   return a;
29 }
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...);
33 }
34 template <typename... Ts>
35 struct max_align_t_ {
36   static constexpr std::size_t value = max_align_(0u, alignof(Ts)...);
37 };
38 using max_align_v_ = max_align_t_<
39     long double,
40     double,
41     float,
42     long long int,
43     long int,
44     int,
45     short int,
46     bool,
47     char,
48     char16_t,
49     char32_t,
50     wchar_t,
51     void*,
52     std::max_align_t>;
53
54 } // namespace detail
55
56 // max_align_v is the alignment of max_align_t.
57 //
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).
62 //
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.
66 //
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
70 // `long double`.
71 //
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.
75 //
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:
80 //
81 // 1. 16-byte granularity also means 16-byte aligned.
82 // 2. `new` and other allocators follow the `malloc` rules.
83 //
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`.
86 //
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 {};
90
91 } // namespace folly