pointer_plus_bits.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2007-2013
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. // (See accompanying file LICENSE_1_0.txt or copy at
  7. // http://www.boost.org/LICENSE_1_0.txt)
  8. //
  9. // See http://www.boost.org/libs/intrusive for documentation.
  10. //
  11. /////////////////////////////////////////////////////////////////////////////
  12. #ifndef BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP
  13. #define BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP
  14. #include <boost/intrusive/detail/config_begin.hpp>
  15. #include <boost/intrusive/intrusive_fwd.hpp>
  16. #include <boost/intrusive/detail/mpl.hpp> //ls_zeros
  17. #include <boost/intrusive/detail/assert.hpp> //BOOST_INTRUSIVE_INVARIANT_ASSERT
  18. #if defined(BOOST_HAS_PRAGMA_ONCE)
  19. # pragma once
  20. #endif
  21. //GCC reports uninitialized values when an uninitialized pointer plus bits type
  22. //is asigned some bits or some pointer value, but that's ok, because we don't want
  23. //to default initialize parts that are not being updated.
  24. #if defined(BOOST_GCC)
  25. # if (BOOST_GCC >= 40600)
  26. # pragma GCC diagnostic push
  27. # pragma GCC diagnostic ignored "-Wuninitialized"
  28. # if (BOOST_GCC >= 40700)
  29. # pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
  30. # endif
  31. # endif
  32. #endif
  33. namespace boost {
  34. namespace intrusive {
  35. //!This trait class is used to know if a pointer
  36. //!can embed extra bits of information if
  37. //!it's going to be used to point to objects
  38. //!with an alignment of "Alignment" bytes.
  39. template<class VoidPointer, std::size_t Alignment>
  40. struct max_pointer_plus_bits
  41. {
  42. static const std::size_t value = 0;
  43. };
  44. //!This is a specialization for raw pointers.
  45. //!Raw pointers can embed extra bits in the lower bits
  46. //!if the alignment is multiple of 2pow(NumBits).
  47. template<std::size_t Alignment>
  48. struct max_pointer_plus_bits<void*, Alignment>
  49. {
  50. static const std::size_t value = detail::ls_zeros<Alignment>::value;
  51. };
  52. //!This is class that is supposed to have static methods
  53. //!to embed extra bits of information in a pointer.
  54. //!This is a declaration and there is no default implementation,
  55. //!because operations to embed the bits change with every pointer type.
  56. //!
  57. //!An implementation that detects that a pointer type whose
  58. //!has_pointer_plus_bits<>::value is non-zero can make use of these
  59. //!operations to embed the bits in the pointer.
  60. template<class Pointer, std::size_t NumBits>
  61. struct pointer_plus_bits
  62. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  63. {}
  64. #endif
  65. ;
  66. //!This is the specialization to embed extra bits of information
  67. //!in a raw pointer. The extra bits are stored in the lower bits of the pointer.
  68. template<class T, std::size_t NumBits>
  69. struct pointer_plus_bits<T*, NumBits>
  70. {
  71. static const uintptr_t Mask = uintptr_t((uintptr_t(1u) << NumBits) - 1);
  72. typedef T* pointer;
  73. BOOST_INTRUSIVE_FORCEINLINE static pointer get_pointer(pointer n)
  74. { return pointer(uintptr_t(n) & uintptr_t(~Mask)); }
  75. BOOST_INTRUSIVE_FORCEINLINE static void set_pointer(pointer &n, pointer p)
  76. {
  77. BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (uintptr_t(p) & Mask));
  78. n = pointer(uintptr_t(p) | (uintptr_t(n) & Mask));
  79. }
  80. BOOST_INTRUSIVE_FORCEINLINE static std::size_t get_bits(pointer n)
  81. { return std::size_t(uintptr_t(n) & Mask); }
  82. BOOST_INTRUSIVE_FORCEINLINE static void set_bits(pointer &n, std::size_t c)
  83. {
  84. BOOST_INTRUSIVE_INVARIANT_ASSERT(uintptr_t(c) <= Mask);
  85. n = pointer(uintptr_t((get_pointer)(n)) | uintptr_t(c));
  86. }
  87. };
  88. } //namespace intrusive
  89. } //namespace boost
  90. #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
  91. # pragma GCC diagnostic pop
  92. #endif
  93. #include <boost/intrusive/detail/config_end.hpp>
  94. #endif //BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP