storage_traits.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * Copyright (c) 2009 Helge Bahmann
  7. * Copyright (c) 2012 Tim Blechmann
  8. * Copyright (c) 2013 - 2020 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/storage_traits.hpp
  12. *
  13. * This header defines underlying types used as storage
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_STORAGE_TRAITS_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_STORAGE_TRAITS_HPP_INCLUDED_
  17. #include <cstddef>
  18. #include <boost/cstdint.hpp>
  19. #include <boost/atomic/detail/config.hpp>
  20. #include <boost/atomic/detail/string_ops.hpp>
  21. #include <boost/atomic/detail/type_traits/alignment_of.hpp>
  22. #if defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  23. #include <boost/type_traits/type_with_alignment.hpp>
  24. #endif
  25. #ifdef BOOST_HAS_PRAGMA_ONCE
  26. #pragma once
  27. #endif
  28. namespace boost {
  29. namespace atomics {
  30. namespace detail {
  31. template< typename T >
  32. BOOST_FORCEINLINE void non_atomic_load(T const volatile& from, T& to) BOOST_NOEXCEPT
  33. {
  34. to = from;
  35. }
  36. template< std::size_t Size, std::size_t Alignment = 1u >
  37. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS buffer_storage
  38. {
  39. #if !defined(BOOST_ATOMIC_DETAIL_NO_CXX11_ALIGNAS)
  40. alignas(Alignment) unsigned char data[Size];
  41. #else
  42. union
  43. {
  44. unsigned char data[Size];
  45. typename boost::type_with_alignment< Alignment >::type aligner;
  46. };
  47. #endif
  48. BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
  49. {
  50. return (data[0] == 0u && BOOST_ATOMIC_DETAIL_MEMCMP(data, data + 1, Size - 1u) == 0);
  51. }
  52. BOOST_FORCEINLINE bool operator== (buffer_storage const& that) const BOOST_NOEXCEPT
  53. {
  54. return BOOST_ATOMIC_DETAIL_MEMCMP(data, that.data, Size) == 0;
  55. }
  56. BOOST_FORCEINLINE bool operator!= (buffer_storage const& that) const BOOST_NOEXCEPT
  57. {
  58. return BOOST_ATOMIC_DETAIL_MEMCMP(data, that.data, Size) != 0;
  59. }
  60. };
  61. template< std::size_t Size, std::size_t Alignment >
  62. BOOST_FORCEINLINE void non_atomic_load(buffer_storage< Size, Alignment > const volatile& from, buffer_storage< Size, Alignment >& to) BOOST_NOEXCEPT
  63. {
  64. BOOST_ATOMIC_DETAIL_MEMCPY(to.data, const_cast< unsigned char const* >(from.data), Size);
  65. }
  66. template< std::size_t Size >
  67. struct storage_traits
  68. {
  69. typedef buffer_storage< Size, 1u > type;
  70. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = 1u;
  71. // By default, prefer the maximum supported alignment
  72. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  73. };
  74. template< >
  75. struct storage_traits< 1u >
  76. {
  77. typedef boost::uint8_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  78. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = 1u;
  79. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 1u;
  80. };
  81. template< >
  82. struct storage_traits< 2u >
  83. {
  84. typedef boost::uint16_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  85. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint16_t >::value;
  86. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 2u;
  87. };
  88. template< >
  89. struct storage_traits< 4u >
  90. {
  91. typedef boost::uint32_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  92. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint32_t >::value;
  93. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 4u;
  94. };
  95. template< >
  96. struct storage_traits< 8u >
  97. {
  98. typedef boost::uint64_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  99. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint64_t >::value;
  100. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 8u;
  101. };
  102. #if defined(BOOST_HAS_INT128)
  103. template< >
  104. struct storage_traits< 16u >
  105. {
  106. typedef boost::uint128_type BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  107. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint128_type >::value;
  108. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  109. };
  110. #else
  111. #if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) &&\
  112. (!defined(BOOST_GCC_VERSION) || BOOST_GCC_VERSION >= 40900)
  113. using std::max_align_t;
  114. #else
  115. #if defined(BOOST_MSVC)
  116. #pragma warning(push)
  117. // alignment is sensitive to packing
  118. #pragma warning(disable: 4121)
  119. #endif
  120. class max_align_helper;
  121. union max_align_t
  122. {
  123. void* ptr;
  124. void (*fun_ptr)();
  125. int max_align_helper::*mem_ptr;
  126. void (max_align_helper::*mem_fun_ptr)();
  127. long long ll;
  128. long double ld;
  129. #if defined(BOOST_HAS_INT128)
  130. boost::int128_type i128;
  131. #endif
  132. #if defined(BOOST_HAS_FLOAT128)
  133. boost::float128_type f128;
  134. #endif
  135. };
  136. #if defined(BOOST_MSVC)
  137. #pragma warning(pop)
  138. #endif
  139. #endif // __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
  140. template< >
  141. struct storage_traits< 16u >
  142. {
  143. typedef buffer_storage< 16u, atomics::detail::alignment_of< atomics::detail::max_align_t >::value > type;
  144. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< atomics::detail::max_align_t >::value;
  145. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  146. };
  147. #endif
  148. template< typename T >
  149. struct storage_size_of
  150. {
  151. static BOOST_CONSTEXPR_OR_CONST std::size_t size = sizeof(T);
  152. static BOOST_CONSTEXPR_OR_CONST std::size_t value = (size == 3u ? 4u : (size >= 5u && size <= 7u ? 8u : (size >= 9u && size <= 15u ? 16u : size)));
  153. };
  154. } // namespace detail
  155. } // namespace atomics
  156. } // namespace boost
  157. #endif // BOOST_ATOMIC_DETAIL_STORAGE_TRAITS_HPP_INCLUDED_