Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2007-2013 Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. Chris@16: // (See accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/intrusive for documentation. Chris@16: // Chris@16: ///////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP Chris@16: #define BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP Chris@16: Chris@101: #include Chris@101: #include Chris@16: #include //ls_zeros Chris@16: #include //BOOST_INTRUSIVE_INVARIANT_ASSERT Chris@16: Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@101: # pragma once Chris@101: #endif Chris@101: Chris@16: namespace boost { Chris@16: namespace intrusive { Chris@16: Chris@16: //!This trait class is used to know if a pointer Chris@16: //!can embed extra bits of information if Chris@16: //!it's going to be used to point to objects Chris@16: //!with an alignment of "Alignment" bytes. Chris@16: template Chris@16: struct max_pointer_plus_bits Chris@16: { Chris@16: static const std::size_t value = 0; Chris@16: }; Chris@16: Chris@16: //!This is a specialization for raw pointers. Chris@16: //!Raw pointers can embed extra bits in the lower bits Chris@16: //!if the alignment is multiple of 2pow(NumBits). Chris@16: template Chris@16: struct max_pointer_plus_bits Chris@16: { Chris@16: static const std::size_t value = detail::ls_zeros::value; Chris@16: }; Chris@16: Chris@16: //!This is class that is supposed to have static methods Chris@16: //!to embed extra bits of information in a pointer. Chris@16: //!This is a declaration and there is no default implementation, Chris@16: //!because operations to embed the bits change with every pointer type. Chris@16: //! Chris@16: //!An implementation that detects that a pointer type whose Chris@16: //!has_pointer_plus_bits<>::value is non-zero can make use of these Chris@16: //!operations to embed the bits in the pointer. Chris@16: template Chris@16: struct pointer_plus_bits Chris@16: #ifdef BOOST_INTRUSIVE_DOXYGEN_INVOKED Chris@16: {} Chris@16: #endif Chris@16: ; Chris@16: Chris@16: //!This is the specialization to embed extra bits of information Chris@16: //!in a raw pointer. The extra bits are stored in the lower bits of the pointer. Chris@16: template Chris@16: struct pointer_plus_bits Chris@16: { Chris@16: static const std::size_t Mask = ((std::size_t(1u) << NumBits) - 1); Chris@16: typedef T* pointer; Chris@16: Chris@16: static pointer get_pointer(pointer n) Chris@16: { return pointer(std::size_t(n) & ~Mask); } Chris@16: Chris@16: static void set_pointer(pointer &n, pointer p) Chris@16: { Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (std::size_t(p) & Mask)); Chris@16: n = pointer(std::size_t(p) | (std::size_t(n) & Mask)); Chris@16: } Chris@16: Chris@16: static std::size_t get_bits(pointer n) Chris@16: { return (std::size_t(n) & Mask); } Chris@16: Chris@16: static void set_bits(pointer &n, std::size_t c) Chris@16: { Chris@16: BOOST_INTRUSIVE_INVARIANT_ASSERT(c <= Mask); Chris@16: n = pointer(std::size_t(get_pointer(n)) | c); Chris@16: } Chris@16: }; Chris@16: Chris@16: } //namespace intrusive Chris@16: } //namespace boost Chris@16: Chris@101: #include Chris@101: Chris@16: #endif //BOOST_INTRUSIVE_POINTER_PLUS_BITS_HPP