storage_traits.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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/aligned_variable.hpp>
  22. #include <boost/atomic/detail/type_traits/alignment_of.hpp>
  23. #include <boost/atomic/detail/header.hpp>
  24. #ifdef BOOST_HAS_PRAGMA_ONCE
  25. #pragma once
  26. #endif
  27. namespace boost {
  28. namespace atomics {
  29. namespace detail {
  30. template< typename T >
  31. BOOST_FORCEINLINE void non_atomic_load(T const volatile& from, T& to) BOOST_NOEXCEPT
  32. {
  33. to = from;
  34. }
  35. template< std::size_t Size, std::size_t Alignment = 1u >
  36. struct BOOST_ATOMIC_DETAIL_MAY_ALIAS buffer_storage
  37. {
  38. typedef unsigned char data_type[Size];
  39. BOOST_ATOMIC_DETAIL_ALIGNED_VAR_TPL(Alignment, data_type, data);
  40. BOOST_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
  41. {
  42. return (data[0] == 0u && BOOST_ATOMIC_DETAIL_MEMCMP(data, data + 1, Size - 1u) == 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. BOOST_FORCEINLINE bool operator!= (buffer_storage const& that) const BOOST_NOEXCEPT
  49. {
  50. return BOOST_ATOMIC_DETAIL_MEMCMP(data, that.data, Size) != 0;
  51. }
  52. };
  53. template< std::size_t Size, std::size_t Alignment >
  54. BOOST_FORCEINLINE void non_atomic_load(buffer_storage< Size, Alignment > const volatile& from, buffer_storage< Size, Alignment >& to) BOOST_NOEXCEPT
  55. {
  56. BOOST_ATOMIC_DETAIL_MEMCPY(to.data, const_cast< unsigned char const* >(from.data), Size);
  57. }
  58. template< std::size_t Size >
  59. struct storage_traits
  60. {
  61. typedef buffer_storage< Size, 1u > type;
  62. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = 1u;
  63. // By default, prefer the maximum supported alignment
  64. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  65. };
  66. template< >
  67. struct storage_traits< 1u >
  68. {
  69. typedef boost::uint8_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  70. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = 1u;
  71. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 1u;
  72. };
  73. template< >
  74. struct storage_traits< 2u >
  75. {
  76. typedef boost::uint16_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  77. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint16_t >::value;
  78. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 2u;
  79. };
  80. template< >
  81. struct storage_traits< 4u >
  82. {
  83. typedef boost::uint32_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  84. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint32_t >::value;
  85. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 4u;
  86. };
  87. template< >
  88. struct storage_traits< 8u >
  89. {
  90. typedef boost::uint64_t BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  91. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint64_t >::value;
  92. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 8u;
  93. };
  94. #if defined(BOOST_HAS_INT128)
  95. template< >
  96. struct storage_traits< 16u >
  97. {
  98. typedef boost::uint128_type BOOST_ATOMIC_DETAIL_MAY_ALIAS type;
  99. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< boost::uint128_type >::value;
  100. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  101. };
  102. #else
  103. #if (__cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)) &&\
  104. (!defined(BOOST_GCC_VERSION) || BOOST_GCC_VERSION >= 40900)
  105. using std::max_align_t;
  106. #else
  107. #if defined(BOOST_MSVC)
  108. #pragma warning(push)
  109. // alignment is sensitive to packing
  110. #pragma warning(disable: 4121)
  111. #endif
  112. class max_align_helper;
  113. union max_align_t
  114. {
  115. void* ptr;
  116. void (*fun_ptr)();
  117. int max_align_helper::*mem_ptr;
  118. void (max_align_helper::*mem_fun_ptr)();
  119. long long ll;
  120. long double ld;
  121. #if defined(BOOST_HAS_INT128)
  122. boost::int128_type i128;
  123. #endif
  124. #if defined(BOOST_HAS_FLOAT128)
  125. boost::float128_type f128;
  126. #endif
  127. };
  128. #if defined(BOOST_MSVC)
  129. #pragma warning(pop)
  130. #endif
  131. #endif // __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
  132. template< >
  133. struct storage_traits< 16u >
  134. {
  135. typedef buffer_storage< 16u, atomics::detail::alignment_of< atomics::detail::max_align_t >::value > type;
  136. static BOOST_CONSTEXPR_OR_CONST std::size_t native_alignment = atomics::detail::alignment_of< atomics::detail::max_align_t >::value;
  137. static BOOST_CONSTEXPR_OR_CONST std::size_t alignment = 16u;
  138. };
  139. #endif
  140. template< typename T >
  141. struct storage_size_of
  142. {
  143. static BOOST_CONSTEXPR_OR_CONST std::size_t size = sizeof(T);
  144. static BOOST_CONSTEXPR_OR_CONST std::size_t value = (size == 3u ? 4u : (size >= 5u && size <= 7u ? 8u : (size >= 9u && size <= 15u ? 16u : size)));
  145. };
  146. } // namespace detail
  147. } // namespace atomics
  148. } // namespace boost
  149. #include <boost/atomic/detail/footer.hpp>
  150. #endif // BOOST_ATOMIC_DETAIL_STORAGE_TRAITS_HPP_INCLUDED_