pointer_plus_bits.hpp 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/mpl.hpp> //ls_zeros
  15. #include <boost/intrusive/detail/assert.hpp> //BOOST_INTRUSIVE_INVARIANT_ASSERT
  16. namespace boost {
  17. namespace intrusive {
  18. //!This trait class is used to know if a pointer
  19. //!can embed extra bits of information if
  20. //!it's going to be used to point to objects
  21. //!with an alignment of "Alignment" bytes.
  22. template<class VoidPointer, std::size_t Alignment>
  23. struct max_pointer_plus_bits
  24. {
  25. static const std::size_t value = 0;
  26. };
  27. //!This is a specialization for raw pointers.
  28. //!Raw pointers can embed extra bits in the lower bits
  29. //!if the alignment is multiple of 2pow(NumBits).
  30. template<std::size_t Alignment>
  31. struct max_pointer_plus_bits<void*, Alignment>
  32. {
  33. static const std::size_t value = detail::ls_zeros<Alignment>::value;
  34. };
  35. //!This is class that is supposed to have static methods
  36. //!to embed extra bits of information in a pointer.
  37. //!This is a declaration and there is no default implementation,
  38. //!because operations to embed the bits change with every pointer type.
  39. //!
  40. //!An implementation that detects that a pointer type whose
  41. //!has_pointer_plus_bits<>::value is non-zero can make use of these
  42. //!operations to embed the bits in the pointer.
  43. template<class Pointer, std::size_t NumBits>
  44. struct pointer_plus_bits
  45. #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED
  46. {}
  47. #endif
  48. ;
  49. //!This is the specialization to embed extra bits of information
  50. //!in a raw pointer. The extra bits are stored in the lower bits of the pointer.
  51. template<class T, std::size_t NumBits>
  52. struct pointer_plus_bits<T*, NumBits>
  53. {
  54. static const std::size_t Mask = ((std::size_t(1u) << NumBits) - 1);
  55. typedef T* pointer;
  56. static pointer get_pointer(pointer n)
  57. { return pointer(std::size_t(n) & ~Mask); }
  58. static void set_pointer(pointer &n, pointer p)
  59. {
  60. BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (std::size_t(p) & Mask));
  61. n = pointer(std::size_t(p) | (std::size_t(n) & Mask));
  62. }
  63. static std::size_t get_bits(pointer n)
  64. { return (std::size_t(n) & Mask); }
  65. static void set_bits(pointer &n, std::size_t c)
  66. {
  67. BOOST_INTRUSIVE_INVARIANT_ASSERT(c <= Mask);
  68. n = pointer(std::size_t(get_pointer(n)) | c);
  69. }
  70. };
  71. } //namespace intrusive
  72. } //namespace boost
  73. #endif //BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP