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 {
27 AlignmentCalcImpl() {} // Never instantiate.
30 /// AlignOf - A templated class that contains an enum value representing
31 /// the alignment of the template argument. For example,
32 /// AlignOf<int>::Alignment represents the alignment of type "int". The
33 /// alignment calculated is the minimum alignment, and not necessarily
34 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
35 /// that because the alignment is an enum value, it can be used as a
36 /// compile-time constant (e.g., for template instantiation).
40 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
42 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
43 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
44 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
45 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
47 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
48 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
49 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
50 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
53 /// alignOf - A templated function that returns the minimum alignment of
54 /// of a type. This provides no extra functionality beyond the AlignOf
55 /// class besides some cosmetic cleanliness. Example usage:
56 /// alignOf<int>() returns the alignment of an int.
58 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
60 /// \struct AlignedCharArray
61 /// \brief Helper for building an aligned character array type.
63 /// This template is used to explicitly build up a collection of aligned
64 /// character array types. We have to build these up using a macro and explicit
65 /// specialization to cope with old versions of MSVC and GCC where only an
66 /// integer literal can be used to specify an alignment constraint. Once built
67 /// up here, we can then begin to indirect between these using normal C++
68 /// template parameters.
70 // MSVC requires special handling here.
73 #if __has_feature(cxx_alignas)
74 template<std::size_t Alignment, std::size_t Size>
75 struct AlignedCharArray {
76 alignas(Alignment) char buffer[Size];
79 #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
80 /// \brief Create a type with an aligned char buffer.
81 template<std::size_t Alignment, std::size_t Size>
82 struct AlignedCharArray;
84 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
85 template<std::size_t Size> \
86 struct AlignedCharArray<x, Size> { \
87 __attribute__((aligned(x))) char buffer[Size]; \
90 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
91 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
92 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
93 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
94 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
95 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
96 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
97 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
99 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
102 # error No supported align as directive.
107 /// \brief Create a type with an aligned char buffer.
108 template<std::size_t Alignment, std::size_t Size>
109 struct AlignedCharArray;
111 // We provide special variations of this template for the most common
112 // alignments because __declspec(align(...)) doesn't actually work when it is
113 // a member of a by-value function argument in MSVC, even if the alignment
114 // request is something reasonably like 8-byte or 16-byte.
116 template<std::size_t Size>
117 struct AlignedCharArray<1, Size> {
120 __declspec(align(1)) char buffer[Size];
124 template<std::size_t Size>
125 struct AlignedCharArray<2, Size> {
128 __declspec(align(2)) char buffer[Size];
132 template<std::size_t Size>
133 struct AlignedCharArray<4, Size> {
136 __declspec(align(4)) char buffer[Size];
140 template<std::size_t Size>
141 struct AlignedCharArray<8, Size> {
144 __declspec(align(8)) char buffer[Size];
148 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
149 template<std::size_t Size> \
150 struct AlignedCharArray<x, Size> { \
151 __declspec(align(x)) char buffer[Size]; \
154 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
155 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
156 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
157 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
159 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
164 template <typename T1,
165 typename T2 = char, typename T3 = char, typename T4 = char>
167 T1 t1; T2 t2; T3 t3; T4 t4;
169 AlignerImpl(); // Never defined or instantiated.
172 template <typename T1,
173 typename T2 = char, typename T3 = char, typename T4 = char>
175 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)];
177 } // end namespace detail
179 /// \brief This union template exposes a suitably aligned and sized character
180 /// array member which can hold elements of any of up to four types.
182 /// These types may be arrays, structs, or any other types. The goal is to
183 /// expose a char array buffer member which can be used as suitable storage for
184 /// a placement new of any of these types. Support for more than four types can
185 /// be added at the cost of more boiler plate.
186 template <typename T1,
187 typename T2 = char, typename T3 = char, typename T4 = char>
188 struct AlignedCharArrayUnion :
189 llvm::AlignedCharArray<AlignOf<detail::AlignerImpl<T1, T2, T3, T4> >
191 sizeof(detail::SizerImpl<T1, T2, T3, T4>)> {
193 } // end namespace llvm