1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2015. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP
12 #define BOOST_INTERPROCESS_OFFSET_PTR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/type_traits/is_convertible.hpp>
27 #include <boost/interprocess/interprocess_fwd.hpp>
28 #include <boost/interprocess/detail/utilities.hpp>
29 #include <boost/interprocess/detail/cast_tags.hpp>
30 #include <boost/interprocess/detail/mpl.hpp>
31 #include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage
32 #include <boost/assert.hpp>
36 //!Describes a smart pointer that stores the offset between this pointer and
37 //!target pointee, called offset_ptr.
41 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
45 struct has_trivial_destructor;
47 #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
49 namespace interprocess {
51 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
54 template<class OffsetType, std::size_t OffsetAlignment>
55 union offset_ptr_internal
57 BOOST_STATIC_ASSERT(sizeof(OffsetType) >= sizeof(uintptr_t));
59 explicit offset_ptr_internal(OffsetType off)
63 OffsetType m_offset; //Distance between this object and pointee address
65 typename ::boost::container::container_detail::aligned_storage
66 < sizeof(OffsetType)//for offset_type_alignment m_offset will be enough
67 , (OffsetAlignment == offset_type_alignment) ? 1u : OffsetAlignment
68 >::type alignment_helper;
71 //Note: using the address of a local variable to point to another address
72 //is not standard conforming and this can be optimized-away by the compiler.
73 //Non-inlining is a method to remain illegal but correct
75 //Undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_XXX if your compiler can inline
76 //this code without breaking the library
78 ////////////////////////////////////////////////////////////////////////
80 // offset_ptr_to_raw_pointer
82 ////////////////////////////////////////////////////////////////////////
83 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
84 BOOST_INTERPROCESS_FORCEINLINE void * offset_ptr_to_raw_pointer(const volatile void *this_ptr, uintptr_t offset)
86 typedef pointer_uintptr_caster<void*> caster_t;
87 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
92 return caster_t(caster_t(this_ptr).uintptr() + offset).pointer();
95 uintptr_t mask = offset == 1;
97 uintptr_t target_offset = caster_t(this_ptr).uintptr() + offset;
98 target_offset &= mask;
99 return caster_t(target_offset).pointer();
103 ////////////////////////////////////////////////////////////////////////
105 // offset_ptr_to_offset
107 ////////////////////////////////////////////////////////////////////////
108 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
109 BOOST_INTERPROCESS_FORCEINLINE uintptr_t offset_ptr_to_offset(const volatile void *ptr, const volatile void *this_ptr)
111 typedef pointer_uintptr_caster<void*> caster_t;
112 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF
113 //offset == 1 && ptr != 0 is not legal for this pointer
118 uintptr_t offset = caster_t(ptr).uintptr() - caster_t(this_ptr).uintptr();
119 BOOST_ASSERT(offset != 1);
123 //const uintptr_t other = -uintptr_t(ptr != 0);
124 //const uintptr_t offset = (caster_t(ptr).uintptr() - caster_t(this_ptr).uintptr()) & other;
125 //return offset + uintptr_t(!other);
127 uintptr_t offset = caster_t(ptr).uintptr() - caster_t(this_ptr).uintptr();
129 uintptr_t mask = uintptr_t(ptr == 0);
136 ////////////////////////////////////////////////////////////////////////
138 // offset_ptr_to_offset_from_other
140 ////////////////////////////////////////////////////////////////////////
141 #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
142 BOOST_INTERPROCESS_FORCEINLINE uintptr_t offset_ptr_to_offset_from_other
143 (const volatile void *this_ptr, const volatile void *other_ptr, uintptr_t other_offset)
145 typedef pointer_uintptr_caster<void*> caster_t;
146 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
147 if(other_offset == 1){
151 uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr() + other_offset;
152 BOOST_ASSERT(offset != 1);
156 uintptr_t mask = other_offset == 1;
158 uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr();
160 return offset + other_offset;
162 //uintptr_t mask = -uintptr_t(other_offset != 1);
163 //uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr();
165 //return offset + other_offset;
169 ////////////////////////////////////////////////////////////////////////
171 // Let's assume cast to void and cv cast don't change any target address
173 ////////////////////////////////////////////////////////////////////////
174 template<class From, class To>
175 struct offset_ptr_maintains_address
177 static const bool value = ipcdetail::is_cv_same<From, To>::value
178 || ipcdetail::is_cv_same<void, To>::value
179 || ipcdetail::is_cv_same<char, To>::value
183 template<class From, class To, class Ret = void>
184 struct enable_if_convertible_equal_address
185 : enable_if_c< ::boost::is_convertible<From*, To*>::value
186 && offset_ptr_maintains_address<From, To>::value
190 template<class From, class To, class Ret = void>
191 struct enable_if_convertible_unequal_address
192 : enable_if_c< ::boost::is_convertible<From*, To*>::value
193 && !offset_ptr_maintains_address<From, To>::value
197 } //namespace ipcdetail {
198 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
200 //!A smart pointer that stores the offset between between the pointer and the
201 //!the object it points. This allows offset allows special properties, since
202 //!the pointer is independent from the address of the pointee, if the
203 //!pointer and the pointee are still separated by the same offset. This feature
204 //!converts offset_ptr in a smart pointer that can be placed in shared memory and
205 //!memory mapped files mapped in different addresses in every process.
207 //! \tparam PointedType The type of the pointee.
208 //! \tparam DifferenceType A signed integer type that can represent the arithmetic operations on the pointer
209 //! \tparam OffsetType An unsigned integer type that can represent the
210 //! distance between two pointers reinterpret_cast-ed as unsigned integers. This type
211 //! should be at least of the same size of std::uintptr_t. In some systems it's possible to communicate
212 //! between 32 and 64 bit processes using 64 bit offsets.
213 //! \tparam OffsetAlignment Alignment of the OffsetType stored inside. In some systems might be necessary
214 //! to align it to 64 bits in order to communicate 32 and 64 bit processes using 64 bit offsets.
216 //!<b>Note</b>: offset_ptr uses implementation defined properties, present in most platforms, for
217 //!performance reasons:
218 //! - Assumes that uintptr_t representation of nullptr is (uintptr_t)zero.
219 //! - Assumes that incrementing a uintptr_t obtained from a pointer is equivalent
220 //! to incrementing the pointer and then converting it back to uintptr_t.
221 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment>
224 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
225 typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> self_t;
226 void unspecified_bool_type_func() const {}
227 typedef void (self_t::*unspecified_bool_type)() const;
228 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
231 typedef PointedType element_type;
232 typedef PointedType * pointer;
233 typedef typename ipcdetail::
234 add_reference<PointedType>::type reference;
235 typedef typename ipcdetail::
236 remove_volatile<typename ipcdetail::
237 remove_const<PointedType>::type
239 typedef DifferenceType difference_type;
240 typedef std::random_access_iterator_tag iterator_category;
241 typedef OffsetType offset_type;
243 public: //Public Functions
245 //!Default constructor (null pointer).
247 BOOST_INTERPROCESS_FORCEINLINE offset_ptr() BOOST_NOEXCEPT
251 //!Constructor from raw pointer (allows "0" pointer conversion).
253 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(pointer ptr) BOOST_NOEXCEPT
254 : internal(static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset(ptr, this)))
257 //!Constructor from other pointer.
260 BOOST_INTERPROCESS_FORCEINLINE offset_ptr( T *ptr
261 , typename ipcdetail::enable_if< ::boost::is_convertible<T*, PointedType*> >::type * = 0) BOOST_NOEXCEPT
262 : internal(static_cast<OffsetType>
263 (ipcdetail::offset_ptr_to_offset(static_cast<PointedType*>(ptr), this)))
266 //!Constructor from other offset_ptr
268 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr& ptr) BOOST_NOEXCEPT
269 : internal(static_cast<OffsetType>
270 (ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset)))
273 //!Constructor from other offset_ptr. If pointers of pointee types are
274 //!convertible, offset_ptrs will be convertibles. Never throws.
276 BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
277 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
278 , typename ipcdetail::enable_if_convertible_equal_address<T2, PointedType>::type* = 0
281 : internal(static_cast<OffsetType>
282 (ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset())))
285 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
287 //!Constructor from other offset_ptr. If pointers of pointee types are
288 //!convertible, offset_ptrs will be convertibles. Never throws.
290 BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr
291 , typename ipcdetail::enable_if_convertible_unequal_address<T2, PointedType>::type* = 0) BOOST_NOEXCEPT
292 : internal(static_cast<OffsetType>
293 (ipcdetail::offset_ptr_to_offset(static_cast<PointedType*>(ptr.get()), this)))
298 //!Emulates static_cast operator.
300 template<class T2, class P2, class O2, std::size_t A2>
301 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::static_cast_tag) BOOST_NOEXCEPT
302 : internal(static_cast<OffsetType>
303 (ipcdetail::offset_ptr_to_offset(static_cast<PointedType*>(r.get()), this)))
306 //!Emulates const_cast operator.
308 template<class T2, class P2, class O2, std::size_t A2>
309 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::const_cast_tag) BOOST_NOEXCEPT
310 : internal(static_cast<OffsetType>
311 (ipcdetail::offset_ptr_to_offset(const_cast<PointedType*>(r.get()), this)))
314 //!Emulates dynamic_cast operator.
316 template<class T2, class P2, class O2, std::size_t A2>
317 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::dynamic_cast_tag) BOOST_NOEXCEPT
318 : internal(static_cast<OffsetType>
319 (ipcdetail::offset_ptr_to_offset(dynamic_cast<PointedType*>(r.get()), this)))
322 //!Emulates reinterpret_cast operator.
324 template<class T2, class P2, class O2, std::size_t A2>
325 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::reinterpret_cast_tag) BOOST_NOEXCEPT
326 : internal(static_cast<OffsetType>
327 (ipcdetail::offset_ptr_to_offset(reinterpret_cast<PointedType*>(r.get()), this)))
330 //!Obtains raw pointer from offset.
332 BOOST_INTERPROCESS_FORCEINLINE pointer get() const BOOST_NOEXCEPT
333 { return (pointer)ipcdetail::offset_ptr_to_raw_pointer(this, this->internal.m_offset); }
335 BOOST_INTERPROCESS_FORCEINLINE offset_type get_offset() const BOOST_NOEXCEPT
336 { return this->internal.m_offset; }
338 //!Pointer-like -> operator. It can return 0 pointer.
340 BOOST_INTERPROCESS_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT
341 { return this->get(); }
343 //!Dereferencing operator, if it is a null offset_ptr behavior
344 //! is undefined. Never throws.
345 BOOST_INTERPROCESS_FORCEINLINE reference operator* () const BOOST_NOEXCEPT
347 pointer p = this->get();
352 //!Indexing operator.
354 BOOST_INTERPROCESS_FORCEINLINE reference operator[](difference_type idx) const BOOST_NOEXCEPT
355 { return this->get()[idx]; }
357 //!Assignment from pointer (saves extra conversion).
359 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (pointer from) BOOST_NOEXCEPT
361 this->internal.m_offset =
362 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset(from, this));
366 //!Assignment from other offset_ptr.
368 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (const offset_ptr & ptr) BOOST_NOEXCEPT
370 this->internal.m_offset =
371 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset));
375 //!Assignment from related offset_ptr. If pointers of pointee types
376 //! are assignable, offset_ptrs will be assignable. Never throws.
377 template<class T2> BOOST_INTERPROCESS_FORCEINLINE
378 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
379 typename ipcdetail::enable_if_c
380 < ::boost::is_convertible<T2*, PointedType*>::value, offset_ptr&>::type
384 operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr) BOOST_NOEXCEPT
386 this->assign(ptr, ipcdetail::bool_<ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value>());
392 //!offset_ptr += difference_type.
394 BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator+= (difference_type offset) BOOST_NOEXCEPT
395 { this->inc_offset(offset * sizeof (PointedType)); return *this; }
397 //!offset_ptr -= difference_type.
399 BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator-= (difference_type offset) BOOST_NOEXCEPT
400 { this->dec_offset(offset * sizeof (PointedType)); return *this; }
404 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator++ (void) BOOST_NOEXCEPT
405 { this->inc_offset(sizeof (PointedType)); return *this; }
409 BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator++ (int) BOOST_NOEXCEPT
411 offset_ptr tmp(*this);
412 this->inc_offset(sizeof (PointedType));
418 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator-- (void) BOOST_NOEXCEPT
419 { this->dec_offset(sizeof (PointedType)); return *this; }
423 BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator-- (int) BOOST_NOEXCEPT
425 offset_ptr tmp(*this);
426 this->dec_offset(sizeof (PointedType));
430 //!safe bool conversion operator.
432 #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS)
433 BOOST_INTERPROCESS_FORCEINLINE operator unspecified_bool_type() const BOOST_NOEXCEPT
434 { return this->internal.m_offset != 1? &self_t::unspecified_bool_type_func : 0; }
436 explicit operator bool() const BOOST_NOEXCEPT
437 { return this->internal.m_offset != 1; }
440 //!Not operator. Not needed in theory, but improves portability.
442 BOOST_INTERPROCESS_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
443 { return this->internal.m_offset == 1; }
445 //!Compatibility with pointer_traits
449 { typedef offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> other; };
451 //!Compatibility with pointer_traits
453 BOOST_INTERPROCESS_FORCEINLINE static offset_ptr pointer_to(reference r) BOOST_NOEXCEPT
454 { return offset_ptr(&r); }
456 //!difference_type + offset_ptr
458 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
459 { right += diff; return right; }
461 //!offset_ptr + difference_type
463 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
464 { left += diff; return left; }
468 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
469 { left -= diff; return left; }
473 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
474 { right -= diff; return right; }
476 //!offset_ptr - offset_ptr
478 BOOST_INTERPROCESS_FORCEINLINE friend difference_type operator-(const offset_ptr &pt, const offset_ptr &pt2) BOOST_NOEXCEPT
479 { return difference_type(pt.get()- pt2.get()); }
482 BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
483 { return pt1.get() == pt2.get(); }
485 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
486 { return pt1.get() != pt2.get(); }
488 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
489 { return pt1.get() < pt2.get(); }
491 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
492 { return pt1.get() <= pt2.get(); }
494 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
495 { return pt1.get() > pt2.get(); }
497 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
498 { return pt1.get() >= pt2.get(); }
500 //Comparison to raw ptr to support literal 0
501 BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
502 { return pt1 == pt2.get(); }
504 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
505 { return pt1 != pt2.get(); }
507 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
508 { return pt1 < pt2.get(); }
510 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
511 { return pt1 <= pt2.get(); }
513 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
514 { return pt1 > pt2.get(); }
516 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
517 { return pt1 >= pt2.get(); }
520 BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
521 { return pt1.get() == pt2; }
523 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
524 { return pt1.get() != pt2; }
526 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
527 { return pt1.get() < pt2; }
529 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
530 { return pt1.get() <= pt2; }
532 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
533 { return pt1.get() > pt2; }
535 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
536 { return pt1.get() >= pt2; }
538 BOOST_INTERPROCESS_FORCEINLINE friend void swap(offset_ptr &left, offset_ptr &right) BOOST_NOEXCEPT
540 pointer ptr = right.get();
547 BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<true>) BOOST_NOEXCEPT
548 { //no need to pointer adjustment
549 this->internal.m_offset =
550 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset()));
554 BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<false>) BOOST_NOEXCEPT
555 { //we must convert to raw before calculating the offset
556 this->internal.m_offset =
557 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset(static_cast<PointedType*>(ptr.get()), this));
560 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
561 BOOST_INTERPROCESS_FORCEINLINE void inc_offset(DifferenceType bytes) BOOST_NOEXCEPT
562 { internal.m_offset += bytes; }
564 BOOST_INTERPROCESS_FORCEINLINE void dec_offset(DifferenceType bytes) BOOST_NOEXCEPT
565 { internal.m_offset -= bytes; }
567 ipcdetail::offset_ptr_internal<OffsetType, OffsetAlignment> internal;
570 BOOST_INTERPROCESS_FORCEINLINE const OffsetType &priv_offset() const BOOST_NOEXCEPT
571 { return internal.m_offset; }
573 BOOST_INTERPROCESS_FORCEINLINE OffsetType &priv_offset() BOOST_NOEXCEPT
574 { return internal.m_offset; }
576 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
581 template<class E, class T, class W, class X, class Y, std::size_t Z>
582 inline std::basic_ostream<E, T> & operator<<
583 (std::basic_ostream<E, T> & os, offset_ptr<W, X, Y, Z> const & p)
584 { return os << p.get_offset(); }
588 template<class E, class T, class W, class X, class Y, std::size_t Z>
589 inline std::basic_istream<E, T> & operator>>
590 (std::basic_istream<E, T> & is, offset_ptr<W, X, Y, Z> & p)
591 { return is >> p.get_offset(); }
593 //!Simulation of static_cast between pointers. Never throws.
594 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
595 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
596 static_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
598 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
599 (r, boost::interprocess::ipcdetail::static_cast_tag());
602 //!Simulation of const_cast between pointers. Never throws.
603 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
604 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
605 const_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
607 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
608 (r, boost::interprocess::ipcdetail::const_cast_tag());
611 //!Simulation of dynamic_cast between pointers. Never throws.
612 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
613 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
614 dynamic_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
616 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
617 (r, boost::interprocess::ipcdetail::dynamic_cast_tag());
620 //!Simulation of reinterpret_cast between pointers. Never throws.
621 template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2>
622 BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1>
623 reinterpret_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT
625 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
626 (r, boost::interprocess::ipcdetail::reinterpret_cast_tag());
629 } //namespace interprocess {
631 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
633 ///has_trivial_destructor<> == true_type specialization for optimizations
634 template <class T, class P, class O, std::size_t A>
635 struct has_trivial_destructor< ::boost::interprocess::offset_ptr<T, P, O, A> >
637 static const bool value = true;
640 namespace move_detail {
642 ///has_trivial_destructor<> == true_type specialization for optimizations
643 template <class T, class P, class O, std::size_t A>
644 struct is_trivially_destructible< ::boost::interprocess::offset_ptr<T, P, O, A> >
646 static const bool value = true;
649 } //namespace move_detail {
651 namespace interprocess {
653 //!to_raw_pointer() enables boost::mem_fn to recognize offset_ptr.
655 template <class T, class P, class O, std::size_t A>
656 BOOST_INTERPROCESS_FORCEINLINE T * to_raw_pointer(boost::interprocess::offset_ptr<T, P, O, A> const & p) BOOST_NOEXCEPT
657 { return ipcdetail::to_raw_pointer(p); }
659 } //namespace interprocess
662 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
663 } //namespace boost {
665 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
669 //This is to support embedding a bit in the pointer
670 //for intrusive containers, saving space
671 namespace intrusive {
673 //Predeclaration to avoid including header
674 template<class VoidPointer, std::size_t N>
675 struct max_pointer_plus_bits;
677 template<std::size_t OffsetAlignment, class P, class O, std::size_t A>
678 struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void, P, O, A>, OffsetAlignment>
680 //The offset ptr can embed one bit less than the alignment since it
681 //uses offset == 1 to store the null pointer.
682 static const std::size_t value = ::boost::interprocess::ipcdetail::ls_zeros<OffsetAlignment>::value - 1;
686 template<class Pointer, std::size_t NumBits>
687 struct pointer_plus_bits;
689 template<class T, class P, class O, std::size_t A, std::size_t NumBits>
690 struct pointer_plus_bits<boost::interprocess::offset_ptr<T, P, O, A>, NumBits>
692 typedef boost::interprocess::offset_ptr<T, P, O, A> pointer;
693 //Bits are stored in the lower bits of the pointer except the LSB,
694 //because this bit is used to represent the null pointer.
695 static const uintptr_t Mask = ((uintptr_t(1) << uintptr_t(NumBits)) - uintptr_t(1)) << uintptr_t(1);
696 BOOST_STATIC_ASSERT(0 ==(Mask&1));
698 //We must ALWAYS take argument "n" by reference as a copy of a null pointer
699 //with a bit (e.g. offset == 3) would be incorrectly copied and interpreted as non-null.
701 BOOST_INTERPROCESS_FORCEINLINE static pointer get_pointer(const pointer &n) BOOST_NOEXCEPT
704 O const tmp_off = n.priv_offset() & O(~Mask);
705 p.priv_offset() = boost::interprocess::ipcdetail::offset_ptr_to_offset_from_other(&p, &n, tmp_off);
709 BOOST_INTERPROCESS_FORCEINLINE static void set_pointer(pointer &n, const pointer &p) BOOST_NOEXCEPT
711 BOOST_ASSERT(0 == (get_bits)(p));
712 O const stored_bits = O(n.priv_offset() & Mask);
714 n.priv_offset() |= stored_bits;
717 BOOST_INTERPROCESS_FORCEINLINE static std::size_t get_bits(const pointer &n) BOOST_NOEXCEPT
719 return std::size_t((n.priv_offset() & Mask) >> 1u);
722 BOOST_INTERPROCESS_FORCEINLINE static void set_bits(pointer &n, std::size_t const b) BOOST_NOEXCEPT
724 BOOST_ASSERT(b < (std::size_t(1) << NumBits));
725 O tmp = n.priv_offset();
728 n.priv_offset() = tmp;
732 } //namespace intrusive
735 template<class T, class U>
736 struct pointer_to_other;
738 //Backwards compatibility with pointer_to_other
739 template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment, class U>
740 struct pointer_to_other
741 < ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment>, U >
743 typedef ::boost::interprocess::offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> type;
747 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
749 #include <boost/interprocess/detail/config_end.hpp>
751 #endif //#ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP