1 //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AlignOf function that computes alignments for
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_ALIGNOF_H
16 #define LLVM_SUPPORT_ALIGNOF_H
18 #include "llvm/Support/Compiler.h"
23 struct AlignmentCalcImpl {
26 // Disables "structure was padded due to __declspec(align())" warnings that are
27 // generated by any class using AlignOf<T> with a manually specified alignment.
28 // Although the warning is disabled in the LLVM project we need this pragma
29 // as AlignOf.h is a published support header that's available for use
30 // out-of-tree, and we would like that to compile cleanly at /W4.
31 #pragma warning(suppress : 4324)
35 AlignmentCalcImpl() {} // Never instantiate.
38 /// AlignOf - A templated class that contains an enum value representing
39 /// the alignment of the template argument. For example,
40 /// AlignOf<int>::Alignment represents the alignment of type "int". The
41 /// alignment calculated is the minimum alignment, and not necessarily
42 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
43 /// that because the alignment is an enum value, it can be used as a
44 /// compile-time constant (e.g., for template instantiation).
48 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
50 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
51 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
52 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
53 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
55 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
56 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
57 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
58 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
61 /// alignOf - A templated function that returns the minimum alignment of
62 /// of a type. This provides no extra functionality beyond the AlignOf
63 /// class besides some cosmetic cleanliness. Example usage:
64 /// alignOf<int>() returns the alignment of an int.
66 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
68 /// \struct AlignedCharArray
69 /// \brief Helper for building an aligned character array type.
71 /// This template is used to explicitly build up a collection of aligned
72 /// character array types. We have to build these up using a macro and explicit
73 /// specialization to cope with old versions of MSVC and GCC where only an
74 /// integer literal can be used to specify an alignment constraint. Once built
75 /// up here, we can then begin to indirect between these using normal C++
76 /// template parameters.
78 // MSVC requires special handling here.
81 #if __has_feature(cxx_alignas)
82 template<std::size_t Alignment, std::size_t Size>
83 struct AlignedCharArray {
84 alignas(Alignment) char buffer[Size];
87 #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
88 /// \brief Create a type with an aligned char buffer.
89 template<std::size_t Alignment, std::size_t Size>
90 struct AlignedCharArray;
92 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
93 template<std::size_t Size> \
94 struct AlignedCharArray<x, Size> { \
95 __attribute__((aligned(x))) char buffer[Size]; \
98 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
99 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
100 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
101 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
102 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
103 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
104 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
105 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
107 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
110 # error No supported align as directive.
115 /// \brief Create a type with an aligned char buffer.
116 template<std::size_t Alignment, std::size_t Size>
117 struct AlignedCharArray;
119 // We provide special variations of this template for the most common
120 // alignments because __declspec(align(...)) doesn't actually work when it is
121 // a member of a by-value function argument in MSVC, even if the alignment
122 // request is something reasonably like 8-byte or 16-byte. Note that we can't
123 // even include the declspec with the union that forces the alignment because
124 // MSVC warns on the existence of the declspec despite the union member forcing
127 template<std::size_t Size>
128 struct AlignedCharArray<1, Size> {
135 template<std::size_t Size>
136 struct AlignedCharArray<2, Size> {
143 template<std::size_t Size>
144 struct AlignedCharArray<4, Size> {
151 template<std::size_t Size>
152 struct AlignedCharArray<8, Size> {
160 // The rest of these are provided with a __declspec(align(...)) and we simply
161 // can't pass them by-value as function arguments on MSVC.
163 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
164 template<std::size_t Size> \
165 struct AlignedCharArray<x, Size> { \
166 __declspec(align(x)) char buffer[Size]; \
169 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
170 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
171 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
172 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
174 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
179 template <typename T1,
180 typename T2 = char, typename T3 = char, typename T4 = char,
181 typename T5 = char, typename T6 = char, typename T7 = char,
182 typename T8 = char, typename T9 = char, typename T10 = char>
184 T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
186 AlignerImpl(); // Never defined or instantiated.
189 template <typename T1,
190 typename T2 = char, typename T3 = char, typename T4 = char,
191 typename T5 = char, typename T6 = char, typename T7 = char,
192 typename T8 = char, typename T9 = char, typename T10 = char>
194 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
195 arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
196 arr9[sizeof(T9)], arr10[sizeof(T10)];
198 } // end namespace detail
200 /// \brief This union template exposes a suitably aligned and sized character
201 /// array member which can hold elements of any of up to ten types.
203 /// These types may be arrays, structs, or any other types. The goal is to
204 /// expose a char array buffer member which can be used as suitable storage for
205 /// a placement new of any of these types. Support for more than ten types can
206 /// be added at the cost of more boilerplate.
207 template <typename T1,
208 typename T2 = char, typename T3 = char, typename T4 = char,
209 typename T5 = char, typename T6 = char, typename T7 = char,
210 typename T8 = char, typename T9 = char, typename T10 = char>
211 struct AlignedCharArrayUnion : llvm::AlignedCharArray<
212 AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5,
213 T6, T7, T8, T9, T10> >::Alignment,
214 sizeof(detail::SizerImpl<T1, T2, T3, T4, T5,
215 T6, T7, T8, T9, T10>)> {
217 } // end namespace llvm