llvm-AlignOf.h 6.4 KB

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