]> git.saurik.com Git - apple/objc4.git/blob - runtime/llvm-AlignOf.h
objc4-709.tar.gz
[apple/objc4.git] / runtime / llvm-AlignOf.h
1 //===--- AlignOf.h - Portable calculation of type alignment -----*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the AlignOf function that computes alignments for
11 // arbitrary types.
12 //
13 //===----------------------------------------------------------------------===//
14
15 // Taken from llvmCore-3425.0.31.
16
17 #ifndef LLVM_SUPPORT_ALIGNOF_H
18 #define LLVM_SUPPORT_ALIGNOF_H
19
20 #include <cstddef>
21
22 namespace objc {
23
24 template <typename T>
25 struct AlignmentCalcImpl {
26 char x;
27 T t;
28 private:
29 AlignmentCalcImpl() {} // Never instantiate.
30 };
31
32 /// AlignOf - A templated class that contains an enum value representing
33 /// the alignment of the template argument. For example,
34 /// AlignOf<int>::Alignment represents the alignment of type "int". The
35 /// alignment calculated is the minimum alignment, and not necessarily
36 /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
37 /// that because the alignment is an enum value, it can be used as a
38 /// compile-time constant (e.g., for template instantiation).
39 template <typename T>
40 struct AlignOf {
41 enum { Alignment =
42 static_cast<unsigned int>(sizeof(AlignmentCalcImpl<T>) - sizeof(T)) };
43
44 enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
45 enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
46 enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
47 enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
48
49 enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
50 enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
51 enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
52 enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
53
54 };
55
56 /// alignOf - A templated function that returns the minimum alignment of
57 /// of a type. This provides no extra functionality beyond the AlignOf
58 /// class besides some cosmetic cleanliness. Example usage:
59 /// alignOf<int>() returns the alignment of an int.
60 template <typename T>
61 inline unsigned alignOf() { return AlignOf<T>::Alignment; }
62
63
64 /// \brief Helper for building an aligned character array type.
65 ///
66 /// This template is used to explicitly build up a collection of aligned
67 /// character types. We have to build these up using a macro and explicit
68 /// specialization to cope with old versions of MSVC and GCC where only an
69 /// integer literal can be used to specify an alignment constraint. Once built
70 /// up here, we can then begin to indirect between these using normal C++
71 /// template parameters.
72 template <size_t Alignment> struct AlignedCharArrayImpl;
73
74 // MSVC requires special handling here.
75 #ifndef _MSC_VER
76
77 #if __has_feature(cxx_alignas)
78 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
79 template <> struct AlignedCharArrayImpl<x> { \
80 char aligned alignas(x); \
81 }
82 #elif defined(__GNUC__)
83 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
84 template <> struct AlignedCharArrayImpl<x> { \
85 char aligned __attribute__((aligned(x))); \
86 }
87 #else
88 # error No supported align as directive.
89 #endif
90
91 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1);
92 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2);
93 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4);
94 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8);
95 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
96 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
97 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64);
98 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128);
99 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(512);
100 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1024);
101 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2048);
102 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4096);
103 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8192);
104
105 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
106
107 #else // _MSC_VER
108
109 // We provide special variations of this template for the most common
110 // alignments because __declspec(align(...)) doesn't actually work when it is
111 // a member of a by-value function argument in MSVC, even if the alignment
112 // request is something reasonably like 8-byte or 16-byte.
113 template <> struct AlignedCharArrayImpl<1> { char aligned; };
114 template <> struct AlignedCharArrayImpl<2> { short aligned; };
115 template <> struct AlignedCharArrayImpl<4> { int aligned; };
116 template <> struct AlignedCharArrayImpl<8> { double aligned; };
117
118 #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
119 template <> struct AlignedCharArrayImpl<x> { \
120 __declspec(align(x)) char aligned; \
121 }
122 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16);
123 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32);
124 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64);
125 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128);
126 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(512);
127 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1024);
128 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2048);
129 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4096);
130 LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8192);
131 // Any larger and MSVC complains.
132 #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
133
134 #endif // _MSC_VER
135
136 /// \brief This union template exposes a suitably aligned and sized character
137 /// array member which can hold elements of any of up to four types.
138 ///
139 /// These types may be arrays, structs, or any other types. The goal is to
140 /// produce a union type containing a character array which, when used, forms
141 /// storage suitable to placement new any of these types over. Support for more
142 /// than four types can be added at the cost of more boiler plate.
143 template <typename T1,
144 typename T2 = char, typename T3 = char, typename T4 = char>
145 union AlignedCharArrayUnion {
146 private:
147 class AlignerImpl {
148 T1 t1; T2 t2; T3 t3; T4 t4;
149
150 AlignerImpl(); // Never defined or instantiated.
151 };
152 union SizerImpl {
153 char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)];
154 };
155
156 public:
157 /// \brief The character array buffer for use by clients.
158 ///
159 /// No other member of this union should be referenced. The exist purely to
160 /// constrain the layout of this character array.
161 char buffer[sizeof(SizerImpl)];
162
163 private:
164 // Tests seem to indicate that both Clang and GCC will properly register the
165 // alignment of a struct containing an aligned member, and this alignment
166 // should carry over to the character array in the union.
167 AlignedCharArrayImpl<AlignOf<AlignerImpl>::Alignment> nonce_member;
168 };
169
170 } // end namespace objc
171 #endif