AlignOf.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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. #ifndef LLVM_SUPPORT_ALIGNOF_H
  15. #define LLVM_SUPPORT_ALIGNOF_H
  16. #include "Compiler.h"
  17. #include <cstddef>
  18. #include <type_traits>
  19. namespace llvm {
  20. namespace detail {
  21. // For everything other than an abstract class we can calulate alignment by
  22. // building a class with a single character and a member of the given type.
  23. template <typename T, bool = std::is_abstract<T>::value>
  24. struct AlignmentCalcImpl {
  25. char x;
  26. #if defined(_MSC_VER)
  27. // Disables "structure was padded due to __declspec(align())" warnings that are
  28. // generated by any class using AlignOf<T> with a manually specified alignment.
  29. // Although the warning is disabled in the LLVM project we need this pragma
  30. // as AlignOf.h is a published support header that's available for use
  31. // out-of-tree, and we would like that to compile cleanly at /W4.
  32. #pragma warning(suppress : 4324)
  33. #endif
  34. T t;
  35. private:
  36. AlignmentCalcImpl() = delete;
  37. };
  38. // Abstract base class helper, this will have the minimal alignment and size
  39. // for any abstract class. We don't even define its destructor because this
  40. // type should never be used in a way that requires it.
  41. struct AlignmentCalcImplBase {
  42. virtual ~AlignmentCalcImplBase() = 0;
  43. };
  44. // When we have an abstract class type, specialize the alignment computation
  45. // engine to create another abstract class that derives from both an empty
  46. // abstract base class and the provided type. This has the same effect as the
  47. // above except that it handles the fact that we can't actually create a member
  48. // of type T.
  49. template <typename T>
  50. struct AlignmentCalcImpl<T, true> : AlignmentCalcImplBase, T {
  51. ~AlignmentCalcImpl() override = 0;
  52. };
  53. } // End detail namespace.
  54. /// AlignOf - A templated class that contains an enum value representing
  55. /// the alignment of the template argument. For example,
  56. /// AlignOf<int>::Alignment represents the alignment of type "int". The
  57. /// alignment calculated is the minimum alignment, and not necessarily
  58. /// the "desired" alignment returned by GCC's __alignof__ (for example). Note
  59. /// that because the alignment is an enum value, it can be used as a
  60. /// compile-time constant (e.g., for template instantiation).
  61. template <typename T>
  62. struct AlignOf {
  63. #ifndef _MSC_VER
  64. // Avoid warnings from GCC like:
  65. // comparison between 'enum llvm::AlignOf<X>::<anonymous>' and 'enum
  66. // llvm::AlignOf<Y>::<anonymous>' [-Wenum-compare]
  67. // by using constexpr instead of enum.
  68. // (except on MSVC, since it doesn't support constexpr yet).
  69. static constexpr unsigned Alignment = static_cast<unsigned int>(
  70. sizeof(detail::AlignmentCalcImpl<T>) - sizeof(T));
  71. #else
  72. enum {
  73. Alignment = static_cast<unsigned int>(
  74. sizeof(::llvm::detail::AlignmentCalcImpl<T>) - sizeof(T))
  75. };
  76. #endif
  77. enum { Alignment_GreaterEqual_2Bytes = Alignment >= 2 ? 1 : 0 };
  78. enum { Alignment_GreaterEqual_4Bytes = Alignment >= 4 ? 1 : 0 };
  79. enum { Alignment_GreaterEqual_8Bytes = Alignment >= 8 ? 1 : 0 };
  80. enum { Alignment_GreaterEqual_16Bytes = Alignment >= 16 ? 1 : 0 };
  81. enum { Alignment_LessEqual_2Bytes = Alignment <= 2 ? 1 : 0 };
  82. enum { Alignment_LessEqual_4Bytes = Alignment <= 4 ? 1 : 0 };
  83. enum { Alignment_LessEqual_8Bytes = Alignment <= 8 ? 1 : 0 };
  84. enum { Alignment_LessEqual_16Bytes = Alignment <= 16 ? 1 : 0 };
  85. };
  86. #ifndef _MSC_VER
  87. template <typename T> constexpr unsigned AlignOf<T>::Alignment;
  88. #endif
  89. /// alignOf - A templated function that returns the minimum alignment of
  90. /// of a type. This provides no extra functionality beyond the AlignOf
  91. /// class besides some cosmetic cleanliness. Example usage:
  92. /// alignOf<int>() returns the alignment of an int.
  93. template <typename T>
  94. inline unsigned alignOf() { return AlignOf<T>::Alignment; }
  95. /// \struct AlignedCharArray
  96. /// \brief Helper for building an aligned character array type.
  97. ///
  98. /// This template is used to explicitly build up a collection of aligned
  99. /// character array types. We have to build these up using a macro and explicit
  100. /// specialization to cope with old versions of MSVC and GCC where only an
  101. /// integer literal can be used to specify an alignment constraint. Once built
  102. /// up here, we can then begin to indirect between these using normal C++
  103. /// template parameters.
  104. // MSVC requires special handling here.
  105. #ifndef _MSC_VER
  106. #if __has_feature(cxx_alignas)
  107. template<std::size_t Alignment, std::size_t Size>
  108. struct AlignedCharArray {
  109. alignas(Alignment) char buffer[Size];
  110. };
  111. #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
  112. /// \brief Create a type with an aligned char buffer.
  113. template<std::size_t Alignment, std::size_t Size>
  114. struct AlignedCharArray;
  115. #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
  116. template<std::size_t Size> \
  117. struct AlignedCharArray<x, Size> { \
  118. __attribute__((aligned(x))) char buffer[Size]; \
  119. };
  120. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
  121. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
  122. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
  123. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
  124. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
  125. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
  126. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
  127. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
  128. #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
  129. #else
  130. # error No supported align as directive.
  131. #endif
  132. #else // _MSC_VER
  133. /// \brief Create a type with an aligned char buffer.
  134. template<std::size_t Alignment, std::size_t Size>
  135. struct AlignedCharArray;
  136. // We provide special variations of this template for the most common
  137. // alignments because __declspec(align(...)) doesn't actually work when it is
  138. // a member of a by-value function argument in MSVC, even if the alignment
  139. // request is something reasonably like 8-byte or 16-byte. Note that we can't
  140. // even include the declspec with the union that forces the alignment because
  141. // MSVC warns on the existence of the declspec despite the union member forcing
  142. // proper alignment.
  143. template<std::size_t Size>
  144. struct AlignedCharArray<1, Size> {
  145. union {
  146. char aligned;
  147. char buffer[Size];
  148. };
  149. };
  150. template<std::size_t Size>
  151. struct AlignedCharArray<2, Size> {
  152. union {
  153. short aligned;
  154. char buffer[Size];
  155. };
  156. };
  157. template<std::size_t Size>
  158. struct AlignedCharArray<4, Size> {
  159. union {
  160. int aligned;
  161. char buffer[Size];
  162. };
  163. };
  164. template<std::size_t Size>
  165. struct AlignedCharArray<8, Size> {
  166. union {
  167. double aligned;
  168. char buffer[Size];
  169. };
  170. };
  171. // The rest of these are provided with a __declspec(align(...)) and we simply
  172. // can't pass them by-value as function arguments on MSVC.
  173. #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
  174. template<std::size_t Size> \
  175. struct AlignedCharArray<x, Size> { \
  176. __declspec(align(x)) char buffer[Size]; \
  177. };
  178. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
  179. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
  180. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
  181. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
  182. #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
  183. #endif // _MSC_VER
  184. namespace detail {
  185. template <typename T1,
  186. typename T2 = char, typename T3 = char, typename T4 = char,
  187. typename T5 = char, typename T6 = char, typename T7 = char,
  188. typename T8 = char, typename T9 = char, typename T10 = char>
  189. class AlignerImpl {
  190. T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7; T8 t8; T9 t9; T10 t10;
  191. AlignerImpl() = delete;
  192. };
  193. template <typename T1,
  194. typename T2 = char, typename T3 = char, typename T4 = char,
  195. typename T5 = char, typename T6 = char, typename T7 = char,
  196. typename T8 = char, typename T9 = char, typename T10 = char>
  197. union SizerImpl {
  198. char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
  199. arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)], arr8[sizeof(T8)],
  200. arr9[sizeof(T9)], arr10[sizeof(T10)];
  201. };
  202. } // end namespace detail
  203. /// \brief This union template exposes a suitably aligned and sized character
  204. /// array member which can hold elements of any of up to ten types.
  205. ///
  206. /// These types may be arrays, structs, or any other types. The goal is to
  207. /// expose a char array buffer member which can be used as suitable storage for
  208. /// a placement new of any of these types. Support for more than ten types can
  209. /// be added at the cost of more boilerplate.
  210. template <typename T1,
  211. typename T2 = char, typename T3 = char, typename T4 = char,
  212. typename T5 = char, typename T6 = char, typename T7 = char,
  213. typename T8 = char, typename T9 = char, typename T10 = char>
  214. struct AlignedCharArrayUnion : llvm::AlignedCharArray<
  215. AlignOf<llvm::detail::AlignerImpl<T1, T2, T3, T4, T5,
  216. T6, T7, T8, T9, T10> >::Alignment,
  217. sizeof(::llvm::detail::SizerImpl<T1, T2, T3, T4, T5,
  218. T6, T7, T8, T9, T10>)> {
  219. };
  220. } // end namespace llvm
  221. #endif // LLVM_SUPPORT_ALIGNOF_H