Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:35

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP
0012 #define BOOST_INTERPROCESS_OFFSET_PTR_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 
0025 #include <boost/type_traits/is_convertible.hpp>
0026 #include <boost/type_traits/is_constructible.hpp>
0027 #include <boost/type_traits/is_integral.hpp>
0028 #include <boost/type_traits/is_unsigned.hpp>
0029 
0030 #include <boost/interprocess/interprocess_fwd.hpp>
0031 #include <boost/interprocess/detail/utilities.hpp>
0032 #include <boost/interprocess/detail/cast_tags.hpp>
0033 #include <boost/interprocess/detail/mpl.hpp>
0034 #include <boost/container/detail/type_traits.hpp>  //alignment_of, aligned_storage
0035 #include <boost/assert.hpp>
0036 #include <boost/static_assert.hpp>
0037 #include <iosfwd>
0038 
0039 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
0040 #pragma GCC diagnostic push
0041 #pragma GCC diagnostic ignored "-Wmaybe-uninitialized"
0042 #endif
0043 
0044 
0045 //!\file
0046 //!Describes a smart pointer that stores the offset between this pointer and
0047 //!target pointee, called offset_ptr.
0048 
0049 namespace boost {
0050 
0051 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0052 
0053 //Predeclarations
0054 template <class T>
0055 struct has_trivial_destructor;
0056 
0057 #endif   //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0058 
0059 namespace interprocess {
0060 
0061 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0062 namespace ipcdetail {
0063 
0064    template<class OffsetType, std::size_t OffsetAlignment>
0065    union offset_ptr_internal
0066    {
0067       BOOST_STATIC_ASSERT(sizeof(OffsetType) >= sizeof(uintptr_t));
0068       BOOST_STATIC_ASSERT(boost::is_integral<OffsetType>::value && boost::is_unsigned<OffsetType>::value);
0069 
0070       explicit offset_ptr_internal(OffsetType off)
0071          : m_offset(off)
0072       {}
0073 
0074       OffsetType m_offset; //Distance between this object and pointee address
0075 
0076       typename ::boost::container::dtl::aligned_storage
0077          < sizeof(OffsetType)//for offset_type_alignment m_offset will be enough
0078          , (OffsetAlignment == offset_type_alignment) ? 1u : OffsetAlignment
0079          >::type alignment_helper;
0080    };
0081 
0082    //Note: using the address of a local variable to point to another address
0083    //is not standard conforming and this can be optimized-away by the compiler.
0084    //Non-inlining is a method to remain illegal but correct
0085 
0086    //Undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_XXX if your compiler can inline
0087    //this code without breaking the library
0088 
0089    ////////////////////////////////////////////////////////////////////////
0090    //
0091    //                      offset_ptr_to_raw_pointer
0092    //
0093    ////////////////////////////////////////////////////////////////////////
0094    #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
0095    template <class OffsetType>
0096    BOOST_INTERPROCESS_FORCEINLINE void * offset_ptr_to_raw_pointer(const volatile void *this_ptr, OffsetType offset)
0097    {
0098       typedef pointer_offset_caster<void*, OffsetType> caster_t;
0099       #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
0100          if(offset == 1){
0101             return 0;
0102          }
0103          else{
0104             return caster_t(caster_t(this_ptr).offset() + offset).pointer();
0105          }
0106       #else
0107          OffsetType mask = offset == 1;
0108          --mask;
0109          OffsetType target_offset = caster_t(this_ptr).offset() + offset;
0110          target_offset &= mask;
0111          return caster_t(target_offset).pointer();
0112       #endif
0113    }
0114 
0115    ////////////////////////////////////////////////////////////////////////
0116    //
0117    //                      offset_ptr_to_offset
0118    //
0119    ////////////////////////////////////////////////////////////////////////
0120    #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
0121    template<class OffsetType>
0122    BOOST_INTERPROCESS_FORCEINLINE OffsetType offset_ptr_to_offset(const volatile void *ptr, const volatile void *this_ptr)
0123    {
0124       typedef pointer_offset_caster<void*, OffsetType> caster_t;
0125       #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
0126          //offset == 1 && ptr != 0 is not legal for this pointer
0127          if(!ptr){
0128             return 1;
0129          }
0130          else{
0131             OffsetType offset = caster_t(ptr).offset()- caster_t(this_ptr).offset();
0132             BOOST_ASSERT(offset != 1);
0133             return offset;
0134          }
0135       #else
0136          //const OffsetType other = -OffsetType(ptr != 0);
0137          //const OffsetType offset = (caster_t(ptr).offset() - caster_t(this_ptr).offset()) & other;
0138          //return offset + OffsetType(!other);
0139          //
0140          OffsetType offset = caster_t(ptr).offset() - caster_t(this_ptr).offset();
0141          --offset;
0142          OffsetType mask = ptr == 0;
0143          --mask;
0144          offset &= mask;
0145          return ++offset;
0146       #endif
0147    }
0148 
0149    ////////////////////////////////////////////////////////////////////////
0150    //
0151    //                      offset_ptr_to_offset_from_other
0152    //
0153    ////////////////////////////////////////////////////////////////////////
0154    #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
0155    template<class OffsetType>
0156    BOOST_INTERPROCESS_FORCEINLINE OffsetType offset_ptr_to_offset_from_other
0157       (const volatile void *this_ptr, const volatile void *other_ptr, OffsetType other_offset)
0158    {
0159       typedef pointer_offset_caster<void*, OffsetType> caster_t;
0160       #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
0161       if(other_offset == 1){
0162          return 1;
0163       }
0164       else{
0165          OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset() + other_offset;
0166          BOOST_ASSERT(offset != 1);
0167          return offset;
0168       }
0169       #else
0170       OffsetType mask = other_offset == 1;
0171       --mask;
0172       OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset();
0173       offset &= mask;
0174       return offset + other_offset;
0175 
0176       //OffsetType mask = -OffsetType(other_offset != 1);
0177       //OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset();
0178       //offset &= mask;
0179       //return offset + other_offset;
0180       #endif
0181    }
0182 
0183    ////////////////////////////////////////////////////////////////////////
0184    //
0185    // Let's assume cast to void and cv cast don't change any target address
0186    //
0187    ////////////////////////////////////////////////////////////////////////
0188    template<class From, class To>
0189    struct offset_ptr_maintains_address
0190    {
0191       static const bool value =    ipcdetail::is_cv_same<From, To>::value
0192                                 || ipcdetail::is_cv_same<void, To>::value
0193                                 || ipcdetail::is_cv_same<char, To>::value
0194                                 ;
0195    };
0196 
0197    template<class From, class To, class Ret = void>
0198    struct enable_if_convertible_equal_address
0199       : enable_if_c< ::boost::is_convertible<From*, To*>::value
0200                      && offset_ptr_maintains_address<From, To>::value
0201                   , Ret>
0202    {};
0203 
0204    template<class From, class To, class Ret = void>
0205    struct enable_if_convertible_unequal_address
0206       : enable_if_c< ::boost::is_convertible<From*, To*>::value
0207                      && !offset_ptr_maintains_address<From, To>::value
0208                    , Ret>
0209    {};
0210 
0211 }  //namespace ipcdetail {
0212 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0213 
0214 //!A smart pointer that stores the offset between between the pointer and the
0215 //!the object it points. This allows offset allows special properties, since
0216 //!the pointer is independent from the address of the pointee, if the
0217 //!pointer and the pointee are still separated by the same offset. This feature
0218 //!converts offset_ptr in a smart pointer that can be placed in shared memory and
0219 //!memory mapped files mapped in different addresses in every process.
0220 //!
0221 //! \tparam PointedType The type of the pointee.
0222 //! \tparam DifferenceType A signed integer type that can represent the arithmetic operations on the pointer
0223 //! \tparam OffsetType An unsigned integer type that can represent the
0224 //!   distance between two pointers reinterpret_cast-ed as unsigned integers. This type
0225 //!   should be at least of the same size of std::uintptr_t. In some systems it's possible to communicate
0226 //!   between 32 and 64 bit processes using 64 bit offsets.
0227 //! \tparam OffsetAlignment Alignment of the OffsetType stored inside. In some systems might be necessary
0228 //!   to align it to 64 bits in order to communicate 32 and 64 bit processes using 64 bit offsets.
0229 //!
0230 //!<b>Note</b>: offset_ptr uses implementation defined properties, present in most platforms, for
0231 //!performance reasons:
0232 //!   - Assumes that OffsetType representation of nullptr is (OffsetType)zero.
0233 //!   - Assumes that incrementing a OffsetType obtained from a pointer is equivalent
0234 //!     to incrementing the pointer and then converting it back to OffsetType.
0235 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
0236 class offset_ptr
0237 {
0238    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0239    typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment>   self_t;
0240    void unspecified_bool_type_func() const {}
0241    typedef void (self_t::*unspecified_bool_type)() const;
0242    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0243 
0244    public:
0245    typedef PointedType                       element_type;
0246    typedef PointedType *                     pointer;
0247    typedef typename ipcdetail::
0248       add_reference<PointedType>::type       reference;
0249    typedef typename ipcdetail::
0250       remove_volatile<typename ipcdetail::
0251          remove_const<PointedType>::type
0252             >::type                          value_type;
0253    typedef DifferenceType                    difference_type;
0254    typedef std::random_access_iterator_tag   iterator_category;
0255    typedef OffsetType                        offset_type;
0256 
0257    public:   //Public Functions
0258 
0259    //!Default constructor (null pointer).
0260    //!Never throws.
0261    BOOST_INTERPROCESS_FORCEINLINE offset_ptr() BOOST_NOEXCEPT
0262       : internal(1)
0263    {}
0264 
0265    //!Constructor from raw pointer (allows "0" pointer conversion).
0266    //!Never throws.
0267    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(pointer ptr) BOOST_NOEXCEPT
0268       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(ptr, this))
0269    {}
0270 
0271    //!Constructor from other pointer.
0272    //!Never throws.
0273    template <class T>
0274    BOOST_INTERPROCESS_FORCEINLINE offset_ptr( T *ptr
0275              , typename ipcdetail::enable_if< ::boost::is_convertible<T*, PointedType*> >::type * = 0) BOOST_NOEXCEPT
0276       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr), this))
0277    {}
0278 
0279    //!Constructor from other offset_ptr
0280    //!Never throws.
0281    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr& ptr) BOOST_NOEXCEPT
0282       : internal(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset))
0283    {}
0284 
0285    //!Constructor from other offset_ptr. Only takes part in overload resolution
0286    //!if T2* is convertible to PointedType*. Never throws.
0287    template<class T2>
0288    BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
0289              #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0290              , typename ipcdetail::enable_if_convertible_equal_address<T2, PointedType>::type* = 0
0291              #endif
0292              ) BOOST_NOEXCEPT
0293       : internal(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset()))
0294    {}
0295 
0296    #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0297 
0298    template<class T2>
0299    BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
0300              , typename ipcdetail::enable_if_convertible_unequal_address<T2, PointedType>::type* = 0) BOOST_NOEXCEPT
0301       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this))
0302    {}
0303 
0304    #endif
0305 
0306    //!Constructor from other offset_ptr. Only takes part in overload resolution
0307    //!if PointedType* is constructible from T2* other than via a conversion (e.g. cast to a derived class). Never throws.
0308    template<class T2>
0309    BOOST_INTERPROCESS_FORCEINLINE explicit offset_ptr(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
0310              #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0311              , typename ipcdetail::enable_if_c<
0312                 !::boost::is_convertible<T2*, PointedType*>::value && ::boost::is_constructible<T2*, PointedType*>::value
0313              >::type * = 0
0314              #endif
0315              ) BOOST_NOEXCEPT
0316       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this))
0317    {}
0318 
0319    //!Emulates static_cast operator.
0320    //!Never throws.
0321    template<class T2, class P2, class O2, std::size_t A2>
0322    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::static_cast_tag) BOOST_NOEXCEPT
0323       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(r.get()), this))
0324    {}
0325 
0326    //!Emulates const_cast operator.
0327    //!Never throws.
0328    template<class T2, class P2, class O2, std::size_t A2>
0329    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::const_cast_tag) BOOST_NOEXCEPT
0330       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(const_cast<PointedType*>(r.get()), this))
0331    {}
0332 
0333    //!Emulates dynamic_cast operator.
0334    //!Never throws.
0335    template<class T2, class P2, class O2, std::size_t A2>
0336    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::dynamic_cast_tag) BOOST_NOEXCEPT
0337       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(dynamic_cast<PointedType*>(r.get()), this))
0338    {}
0339 
0340    //!Emulates reinterpret_cast operator.
0341    //!Never throws.
0342    template<class T2, class P2, class O2, std::size_t A2>
0343    BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::reinterpret_cast_tag) BOOST_NOEXCEPT
0344       : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(reinterpret_cast<PointedType*>(r.get()), this))
0345    {}
0346 
0347    //!Obtains raw pointer from offset.
0348    //!Never throws.
0349    BOOST_INTERPROCESS_FORCEINLINE pointer get() const BOOST_NOEXCEPT
0350    {  return static_cast<pointer>(ipcdetail::offset_ptr_to_raw_pointer(this, this->internal.m_offset));   }
0351 
0352    BOOST_INTERPROCESS_FORCEINLINE offset_type get_offset() const BOOST_NOEXCEPT
0353    {  return this->internal.m_offset;  }
0354 
0355    //!Pointer-like -> operator. It can return 0 pointer.
0356    //!Never throws.
0357    BOOST_INTERPROCESS_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT
0358    {  return this->get(); }
0359 
0360    //!Dereferencing operator, if it is a null offset_ptr behavior
0361    //!   is undefined. Never throws.
0362    BOOST_INTERPROCESS_FORCEINLINE reference operator* () const BOOST_NOEXCEPT
0363    {
0364       pointer p = this->get();
0365       reference r = *p;
0366       return r;
0367    }
0368 
0369    //!Indexing operator.
0370    //!Never throws.
0371    BOOST_INTERPROCESS_FORCEINLINE reference operator[](difference_type idx) const BOOST_NOEXCEPT
0372    {  return this->get()[idx];  }
0373 
0374    //!Assignment from pointer (saves extra conversion).
0375    //!Never throws.
0376    BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (pointer from) BOOST_NOEXCEPT
0377    {
0378       this->internal.m_offset = ipcdetail::offset_ptr_to_offset<OffsetType>(from, this);
0379       return *this;
0380    }
0381 
0382    //!Assignment from other offset_ptr.
0383    //!Never throws.
0384    BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (const offset_ptr & ptr) BOOST_NOEXCEPT
0385    {
0386       this->internal.m_offset = ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset);
0387       return *this;
0388    }
0389 
0390    //!Assignment from related offset_ptr. If pointers of pointee types
0391    //!   are assignable, offset_ptrs will be assignable. Never throws.
0392    template<class T2> BOOST_INTERPROCESS_FORCEINLINE 
0393    #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0394    typename ipcdetail::enable_if_c
0395       < ::boost::is_convertible<T2*, PointedType*>::value, offset_ptr&>::type
0396    #else
0397    offset_ptr&
0398    #endif
0399       operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr) BOOST_NOEXCEPT
0400    {
0401       this->assign(ptr, ipcdetail::bool_<ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value>());
0402       return *this;
0403    }
0404 
0405    public:
0406 
0407    //!offset_ptr += difference_type.
0408    //!Never throws.
0409    BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator+= (difference_type offset) BOOST_NOEXCEPT
0410    {  this->inc_offset(offset * difference_type(sizeof(PointedType)));   return *this;  }
0411 
0412    //!offset_ptr -= difference_type.
0413    //!Never throws.
0414    BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator-= (difference_type offset) BOOST_NOEXCEPT
0415    {  this->dec_offset(offset * difference_type(sizeof(PointedType)));   return *this;  }
0416 
0417    //!++offset_ptr.
0418    //!Never throws.
0419    BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator++ (void) BOOST_NOEXCEPT
0420    {  this->inc_offset(difference_type(sizeof(PointedType)));   return *this;  }
0421 
0422    //!offset_ptr++.
0423    //!Never throws.
0424    BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator++ (int) BOOST_NOEXCEPT
0425    {
0426       offset_ptr tmp(*this);
0427       this->inc_offset(sizeof (PointedType));
0428       return tmp;
0429    }
0430 
0431    //!--offset_ptr.
0432    //!Never throws.
0433    BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator-- (void) BOOST_NOEXCEPT
0434    {  this->dec_offset(sizeof (PointedType));   return *this;  }
0435 
0436    //!offset_ptr--.
0437    //!Never throws.
0438    BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator-- (int) BOOST_NOEXCEPT
0439    {
0440       offset_ptr tmp(*this);
0441       this->dec_offset(sizeof (PointedType));
0442       return tmp;
0443    }
0444 
0445    //!safe bool conversion operator.
0446    //!Never throws.
0447    #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
0448    BOOST_INTERPROCESS_FORCEINLINE operator unspecified_bool_type() const BOOST_NOEXCEPT
0449    {  return this->internal.m_offset != 1? &self_t::unspecified_bool_type_func : 0;   }
0450    #else
0451    explicit operator bool() const BOOST_NOEXCEPT
0452    {  return this->internal.m_offset != 1;  }
0453    #endif
0454    
0455    //!Not operator. Not needed in theory, but improves portability.
0456    //!Never throws
0457    BOOST_INTERPROCESS_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
0458    {  return this->internal.m_offset == 1;   }
0459 
0460    //!Compatibility with pointer_traits
0461    //!
0462    #if defined(BOOST_NO_CXX11_TEMPLATE_ALIASES)
0463    template <class U>
0464    struct rebind
0465    {  typedef offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> other;  };
0466    #else
0467    template <class U>
0468    using rebind = offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment>;
0469    #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0470    typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> other;
0471    #endif //BOOST_INTERPROCESS_DOXYGEN_INVOKED
0472    #endif
0473 
0474    //!Compatibility with pointer_traits
0475    //!
0476    BOOST_INTERPROCESS_FORCEINLINE static offset_ptr pointer_to(reference r) BOOST_NOEXCEPT
0477    { return offset_ptr(&r); }
0478 
0479    //!difference_type + offset_ptr
0480    //!operation
0481    BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
0482    {  right += diff;  return right;  }
0483 
0484    //!offset_ptr + difference_type
0485    //!operation
0486    BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
0487    {  left += diff;  return left; }
0488 
0489    //!offset_ptr - diff
0490    //!operation
0491    BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
0492    {  left -= diff;  return left; }
0493 
0494    //!offset_ptr - diff
0495    //!operation
0496    BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
0497    {  right -= diff; return right; }
0498 
0499    //!offset_ptr - offset_ptr
0500    //!operation
0501    BOOST_INTERPROCESS_FORCEINLINE friend difference_type operator-(const offset_ptr &pt, const offset_ptr &pt2) BOOST_NOEXCEPT
0502    {  return difference_type(pt.get()- pt2.get());   }
0503 
0504    //Comparison
0505    BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0506    {  return pt1.get() == pt2.get();  }
0507 
0508    BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0509    {  return pt1.get() != pt2.get();  }
0510 
0511    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0512    {  return pt1.get() < pt2.get();  }
0513 
0514    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0515    {  return pt1.get() <= pt2.get();  }
0516 
0517    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0518    {  return pt1.get() > pt2.get();  }
0519 
0520    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0521    {  return pt1.get() >= pt2.get();  }
0522 
0523    //Comparison to raw ptr to support literal 0
0524    BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0525    {  return pt1 == pt2.get();  }
0526 
0527    BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0528    {  return pt1 != pt2.get();  }
0529 
0530    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0531    {  return pt1 < pt2.get();  }
0532 
0533    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0534    {  return pt1 <= pt2.get();  }
0535 
0536    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0537    {  return pt1 > pt2.get();  }
0538 
0539    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
0540    {  return pt1 >= pt2.get();  }
0541 
0542    //Comparison
0543    BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0544    {  return pt1.get() == pt2;  }
0545 
0546    BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0547    {  return pt1.get() != pt2;  }
0548 
0549    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0550    {  return pt1.get() < pt2;  }
0551 
0552    BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0553    {  return pt1.get() <= pt2;  }
0554 
0555    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0556    {  return pt1.get() > pt2;  }
0557 
0558    BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
0559    {  return pt1.get() >= pt2;  }
0560 
0561    BOOST_INTERPROCESS_FORCEINLINE friend void swap(offset_ptr &left, offset_ptr &right) BOOST_NOEXCEPT
0562    {
0563       pointer ptr = right.get();
0564       right = left;
0565       left = ptr;
0566    }
0567 
0568    private:
0569    template<class T2>
0570    BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<true>) BOOST_NOEXCEPT
0571    {  //no need to pointer adjustment
0572       this->internal.m_offset = ipcdetail::offset_ptr_to_offset_from_other<OffsetType>(this, &ptr, ptr.get_offset());
0573    }
0574 
0575    template<class T2>
0576    BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<false>) BOOST_NOEXCEPT
0577    {  //we must convert to raw before calculating the offset
0578       this->internal.m_offset = ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this);
0579    }
0580 
0581    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0582    BOOST_INTERPROCESS_FORCEINLINE void inc_offset(DifferenceType bytes) BOOST_NOEXCEPT
0583    {  internal.m_offset += OffsetType(bytes);   }
0584 
0585    BOOST_INTERPROCESS_FORCEINLINE void dec_offset(DifferenceType bytes) BOOST_NOEXCEPT
0586    {  internal.m_offset -= OffsetType(bytes);   }
0587 
0588    ipcdetail::offset_ptr_internal<OffsetType, OffsetAlignment> internal;
0589 
0590    public:
0591    BOOST_INTERPROCESS_FORCEINLINE const OffsetType &priv_offset() const BOOST_NOEXCEPT
0592    {  return internal.m_offset;   }
0593 
0594    BOOST_INTERPROCESS_FORCEINLINE       OffsetType &priv_offset() BOOST_NOEXCEPT
0595    {  return internal.m_offset;   }
0596 
0597    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0598 };
0599 
0600 //!operator<<
0601 //!for offset ptr
0602 template<class E, class T, class W, class X, class Y, std::size_t Z>
0603 inline std::basic_ostream<E, T> & operator<<
0604    (std::basic_ostream<E, T> & os, offset_ptr<W, X, Y, Z> const & p)
0605 {  return os << p.get_offset();   }
0606 
0607 //!operator>>
0608 //!for offset ptr
0609 template<class E, class T, class W, class X, class Y, std::size_t Z>
0610 inline std::basic_istream<E, T> & operator>>
0611    (std::basic_istream<E, T> & is, offset_ptr<W, X, Y, Z> & p)
0612 {  return is >> p.get_offset();  }
0613 
0614 //!Simulation of static_cast between pointers. Never throws.
0615 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
0616 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
0617    static_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
0618 {
0619    return boost::interprocess::offset_ptr<T1, P1, O1, A1>
0620             (r, boost::interprocess::ipcdetail::static_cast_tag());
0621 }
0622 
0623 //!Simulation of const_cast between pointers. Never throws.
0624 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
0625 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
0626    const_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
0627 {
0628    return boost::interprocess::offset_ptr<T1, P1, O1, A1>
0629             (r, boost::interprocess::ipcdetail::const_cast_tag());
0630 }
0631 
0632 //!Simulation of dynamic_cast between pointers. Never throws.
0633 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
0634 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
0635    dynamic_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
0636 {
0637    return boost::interprocess::offset_ptr<T1, P1, O1, A1>
0638             (r, boost::interprocess::ipcdetail::dynamic_cast_tag());
0639 }
0640 
0641 //!Simulation of reinterpret_cast between pointers. Never throws.
0642 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
0643 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
0644    reinterpret_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
0645 {
0646    return boost::interprocess::offset_ptr<T1, P1, O1, A1>
0647             (r, boost::interprocess::ipcdetail::reinterpret_cast_tag());
0648 }
0649 
0650 }  //namespace interprocess {
0651 
0652 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0653 
0654 ///has_trivial_destructor<> == true_type specialization for optimizations
0655 template <class T, class P, class O, std::size_t A>
0656 struct has_trivial_destructor< ::boost::interprocess::offset_ptr<T, P, O, A> >
0657 {
0658    static const bool value = true;
0659 };
0660 
0661 namespace move_detail {
0662 
0663 ///has_trivial_destructor<> == true_type specialization for optimizations
0664 template <class T, class P, class O, std::size_t A>
0665 struct is_trivially_destructible< ::boost::interprocess::offset_ptr<T, P, O, A> >
0666 {
0667    static const bool value = true;
0668 };
0669 
0670 }  //namespace move_detail {
0671 
0672 namespace interprocess {
0673 
0674 //!to_raw_pointer() enables boost::mem_fn to recognize offset_ptr.
0675 //!Never throws.
0676 template <class T, class P, class O, std::size_t A>
0677 BOOST_INTERPROCESS_FORCEINLINE T * to_raw_pointer(boost::interprocess::offset_ptr<T, P, O, A> const & p) BOOST_NOEXCEPT
0678 {  return ipcdetail::to_raw_pointer(p);   }
0679 
0680 }  //namespace interprocess
0681 
0682 
0683 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0684 }  //namespace boost {
0685 
0686 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0687 
0688 namespace boost{
0689 
0690 //This is to support embedding a bit in the pointer
0691 //for intrusive containers, saving space
0692 namespace intrusive {
0693 
0694 //Predeclaration to avoid including header
0695 template<class VoidPointer, std::size_t N>
0696 struct max_pointer_plus_bits;
0697 
0698 template<std::size_t OffsetAlignment, class P, class O, std::size_t A>
0699 struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void, P, O, A>, OffsetAlignment>
0700 {
0701    //The offset ptr can embed one bit less than the alignment since it
0702    //uses offset == 1 to store the null pointer.
0703    static const std::size_t value = ::boost::interprocess::ipcdetail::ls_zeros<OffsetAlignment>::value - 1;
0704 };
0705 
0706 //Predeclaration
0707 template<class Pointer, std::size_t NumBits>
0708 struct pointer_plus_bits;
0709 
0710 template<class T, class P, class O, std::size_t A, std::size_t NumBits>
0711 struct pointer_plus_bits<boost::interprocess::offset_ptr<T, P, O, A>, NumBits>
0712 {
0713    typedef boost::interprocess::offset_ptr<T, P, O, A>      pointer;
0714    //Bits are stored in the lower bits of the pointer except the LSB,
0715    //because this bit is used to represent the null pointer.
0716    static const O Mask = ((static_cast<O>(1) << NumBits) - static_cast<O>(1)) << 1;
0717    BOOST_STATIC_ASSERT(0 ==(Mask&1));
0718 
0719    //We must ALWAYS take argument "n" by reference as a copy of a null pointer
0720    //with a bit (e.g. offset == 3) would be incorrectly copied and interpreted as non-null.
0721 
0722    BOOST_INTERPROCESS_FORCEINLINE static pointer get_pointer(const pointer &n) BOOST_NOEXCEPT
0723    {
0724       pointer p;
0725       O const tmp_off = n.priv_offset() & ~Mask;
0726       p.priv_offset() = boost::interprocess::ipcdetail::offset_ptr_to_offset_from_other(&p, &n, tmp_off);
0727       return p;
0728    }
0729 
0730    BOOST_INTERPROCESS_FORCEINLINE static void set_pointer(pointer &n, const pointer &p) BOOST_NOEXCEPT
0731    {
0732       BOOST_ASSERT(0 == (get_bits)(p));
0733       O const stored_bits = n.priv_offset() & Mask;
0734       n = p;
0735       n.priv_offset() |= stored_bits;
0736    }
0737 
0738    BOOST_INTERPROCESS_FORCEINLINE static std::size_t get_bits(const pointer &n) BOOST_NOEXCEPT
0739    {
0740       return std::size_t((n.priv_offset() & Mask) >> 1u);
0741    }
0742 
0743    BOOST_INTERPROCESS_FORCEINLINE static void set_bits(pointer &n, std::size_t const b) BOOST_NOEXCEPT
0744    {
0745       BOOST_ASSERT(b < (std::size_t(1) << NumBits));
0746       O tmp = n.priv_offset();
0747       tmp &= ~Mask;
0748       tmp |= O(b << 1u);
0749       n.priv_offset() = tmp;
0750    }
0751 };
0752 
0753 }  //namespace intrusive
0754 
0755 //Predeclaration
0756 template<class T, class U>
0757 struct pointer_to_other;
0758 
0759 //Backwards compatibility with pointer_to_other
0760 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment, class U>
0761 struct pointer_to_other
0762    < ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment>, U >
0763 {
0764    typedef ::boost::interprocess::offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> type;
0765 };
0766 
0767 }  //namespace boost{
0768 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0769 
0770 #include <boost/interprocess/detail/config_end.hpp>
0771 
0772 #if defined(BOOST_GCC) && (BOOST_GCC >= 40600)
0773 #pragma GCC diagnostic pop
0774 #endif
0775 
0776 #endif //#ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP