Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

204 lines
6.8 KiB

  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 "llvm/Support/Compiler.h"
  17. #include <cstddef>
  18. namespace llvm {
  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. /// \struct AlignedCharArray
  53. /// \brief Helper for building an aligned character array type.
  54. ///
  55. /// This template is used to explicitly build up a collection of aligned
  56. /// character array types. We have to build these up using a macro and explicit
  57. /// specialization to cope with old versions of MSVC and GCC where only an
  58. /// integer literal can be used to specify an alignment constraint. Once built
  59. /// up here, we can then begin to indirect between these using normal C++
  60. /// template parameters.
  61. // MSVC requires special handling here.
  62. #ifndef _MSC_VER
  63. #if __has_feature(cxx_alignas)
  64. template<std::size_t Alignment, std::size_t Size>
  65. struct AlignedCharArray {
  66. alignas(Alignment) char buffer[Size];
  67. };
  68. #elif defined(__GNUC__) || defined(__IBM_ATTRIBUTES)
  69. /// \brief Create a type with an aligned char buffer.
  70. template<std::size_t Alignment, std::size_t Size>
  71. struct AlignedCharArray;
  72. #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
  73. template<std::size_t Size> \
  74. struct AlignedCharArray<x, Size> { \
  75. __attribute__((aligned(x))) char buffer[Size]; \
  76. };
  77. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(1)
  78. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(2)
  79. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(4)
  80. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(8)
  81. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
  82. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
  83. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
  84. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
  85. #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
  86. #else
  87. # error No supported align as directive.
  88. #endif
  89. #else // _MSC_VER
  90. /// \brief Create a type with an aligned char buffer.
  91. template<std::size_t Alignment, std::size_t Size>
  92. struct AlignedCharArray;
  93. // We provide special variations of this template for the most common
  94. // alignments because __declspec(align(...)) doesn't actually work when it is
  95. // a member of a by-value function argument in MSVC, even if the alignment
  96. // request is something reasonably like 8-byte or 16-byte. Note that we can't
  97. // even include the declspec with the union that forces the alignment because
  98. // MSVC warns on the existence of the declspec despite the union member forcing
  99. // proper alignment.
  100. template<std::size_t Size>
  101. struct AlignedCharArray<1, Size> {
  102. union {
  103. char aligned;
  104. char buffer[Size];
  105. };
  106. };
  107. template<std::size_t Size>
  108. struct AlignedCharArray<2, Size> {
  109. union {
  110. short aligned;
  111. char buffer[Size];
  112. };
  113. };
  114. template<std::size_t Size>
  115. struct AlignedCharArray<4, Size> {
  116. union {
  117. int aligned;
  118. char buffer[Size];
  119. };
  120. };
  121. template<std::size_t Size>
  122. struct AlignedCharArray<8, Size> {
  123. union {
  124. double aligned;
  125. char buffer[Size];
  126. };
  127. };
  128. // The rest of these are provided with a __declspec(align(...)) and we simply
  129. // can't pass them by-value as function arguments on MSVC.
  130. #define LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(x) \
  131. template<std::size_t Size> \
  132. struct AlignedCharArray<x, Size> { \
  133. __declspec(align(x)) char buffer[Size]; \
  134. };
  135. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(16)
  136. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(32)
  137. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(64)
  138. LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT(128)
  139. #undef LLVM_ALIGNEDCHARARRAY_TEMPLATE_ALIGNMENT
  140. #endif // _MSC_VER
  141. namespace detail {
  142. template <typename T1,
  143. typename T2 = char, typename T3 = char, typename T4 = char,
  144. typename T5 = char, typename T6 = char, typename T7 = char>
  145. class AlignerImpl {
  146. T1 t1; T2 t2; T3 t3; T4 t4; T5 t5; T6 t6; T7 t7;
  147. AlignerImpl(); // Never defined or instantiated.
  148. };
  149. template <typename T1,
  150. typename T2 = char, typename T3 = char, typename T4 = char,
  151. typename T5 = char, typename T6 = char, typename T7 = char>
  152. union SizerImpl {
  153. char arr1[sizeof(T1)], arr2[sizeof(T2)], arr3[sizeof(T3)], arr4[sizeof(T4)],
  154. arr5[sizeof(T5)], arr6[sizeof(T6)], arr7[sizeof(T7)];
  155. };
  156. } // end namespace detail
  157. /// \brief This union template exposes a suitably aligned and sized character
  158. /// array member which can hold elements of any of up to four types.
  159. ///
  160. /// These types may be arrays, structs, or any other types. The goal is to
  161. /// expose a char array buffer member which can be used as suitable storage for
  162. /// a placement new of any of these types. Support for more than seven types can
  163. /// be added at the cost of more boiler plate.
  164. template <typename T1,
  165. typename T2 = char, typename T3 = char, typename T4 = char,
  166. typename T5 = char, typename T6 = char, typename T7 = char>
  167. struct AlignedCharArrayUnion : llvm::AlignedCharArray<
  168. AlignOf<detail::AlignerImpl<T1, T2, T3, T4, T5, T6, T7> >::Alignment,
  169. sizeof(detail::SizerImpl<T1, T2, T3, T4, T5, T6, T7>)> {
  170. };
  171. } // end namespace llvm
  172. #endif