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