storage_type.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 - 2014 Andrey Semashev
  9. */
  10. /*!
  11. * \file atomic/detail/storage_type.hpp
  12. *
  13. * This header defines underlying types used as storage
  14. */
  15. #ifndef BOOST_ATOMIC_DETAIL_STORAGE_TYPE_HPP_INCLUDED_
  16. #define BOOST_ATOMIC_DETAIL_STORAGE_TYPE_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. #ifdef BOOST_HAS_PRAGMA_ONCE
  22. #pragma once
  23. #endif
  24. namespace boost {
  25. namespace atomics {
  26. namespace detail {
  27. template< typename T >
  28. BOOST_FORCEINLINE void non_atomic_load(T const volatile& from, T& to) BOOST_NOEXCEPT
  29. {
  30. to = from;
  31. }
  32. template< std::size_t Size >
  33. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS buffer_storage
  34. {
  35. BOOST_ALIGNMENT(16) unsigned char data[Size];
  36. BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
  37. {
  38. return (data[0] == 0u && BOOST_ATOMIC_DETAIL_MEMCMP(data, data + 1, Size - 1) == 0);
  39. }
  40. BOOST_FORCEINLINE bool operator== (buffer_storage const& that) const BOOST_NOEXCEPT
  41. {
  42. return BOOST_ATOMIC_DETAIL_MEMCMP(data, that.data, Size) == 0;
  43. }
  44. BOOST_FORCEINLINE bool operator!= (buffer_storage const& that) const BOOST_NOEXCEPT
  45. {
  46. return BOOST_ATOMIC_DETAIL_MEMCMP(data, that.data, Size) != 0;
  47. }
  48. };
  49. template< std::size_t Size >
  50. BOOST_FORCEINLINE void non_atomic_load(buffer_storage< Size > const volatile& from, buffer_storage< Size >& to) BOOST_NOEXCEPT
  51. {
  52. BOOST_ATOMIC_DETAIL_MEMCPY(to.data, const_cast< unsigned char const* >(from.data), Size);
  53. }
  54. template< std::size_t Size >
  55. struct make_storage_type
  56. {
  57. typedef buffer_storage< Size > type;
  58. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  59. {
  60. type value;
  61. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  62. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type const& v) BOOST_NOEXCEPT : value(v) {}
  63. };
  64. };
  65. template< >
  66. struct make_storage_type< 1u >
  67. {
  68. typedef boost::uint8_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  69. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  70. {
  71. type value;
  72. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  73. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type v) BOOST_NOEXCEPT : value(v) {}
  74. };
  75. };
  76. template< >
  77. struct make_storage_type< 2u >
  78. {
  79. typedef boost::uint16_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  80. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  81. {
  82. BOOST_ALIGNMENT(2) type value;
  83. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  84. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type v) BOOST_NOEXCEPT : value(v) {}
  85. };
  86. };
  87. template< >
  88. struct make_storage_type< 4u >
  89. {
  90. typedef boost::uint32_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  91. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  92. {
  93. BOOST_ALIGNMENT(4) type value;
  94. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  95. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type v) BOOST_NOEXCEPT : value(v) {}
  96. };
  97. };
  98. template< >
  99. struct make_storage_type< 8u >
  100. {
  101. typedef boost::uint64_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  102. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  103. {
  104. BOOST_ALIGNMENT(8) type value;
  105. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  106. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type v) BOOST_NOEXCEPT : value(v) {}
  107. };
  108. };
  109. #if defined(BOOST_HAS_INT128)
  110. template< >
  111. struct make_storage_type< 16u >
  112. {
  113. typedef boost::uint128_type BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  114. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  115. {
  116. BOOST_ALIGNMENT(16) type value;
  117. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  118. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type v) BOOST_NOEXCEPT : value(v) {}
  119. };
  120. };
  121. #elif !defined(BOOST_NO_ALIGNMENT)
  122. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS storage128_t
  123. {
  124. typedef boost::uint64_t BOOST_ATOMIC_DETAIL_MAY_ALIAS element_type;
  125. element_type data[2];
  126. BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
  127. {
  128. return (data[0] | data[1]) == 0u;
  129. }
  130. };
  131. BOOST_FORCEINLINE bool operator== (storage128_t const& left, storage128_t const& right) BOOST_NOEXCEPT
  132. {
  133. return ((left.data[0] ^ right.data[0]) | (left.data[1] ^ right.data[1])) == 0u;
  134. }
  135. BOOST_FORCEINLINE bool operator!= (storage128_t const& left, storage128_t const& right) BOOST_NOEXCEPT
  136. {
  137. return !(left == right);
  138. }
  139. BOOST_FORCEINLINE void non_atomic_load(storage128_t const volatile& from, storage128_t& to) BOOST_NOEXCEPT
  140. {
  141. to.data[0] = from.data[0];
  142. to.data[1] = from.data[1];
  143. }
  144. template< >
  145. struct make_storage_type< 16u >
  146. {
  147. typedef storage128_t type;
  148. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS aligned
  149. {
  150. BOOST_ALIGNMENT(16) type value;
  151. BOOST_DEFAULTED_FUNCTION(aligned(), {})
  152. BOOST_FORCEINLINE BOOST_CONSTEXPR explicit aligned(type const& v) BOOST_NOEXCEPT : value(v) {}
  153. };
  154. };
  155. #endif
  156. template< typename T >
  157. struct storage_size_of
  158. {
  159. static BOOST_CONSTEXPR_OR_CONST std::size_t size = sizeof(T);
  160. static BOOST_CONSTEXPR_OR_CONST std::size_t value = (size == 3u ? 4u : (size >= 5u && size <= 7u ? 8u : (size >= 9u && size <= 15u ? 16u : size)));
  161. };
  162. } // namespace detail
  163. } // namespace atomics
  164. } // namespace boost
  165. #endif // BOOST_ATOMIC_DETAIL_STORAGE_TYPE_HPP_INCLUDED_