]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/offset_ptr.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / offset_ptr.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP
12 #define BOOST_INTERPROCESS_OFFSET_PTR_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24
25 #include <boost/type_traits/is_convertible.hpp>
26
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>
33 #include <iosfwd>
34
35 //!\file
36 //!Describes a smart pointer that stores the offset between this pointer and
37 //!target pointee, called offset_ptr.
38
39 namespace boost {
40
41 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42
43 //Predeclarations
44 template <class T>
45 struct has_trivial_destructor;
46
47 #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
48
49 namespace interprocess {
50
51 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
52 namespace ipcdetail {
53
54 template<class OffsetType, std::size_t OffsetAlignment>
55 union offset_ptr_internal
56 {
57 BOOST_STATIC_ASSERT(sizeof(OffsetType) >= sizeof(uintptr_t));
58
59 explicit offset_ptr_internal(OffsetType off)
60 : m_offset(off)
61 {}
62
63 OffsetType m_offset; //Distance between this object and pointee address
64
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;
69 };
70
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
74
75 //Undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_XXX if your compiler can inline
76 //this code without breaking the library
77
78 ////////////////////////////////////////////////////////////////////////
79 //
80 // offset_ptr_to_raw_pointer
81 //
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)
85 {
86 typedef pointer_uintptr_caster<void*> caster_t;
87 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR
88 if(offset == 1){
89 return 0;
90 }
91 else{
92 return caster_t(caster_t(this_ptr).uintptr() + offset).pointer();
93 }
94 #else
95 uintptr_t mask = offset == 1;
96 --mask;
97 uintptr_t target_offset = caster_t(this_ptr).uintptr() + offset;
98 target_offset &= mask;
99 return caster_t(target_offset).pointer();
100 #endif
101 }
102
103 ////////////////////////////////////////////////////////////////////////
104 //
105 // offset_ptr_to_offset
106 //
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)
110 {
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
114 if(!ptr){
115 return 1;
116 }
117 else{
118 uintptr_t offset = caster_t(ptr).uintptr() - caster_t(this_ptr).uintptr();
119 BOOST_ASSERT(offset != 1);
120 return offset;
121 }
122 #else
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);
126 //
127 uintptr_t offset = caster_t(ptr).uintptr() - caster_t(this_ptr).uintptr();
128 --offset;
129 uintptr_t mask = uintptr_t(ptr == 0);
130 --mask;
131 offset &= mask;
132 return ++offset;
133 #endif
134 }
135
136 ////////////////////////////////////////////////////////////////////////
137 //
138 // offset_ptr_to_offset_from_other
139 //
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)
144 {
145 typedef pointer_uintptr_caster<void*> caster_t;
146 #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER
147 if(other_offset == 1){
148 return 1;
149 }
150 else{
151 uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr() + other_offset;
152 BOOST_ASSERT(offset != 1);
153 return offset;
154 }
155 #else
156 uintptr_t mask = other_offset == 1;
157 --mask;
158 uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr();
159 offset &= mask;
160 return offset + other_offset;
161
162 //uintptr_t mask = -uintptr_t(other_offset != 1);
163 //uintptr_t offset = caster_t(other_ptr).uintptr() - caster_t(this_ptr).uintptr();
164 //offset &= mask;
165 //return offset + other_offset;
166 #endif
167 }
168
169 ////////////////////////////////////////////////////////////////////////
170 //
171 // Let's assume cast to void and cv cast don't change any target address
172 //
173 ////////////////////////////////////////////////////////////////////////
174 template<class From, class To>
175 struct offset_ptr_maintains_address
176 {
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
180 ;
181 };
182
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
187 , Ret>
188 {};
189
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
194 , Ret>
195 {};
196
197 } //namespace ipcdetail {
198 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
199
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.
206 //!
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.
215 //!
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>
222 class offset_ptr
223 {
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
229
230 public:
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
238 >::type value_type;
239 typedef DifferenceType difference_type;
240 typedef std::random_access_iterator_tag iterator_category;
241 typedef OffsetType offset_type;
242
243 public: //Public Functions
244
245 //!Default constructor (null pointer).
246 //!Never throws.
247 BOOST_INTERPROCESS_FORCEINLINE offset_ptr() BOOST_NOEXCEPT
248 : internal(1)
249 {}
250
251 //!Constructor from raw pointer (allows "0" pointer conversion).
252 //!Never throws.
253 BOOST_INTERPROCESS_FORCEINLINE offset_ptr(pointer ptr) BOOST_NOEXCEPT
254 : internal(static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset(ptr, this)))
255 {}
256
257 //!Constructor from other pointer.
258 //!Never throws.
259 template <class T>
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)))
264 {}
265
266 //!Constructor from other offset_ptr
267 //!Never throws.
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)))
271 {}
272
273 //!Constructor from other offset_ptr. If pointers of pointee types are
274 //!convertible, offset_ptrs will be convertibles. Never throws.
275 template<class T2>
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
279 #endif
280 ) BOOST_NOEXCEPT
281 : internal(static_cast<OffsetType>
282 (ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset())))
283 {}
284
285 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
286
287 //!Constructor from other offset_ptr. If pointers of pointee types are
288 //!convertible, offset_ptrs will be convertibles. Never throws.
289 template<class T2>
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)))
294 {}
295
296 #endif
297
298 //!Emulates static_cast operator.
299 //!Never throws.
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)))
304 {}
305
306 //!Emulates const_cast operator.
307 //!Never throws.
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)))
312 {}
313
314 //!Emulates dynamic_cast operator.
315 //!Never throws.
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)))
320 {}
321
322 //!Emulates reinterpret_cast operator.
323 //!Never throws.
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)))
328 {}
329
330 //!Obtains raw pointer from offset.
331 //!Never throws.
332 BOOST_INTERPROCESS_FORCEINLINE pointer get() const BOOST_NOEXCEPT
333 { return (pointer)ipcdetail::offset_ptr_to_raw_pointer(this, this->internal.m_offset); }
334
335 BOOST_INTERPROCESS_FORCEINLINE offset_type get_offset() const BOOST_NOEXCEPT
336 { return this->internal.m_offset; }
337
338 //!Pointer-like -> operator. It can return 0 pointer.
339 //!Never throws.
340 BOOST_INTERPROCESS_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT
341 { return this->get(); }
342
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
346 {
347 pointer p = this->get();
348 reference r = *p;
349 return r;
350 }
351
352 //!Indexing operator.
353 //!Never throws.
354 BOOST_INTERPROCESS_FORCEINLINE reference operator[](difference_type idx) const BOOST_NOEXCEPT
355 { return this->get()[idx]; }
356
357 //!Assignment from pointer (saves extra conversion).
358 //!Never throws.
359 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (pointer from) BOOST_NOEXCEPT
360 {
361 this->internal.m_offset =
362 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset(from, this));
363 return *this;
364 }
365
366 //!Assignment from other offset_ptr.
367 //!Never throws.
368 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (const offset_ptr & ptr) BOOST_NOEXCEPT
369 {
370 this->internal.m_offset =
371 static_cast<OffsetType>(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset));
372 return *this;
373 }
374
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
381 #else
382 offset_ptr&
383 #endif
384 operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr) BOOST_NOEXCEPT
385 {
386 this->assign(ptr, ipcdetail::bool_<ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value>());
387 return *this;
388 }
389
390 public:
391
392 //!offset_ptr += difference_type.
393 //!Never throws.
394 BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator+= (difference_type offset) BOOST_NOEXCEPT
395 { this->inc_offset(offset * sizeof (PointedType)); return *this; }
396
397 //!offset_ptr -= difference_type.
398 //!Never throws.
399 BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator-= (difference_type offset) BOOST_NOEXCEPT
400 { this->dec_offset(offset * sizeof (PointedType)); return *this; }
401
402 //!++offset_ptr.
403 //!Never throws.
404 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator++ (void) BOOST_NOEXCEPT
405 { this->inc_offset(sizeof (PointedType)); return *this; }
406
407 //!offset_ptr++.
408 //!Never throws.
409 BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator++ (int) BOOST_NOEXCEPT
410 {
411 offset_ptr tmp(*this);
412 this->inc_offset(sizeof (PointedType));
413 return tmp;
414 }
415
416 //!--offset_ptr.
417 //!Never throws.
418 BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator-- (void) BOOST_NOEXCEPT
419 { this->dec_offset(sizeof (PointedType)); return *this; }
420
421 //!offset_ptr--.
422 //!Never throws.
423 BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator-- (int) BOOST_NOEXCEPT
424 {
425 offset_ptr tmp(*this);
426 this->dec_offset(sizeof (PointedType));
427 return tmp;
428 }
429
430 //!safe bool conversion operator.
431 //!Never throws.
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; }
435 #else
436 explicit operator bool() const BOOST_NOEXCEPT
437 { return this->internal.m_offset != 1; }
438 #endif
439
440 //!Not operator. Not needed in theory, but improves portability.
441 //!Never throws
442 BOOST_INTERPROCESS_FORCEINLINE bool operator! () const BOOST_NOEXCEPT
443 { return this->internal.m_offset == 1; }
444
445 //!Compatibility with pointer_traits
446 //!
447 template <class U>
448 struct rebind
449 { typedef offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> other; };
450
451 //!Compatibility with pointer_traits
452 //!
453 BOOST_INTERPROCESS_FORCEINLINE static offset_ptr pointer_to(reference r) BOOST_NOEXCEPT
454 { return offset_ptr(&r); }
455
456 //!difference_type + offset_ptr
457 //!operation
458 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
459 { right += diff; return right; }
460
461 //!offset_ptr + difference_type
462 //!operation
463 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
464 { left += diff; return left; }
465
466 //!offset_ptr - diff
467 //!operation
468 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(offset_ptr left, difference_type diff) BOOST_NOEXCEPT
469 { left -= diff; return left; }
470
471 //!offset_ptr - diff
472 //!operation
473 BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(difference_type diff, offset_ptr right) BOOST_NOEXCEPT
474 { right -= diff; return right; }
475
476 //!offset_ptr - offset_ptr
477 //!operation
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()); }
480
481 //Comparison
482 BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
483 { return pt1.get() == pt2.get(); }
484
485 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
486 { return pt1.get() != pt2.get(); }
487
488 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
489 { return pt1.get() < pt2.get(); }
490
491 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
492 { return pt1.get() <= pt2.get(); }
493
494 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
495 { return pt1.get() > pt2.get(); }
496
497 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
498 { return pt1.get() >= pt2.get(); }
499
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(); }
503
504 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
505 { return pt1 != pt2.get(); }
506
507 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
508 { return pt1 < pt2.get(); }
509
510 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
511 { return pt1 <= pt2.get(); }
512
513 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
514 { return pt1 > pt2.get(); }
515
516 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT
517 { return pt1 >= pt2.get(); }
518
519 //Comparison
520 BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
521 { return pt1.get() == pt2; }
522
523 BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
524 { return pt1.get() != pt2; }
525
526 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
527 { return pt1.get() < pt2; }
528
529 BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
530 { return pt1.get() <= pt2; }
531
532 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
533 { return pt1.get() > pt2; }
534
535 BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT
536 { return pt1.get() >= pt2; }
537
538 BOOST_INTERPROCESS_FORCEINLINE friend void swap(offset_ptr &left, offset_ptr &right) BOOST_NOEXCEPT
539 {
540 pointer ptr = right.get();
541 right = left;
542 left = ptr;
543 }
544
545 private:
546 template<class T2>
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()));
551 }
552
553 template<class T2>
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));
558 }
559
560 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
561 BOOST_INTERPROCESS_FORCEINLINE void inc_offset(DifferenceType bytes) BOOST_NOEXCEPT
562 { internal.m_offset += bytes; }
563
564 BOOST_INTERPROCESS_FORCEINLINE void dec_offset(DifferenceType bytes) BOOST_NOEXCEPT
565 { internal.m_offset -= bytes; }
566
567 ipcdetail::offset_ptr_internal<OffsetType, OffsetAlignment> internal;
568
569 public:
570 BOOST_INTERPROCESS_FORCEINLINE const OffsetType &priv_offset() const BOOST_NOEXCEPT
571 { return internal.m_offset; }
572
573 BOOST_INTERPROCESS_FORCEINLINE OffsetType &priv_offset() BOOST_NOEXCEPT
574 { return internal.m_offset; }
575
576 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
577 };
578
579 //!operator<<
580 //!for offset ptr
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(); }
585
586 //!operator>>
587 //!for offset ptr
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(); }
592
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
597 {
598 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
599 (r, boost::interprocess::ipcdetail::static_cast_tag());
600 }
601
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
606 {
607 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
608 (r, boost::interprocess::ipcdetail::const_cast_tag());
609 }
610
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
615 {
616 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
617 (r, boost::interprocess::ipcdetail::dynamic_cast_tag());
618 }
619
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
624 {
625 return boost::interprocess::offset_ptr<T1, P1, O1, A1>
626 (r, boost::interprocess::ipcdetail::reinterpret_cast_tag());
627 }
628
629 } //namespace interprocess {
630
631 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
632
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> >
636 {
637 static const bool value = true;
638 };
639
640 namespace move_detail {
641
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> >
645 {
646 static const bool value = true;
647 };
648
649 } //namespace move_detail {
650
651 namespace interprocess {
652
653 //!to_raw_pointer() enables boost::mem_fn to recognize offset_ptr.
654 //!Never throws.
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); }
658
659 } //namespace interprocess
660
661
662 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
663 } //namespace boost {
664
665 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
666
667 namespace boost{
668
669 //This is to support embedding a bit in the pointer
670 //for intrusive containers, saving space
671 namespace intrusive {
672
673 //Predeclaration to avoid including header
674 template<class VoidPointer, std::size_t N>
675 struct max_pointer_plus_bits;
676
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>
679 {
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;
683 };
684
685 //Predeclaration
686 template<class Pointer, std::size_t NumBits>
687 struct pointer_plus_bits;
688
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>
691 {
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));
697
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.
700
701 BOOST_INTERPROCESS_FORCEINLINE static pointer get_pointer(const pointer &n) BOOST_NOEXCEPT
702 {
703 pointer p;
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);
706 return p;
707 }
708
709 BOOST_INTERPROCESS_FORCEINLINE static void set_pointer(pointer &n, const pointer &p) BOOST_NOEXCEPT
710 {
711 BOOST_ASSERT(0 == (get_bits)(p));
712 O const stored_bits = O(n.priv_offset() & Mask);
713 n = p;
714 n.priv_offset() |= stored_bits;
715 }
716
717 BOOST_INTERPROCESS_FORCEINLINE static std::size_t get_bits(const pointer &n) BOOST_NOEXCEPT
718 {
719 return std::size_t((n.priv_offset() & Mask) >> 1u);
720 }
721
722 BOOST_INTERPROCESS_FORCEINLINE static void set_bits(pointer &n, std::size_t const b) BOOST_NOEXCEPT
723 {
724 BOOST_ASSERT(b < (std::size_t(1) << NumBits));
725 O tmp = n.priv_offset();
726 tmp &= O(~Mask);
727 tmp |= O(b << 1u);
728 n.priv_offset() = tmp;
729 }
730 };
731
732 } //namespace intrusive
733
734 //Predeclaration
735 template<class T, class U>
736 struct pointer_to_other;
737
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 >
742 {
743 typedef ::boost::interprocess::offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> type;
744 };
745
746 } //namespace boost{
747 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
748
749 #include <boost/interprocess/detail/config_end.hpp>
750
751 #endif //#ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP