Use uinptr_t instead of size_t, only the former is guaranteed to be able to hold a pointer

This commit is contained in:
Ion Gaztañaga
2015-10-17 00:13:38 +02:00
parent ff9a968958
commit 2208226d2c
2 changed files with 20 additions and 8 deletions

View File

@@ -16,7 +16,11 @@
#ifndef BOOST_CONFIG_HPP
# include <boost/config.hpp>
#endif
#
#ifndef BOOST_CSTDINT_HPP
# include <boost/cstdint.hpp>
#endif
#
#if defined(BOOST_HAS_PRAGMA_ONCE)
# pragma once
#endif
@@ -65,6 +69,14 @@
namespace boost {
namespace intrusive {
#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
# ifdef BOOST_HAS_INTPTR_T
using ::boost::uintptr_t;
# else
typedef std::size_t uintptr_t;
# endif
#endif
////////////////////////////
// Node algorithms
////////////////////////////

View File

@@ -64,25 +64,25 @@ struct pointer_plus_bits
template<class T, std::size_t NumBits>
struct pointer_plus_bits<T*, NumBits>
{
static const std::size_t Mask = ((std::size_t(1u) << NumBits) - 1);
static const uintptr_t Mask = uintptr_t((uintptr_t(1u) << NumBits) - 1);
typedef T* pointer;
static pointer get_pointer(pointer n)
{ return pointer(std::size_t(n) & ~Mask); }
{ return pointer(uintptr_t(n) & uintptr_t(~Mask)); }
static void set_pointer(pointer &n, pointer p)
{
BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (std::size_t(p) & Mask));
n = pointer(std::size_t(p) | (std::size_t(n) & Mask));
BOOST_INTRUSIVE_INVARIANT_ASSERT(0 == (uintptr_t(p) & Mask));
n = pointer(uintptr_t(p) | (uintptr_t(n) & Mask));
}
static std::size_t get_bits(pointer n)
{ return (std::size_t(n) & Mask); }
{ return std::size_t(uintptr_t(n) & Mask); }
static void set_bits(pointer &n, std::size_t c)
{
BOOST_INTRUSIVE_INVARIANT_ASSERT(c <= Mask);
n = pointer(std::size_t(get_pointer(n)) | c);
BOOST_INTRUSIVE_INVARIANT_ASSERT(uintptr_t(c) <= Mask);
n = pointer(uintptr_t((get_pointer)(n)) | uintptr_t(c));
}
};