1 // tagged pointer, for aba prevention
3 // Copyright (C) 2008, 2009, 2016 Tim Blechmann, based on code by Cory Nelson
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 #ifndef BOOST_LOCKFREE_TAGGED_PTR_PTRCOMPRESSION_HPP_INCLUDED
10 #define BOOST_LOCKFREE_TAGGED_PTR_PTRCOMPRESSION_HPP_INCLUDED
12 #include <cstddef> /* for std::size_t */
15 #include <boost/cstdint.hpp>
16 #include <boost/predef.h>
22 #ifdef BOOST_LOCKFREE_PTR_COMPRESSION
27 typedef boost::uint64_t compressed_ptr_t;
30 typedef boost::uint16_t tag_t;
35 compressed_ptr_t value;
39 static const int tag_index = 3;
40 static const compressed_ptr_t ptr_mask = 0xffffffffffffUL; //(1L<<48L)-1;
42 static T* extract_ptr(volatile compressed_ptr_t const & i)
44 return (T*)(i & ptr_mask);
47 static tag_t extract_tag(volatile compressed_ptr_t const & i)
51 return cu.tag[tag_index];
54 static compressed_ptr_t pack_ptr(T * ptr, tag_t tag)
57 ret.value = compressed_ptr_t(ptr);
58 ret.tag[tag_index] = tag;
63 /** uninitialized constructor */
64 tagged_ptr(void) BOOST_NOEXCEPT//: ptr(0), tag(0)
67 /** copy constructor */
68 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
69 tagged_ptr(tagged_ptr const & p):
73 tagged_ptr(tagged_ptr const & p) = default;
76 explicit tagged_ptr(T * p, tag_t t = 0):
80 /** unsafe set operation */
82 #ifdef BOOST_NO_CXX11_DEFAULTED_FUNCTIONS
83 tagged_ptr & operator= (tagged_ptr const & p)
89 tagged_ptr & operator= (tagged_ptr const & p) = default;
92 void set(T * p, tag_t t)
98 /** comparing semantics */
100 bool operator== (volatile tagged_ptr const & p) const
102 return (ptr == p.ptr);
105 bool operator!= (volatile tagged_ptr const & p) const
107 return !operator==(p);
111 /** pointer access */
115 return extract_ptr(ptr);
120 tag_t tag = get_tag();
121 ptr = pack_ptr(p, tag);
127 tag_t get_tag() const
129 return extract_tag(ptr);
132 tag_t get_next_tag() const
134 tag_t next = (get_tag() + 1u) & (std::numeric_limits<tag_t>::max)();
138 void set_tag(tag_t t)
141 ptr = pack_ptr(p, t);
145 /** smart pointer support */
147 T & operator*() const
152 T * operator->() const
157 operator bool(void) const
159 return get_ptr() != 0;
164 compressed_ptr_t ptr;
167 #error unsupported platform
170 } /* namespace detail */
171 } /* namespace lockfree */
172 } /* namespace boost */
174 #endif /* BOOST_LOCKFREE_TAGGED_PTR_PTRCOMPRESSION_HPP_INCLUDED */