pointer_plus_bits.hpp 3.1 KB

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