| 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 | #include <boost/type_traits/is_constructible.hpp> |
| 27 | #include <boost/type_traits/is_integral.hpp> |
| 28 | #include <boost/type_traits/is_unsigned.hpp> |
| 29 | |
| 30 | #include <boost/interprocess/interprocess_fwd.hpp> |
| 31 | #include <boost/interprocess/detail/utilities.hpp> |
| 32 | #include <boost/interprocess/detail/cast_tags.hpp> |
| 33 | #include <boost/interprocess/detail/mpl.hpp> |
| 34 | #include <boost/container/detail/type_traits.hpp> //alignment_of, aligned_storage |
| 35 | #include <boost/assert.hpp> |
| 36 | #include <boost/static_assert.hpp> |
| 37 | #include <iosfwd> |
| 38 | |
| 39 | #if defined(BOOST_GCC) && (BOOST_GCC >= 40600) |
| 40 | #pragma GCC diagnostic push |
| 41 | #pragma GCC diagnostic ignored "-Wmaybe-uninitialized" |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | //!\file |
| 46 | //!Describes a smart pointer that stores the offset between this pointer and |
| 47 | //!target pointee, called offset_ptr. |
| 48 | |
| 49 | namespace boost { |
| 50 | |
| 51 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 52 | |
| 53 | //Predeclarations |
| 54 | template <class T> |
| 55 | struct has_trivial_destructor; |
| 56 | |
| 57 | #endif //#if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 58 | |
| 59 | namespace interprocess { |
| 60 | |
| 61 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 62 | namespace ipcdetail { |
| 63 | |
| 64 | template<class OffsetType, std::size_t OffsetAlignment> |
| 65 | union offset_ptr_internal |
| 66 | { |
| 67 | BOOST_STATIC_ASSERT(sizeof(OffsetType) >= sizeof(uintptr_t)); |
| 68 | BOOST_STATIC_ASSERT(boost::is_integral<OffsetType>::value && boost::is_unsigned<OffsetType>::value); |
| 69 | |
| 70 | explicit offset_ptr_internal(OffsetType off) |
| 71 | : m_offset(off) |
| 72 | {} |
| 73 | |
| 74 | OffsetType m_offset; //Distance between this object and pointee address |
| 75 | |
| 76 | typename ::boost::container::dtl::aligned_storage |
| 77 | < sizeof(OffsetType)//for offset_type_alignment m_offset will be enough |
| 78 | , (OffsetAlignment == offset_type_alignment) ? 1u : OffsetAlignment |
| 79 | >::type alignment_helper; |
| 80 | }; |
| 81 | |
| 82 | //Note: using the address of a local variable to point to another address |
| 83 | //is not standard conforming and this can be optimized-away by the compiler. |
| 84 | //Non-inlining is a method to remain illegal but correct |
| 85 | |
| 86 | //Undef BOOST_INTERPROCESS_OFFSET_PTR_INLINE_XXX if your compiler can inline |
| 87 | //this code without breaking the library |
| 88 | |
| 89 | //////////////////////////////////////////////////////////////////////// |
| 90 | // |
| 91 | // offset_ptr_to_raw_pointer |
| 92 | // |
| 93 | //////////////////////////////////////////////////////////////////////// |
| 94 | #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR |
| 95 | template <class OffsetType> |
| 96 | BOOST_INTERPROCESS_FORCEINLINE void * offset_ptr_to_raw_pointer(const volatile void *this_ptr, OffsetType offset) |
| 97 | { |
| 98 | typedef pointer_offset_caster<void*, OffsetType> caster_t; |
| 99 | #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_PTR |
| 100 | if(offset == 1){ |
| 101 | return 0; |
| 102 | } |
| 103 | else{ |
| 104 | return caster_t(caster_t(this_ptr).offset() + offset).pointer(); |
| 105 | } |
| 106 | #else |
| 107 | OffsetType mask = offset == 1; |
| 108 | --mask; |
| 109 | OffsetType target_offset = caster_t(this_ptr).offset() + offset; |
| 110 | target_offset &= mask; |
| 111 | return caster_t(target_offset).pointer(); |
| 112 | #endif |
| 113 | } |
| 114 | |
| 115 | //////////////////////////////////////////////////////////////////////// |
| 116 | // |
| 117 | // offset_ptr_to_offset |
| 118 | // |
| 119 | //////////////////////////////////////////////////////////////////////// |
| 120 | #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF |
| 121 | template<class OffsetType> |
| 122 | BOOST_INTERPROCESS_FORCEINLINE OffsetType offset_ptr_to_offset(const volatile void *ptr, const volatile void *this_ptr) |
| 123 | { |
| 124 | typedef pointer_offset_caster<void*, OffsetType> caster_t; |
| 125 | #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF |
| 126 | //offset == 1 && ptr != 0 is not legal for this pointer |
| 127 | if(!ptr){ |
| 128 | return 1; |
| 129 | } |
| 130 | else{ |
| 131 | OffsetType offset = caster_t(ptr).offset()- caster_t(this_ptr).offset(); |
| 132 | BOOST_ASSERT(offset != 1); |
| 133 | return offset; |
| 134 | } |
| 135 | #else |
| 136 | //const OffsetType other = -OffsetType(ptr != 0); |
| 137 | //const OffsetType offset = (caster_t(ptr).offset() - caster_t(this_ptr).offset()) & other; |
| 138 | //return offset + OffsetType(!other); |
| 139 | // |
| 140 | OffsetType offset = caster_t(ptr).offset() - caster_t(this_ptr).offset(); |
| 141 | --offset; |
| 142 | OffsetType mask = ptr == 0; |
| 143 | --mask; |
| 144 | offset &= mask; |
| 145 | return ++offset; |
| 146 | #endif |
| 147 | } |
| 148 | |
| 149 | //////////////////////////////////////////////////////////////////////// |
| 150 | // |
| 151 | // offset_ptr_to_offset_from_other |
| 152 | // |
| 153 | //////////////////////////////////////////////////////////////////////// |
| 154 | #define BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER |
| 155 | template<class OffsetType> |
| 156 | BOOST_INTERPROCESS_FORCEINLINE OffsetType offset_ptr_to_offset_from_other |
| 157 | (const volatile void *this_ptr, const volatile void *other_ptr, OffsetType other_offset) |
| 158 | { |
| 159 | typedef pointer_offset_caster<void*, OffsetType> caster_t; |
| 160 | #ifndef BOOST_INTERPROCESS_OFFSET_PTR_BRANCHLESS_TO_OFF_FROM_OTHER |
| 161 | if(other_offset == 1){ |
| 162 | return 1; |
| 163 | } |
| 164 | else{ |
| 165 | OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset() + other_offset; |
| 166 | BOOST_ASSERT(offset != 1); |
| 167 | return offset; |
| 168 | } |
| 169 | #else |
| 170 | OffsetType mask = other_offset == 1; |
| 171 | --mask; |
| 172 | OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset(); |
| 173 | offset &= mask; |
| 174 | return offset + other_offset; |
| 175 | |
| 176 | //OffsetType mask = -OffsetType(other_offset != 1); |
| 177 | //OffsetType offset = caster_t(other_ptr).offset() - caster_t(this_ptr).offset(); |
| 178 | //offset &= mask; |
| 179 | //return offset + other_offset; |
| 180 | #endif |
| 181 | } |
| 182 | |
| 183 | //////////////////////////////////////////////////////////////////////// |
| 184 | // |
| 185 | // Let's assume cast to void and cv cast don't change any target address |
| 186 | // |
| 187 | //////////////////////////////////////////////////////////////////////// |
| 188 | template<class From, class To> |
| 189 | struct offset_ptr_maintains_address |
| 190 | { |
| 191 | static const bool value = ipcdetail::is_cv_same<From, To>::value |
| 192 | || ipcdetail::is_cv_same<void, To>::value |
| 193 | || ipcdetail::is_cv_same<char, To>::value |
| 194 | ; |
| 195 | }; |
| 196 | |
| 197 | template<class From, class To, class Ret = void> |
| 198 | struct enable_if_convertible_equal_address |
| 199 | : enable_if_c< ::boost::is_convertible<From*, To*>::value |
| 200 | && offset_ptr_maintains_address<From, To>::value |
| 201 | , Ret> |
| 202 | {}; |
| 203 | |
| 204 | template<class From, class To, class Ret = void> |
| 205 | struct enable_if_convertible_unequal_address |
| 206 | : enable_if_c< ::boost::is_convertible<From*, To*>::value |
| 207 | && !offset_ptr_maintains_address<From, To>::value |
| 208 | , Ret> |
| 209 | {}; |
| 210 | |
| 211 | } //namespace ipcdetail { |
| 212 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 213 | |
| 214 | //!A smart pointer that stores the offset between between the pointer and the |
| 215 | //!the object it points. This allows offset allows special properties, since |
| 216 | //!the pointer is independent from the address of the pointee, if the |
| 217 | //!pointer and the pointee are still separated by the same offset. This feature |
| 218 | //!converts offset_ptr in a smart pointer that can be placed in shared memory and |
| 219 | //!memory mapped files mapped in different addresses in every process. |
| 220 | //! |
| 221 | //! \tparam PointedType The type of the pointee. |
| 222 | //! \tparam DifferenceType A signed integer type that can represent the arithmetic operations on the pointer |
| 223 | //! \tparam OffsetType An unsigned integer type that can represent the |
| 224 | //! distance between two pointers reinterpret_cast-ed as unsigned integers. This type |
| 225 | //! should be at least of the same size of std::uintptr_t. In some systems it's possible to communicate |
| 226 | //! between 32 and 64 bit processes using 64 bit offsets. |
| 227 | //! \tparam OffsetAlignment Alignment of the OffsetType stored inside. In some systems might be necessary |
| 228 | //! to align it to 64 bits in order to communicate 32 and 64 bit processes using 64 bit offsets. |
| 229 | //! |
| 230 | //!<b>Note</b>: offset_ptr uses implementation defined properties, present in most platforms, for |
| 231 | //!performance reasons: |
| 232 | //! - Assumes that OffsetType representation of nullptr is (OffsetType)zero. |
| 233 | //! - Assumes that incrementing a OffsetType obtained from a pointer is equivalent |
| 234 | //! to incrementing the pointer and then converting it back to OffsetType. |
| 235 | template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment> |
| 236 | class offset_ptr |
| 237 | { |
| 238 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 239 | typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> self_t; |
| 240 | void unspecified_bool_type_func() const {} |
| 241 | typedef void (self_t::*unspecified_bool_type)() const; |
| 242 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 243 | |
| 244 | public: |
| 245 | typedef PointedType element_type; |
| 246 | typedef PointedType * pointer; |
| 247 | typedef typename ipcdetail:: |
| 248 | add_reference<PointedType>::type reference; |
| 249 | typedef typename ipcdetail:: |
| 250 | remove_volatile<typename ipcdetail:: |
| 251 | remove_const<PointedType>::type |
| 252 | >::type value_type; |
| 253 | typedef DifferenceType difference_type; |
| 254 | typedef std::random_access_iterator_tag iterator_category; |
| 255 | typedef OffsetType offset_type; |
| 256 | |
| 257 | public: //Public Functions |
| 258 | |
| 259 | //!Default constructor (null pointer). |
| 260 | //!Never throws. |
| 261 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr() BOOST_NOEXCEPT |
| 262 | : internal(1) |
| 263 | {} |
| 264 | |
| 265 | //!Constructor from raw pointer (allows "0" pointer conversion). |
| 266 | //!Never throws. |
| 267 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(pointer ptr) BOOST_NOEXCEPT |
| 268 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(ptr, this)) |
| 269 | {} |
| 270 | |
| 271 | //!Constructor from other pointer. |
| 272 | //!Never throws. |
| 273 | template <class T> |
| 274 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr( T *ptr |
| 275 | , typename ipcdetail::enable_if< ::boost::is_convertible<T*, PointedType*> >::type * = 0) BOOST_NOEXCEPT |
| 276 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr), this)) |
| 277 | {} |
| 278 | |
| 279 | //!Constructor from other offset_ptr |
| 280 | //!Never throws. |
| 281 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr& ptr) BOOST_NOEXCEPT |
| 282 | : internal(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset)) |
| 283 | {} |
| 284 | |
| 285 | //!Constructor from other offset_ptr. Only takes part in overload resolution |
| 286 | //!if T2* is convertible to PointedType*. Never throws. |
| 287 | template<class T2> |
| 288 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr |
| 289 | #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 290 | , typename ipcdetail::enable_if_convertible_equal_address<T2, PointedType>::type* = 0 |
| 291 | #endif |
| 292 | ) BOOST_NOEXCEPT |
| 293 | : internal(ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.get_offset())) |
| 294 | {} |
| 295 | |
| 296 | #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 297 | |
| 298 | template<class T2> |
| 299 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr( const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr |
| 300 | , typename ipcdetail::enable_if_convertible_unequal_address<T2, PointedType>::type* = 0) BOOST_NOEXCEPT |
| 301 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this)) |
| 302 | {} |
| 303 | |
| 304 | #endif |
| 305 | |
| 306 | //!Constructor from other offset_ptr. Only takes part in overload resolution |
| 307 | //!if PointedType* is constructible from T2* other than via a conversion (e.g. cast to a derived class). Never throws. |
| 308 | template<class T2> |
| 309 | BOOST_INTERPROCESS_FORCEINLINE explicit offset_ptr(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr |
| 310 | #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 311 | , typename ipcdetail::enable_if_c< |
| 312 | !::boost::is_convertible<T2*, PointedType*>::value && ::boost::is_constructible<T2*, PointedType*>::value |
| 313 | >::type * = 0 |
| 314 | #endif |
| 315 | ) BOOST_NOEXCEPT |
| 316 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this)) |
| 317 | {} |
| 318 | |
| 319 | //!Emulates static_cast operator. |
| 320 | //!Never throws. |
| 321 | template<class T2, class P2, class O2, std::size_t A2> |
| 322 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::static_cast_tag) BOOST_NOEXCEPT |
| 323 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(r.get()), this)) |
| 324 | {} |
| 325 | |
| 326 | //!Emulates const_cast operator. |
| 327 | //!Never throws. |
| 328 | template<class T2, class P2, class O2, std::size_t A2> |
| 329 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::const_cast_tag) BOOST_NOEXCEPT |
| 330 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(const_cast<PointedType*>(r.get()), this)) |
| 331 | {} |
| 332 | |
| 333 | //!Emulates dynamic_cast operator. |
| 334 | //!Never throws. |
| 335 | template<class T2, class P2, class O2, std::size_t A2> |
| 336 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::dynamic_cast_tag) BOOST_NOEXCEPT |
| 337 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(dynamic_cast<PointedType*>(r.get()), this)) |
| 338 | {} |
| 339 | |
| 340 | //!Emulates reinterpret_cast operator. |
| 341 | //!Never throws. |
| 342 | template<class T2, class P2, class O2, std::size_t A2> |
| 343 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr(const offset_ptr<T2, P2, O2, A2> & r, ipcdetail::reinterpret_cast_tag) BOOST_NOEXCEPT |
| 344 | : internal(ipcdetail::offset_ptr_to_offset<OffsetType>(reinterpret_cast<PointedType*>(r.get()), this)) |
| 345 | {} |
| 346 | |
| 347 | //!Obtains raw pointer from offset. |
| 348 | //!Never throws. |
| 349 | BOOST_INTERPROCESS_FORCEINLINE pointer get() const BOOST_NOEXCEPT |
| 350 | { return static_cast<pointer>(ipcdetail::offset_ptr_to_raw_pointer(this, this->internal.m_offset)); } |
| 351 | |
| 352 | BOOST_INTERPROCESS_FORCEINLINE offset_type get_offset() const BOOST_NOEXCEPT |
| 353 | { return this->internal.m_offset; } |
| 354 | |
| 355 | //!Pointer-like -> operator. It can return 0 pointer. |
| 356 | //!Never throws. |
| 357 | BOOST_INTERPROCESS_FORCEINLINE pointer operator->() const BOOST_NOEXCEPT |
| 358 | { return this->get(); } |
| 359 | |
| 360 | //!Dereferencing operator, if it is a null offset_ptr behavior |
| 361 | //! is undefined. Never throws. |
| 362 | BOOST_INTERPROCESS_FORCEINLINE reference operator* () const BOOST_NOEXCEPT |
| 363 | { |
| 364 | pointer p = this->get(); |
| 365 | reference r = *p; |
| 366 | return r; |
| 367 | } |
| 368 | |
| 369 | //!Indexing operator. |
| 370 | //!Never throws. |
| 371 | BOOST_INTERPROCESS_FORCEINLINE reference operator[](difference_type idx) const BOOST_NOEXCEPT |
| 372 | { return this->get()[idx]; } |
| 373 | |
| 374 | //!Assignment from pointer (saves extra conversion). |
| 375 | //!Never throws. |
| 376 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (pointer from) BOOST_NOEXCEPT |
| 377 | { |
| 378 | this->internal.m_offset = ipcdetail::offset_ptr_to_offset<OffsetType>(from, this); |
| 379 | return *this; |
| 380 | } |
| 381 | |
| 382 | //!Assignment from other offset_ptr. |
| 383 | //!Never throws. |
| 384 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator= (const offset_ptr & ptr) BOOST_NOEXCEPT |
| 385 | { |
| 386 | this->internal.m_offset = ipcdetail::offset_ptr_to_offset_from_other(this, &ptr, ptr.internal.m_offset); |
| 387 | return *this; |
| 388 | } |
| 389 | |
| 390 | //!Assignment from related offset_ptr. If pointers of pointee types |
| 391 | //! are assignable, offset_ptrs will be assignable. Never throws. |
| 392 | template<class T2> BOOST_INTERPROCESS_FORCEINLINE |
| 393 | #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 394 | typename ipcdetail::enable_if_c |
| 395 | < ::boost::is_convertible<T2*, PointedType*>::value, offset_ptr&>::type |
| 396 | #else |
| 397 | offset_ptr& |
| 398 | #endif |
| 399 | operator= (const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr) BOOST_NOEXCEPT |
| 400 | { |
| 401 | this->assign(ptr, ipcdetail::bool_<ipcdetail::offset_ptr_maintains_address<T2, PointedType>::value>()); |
| 402 | return *this; |
| 403 | } |
| 404 | |
| 405 | public: |
| 406 | |
| 407 | //!offset_ptr += difference_type. |
| 408 | //!Never throws. |
| 409 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator+= (difference_type offset) BOOST_NOEXCEPT |
| 410 | { this->inc_offset(bytes: offset * difference_type(sizeof(PointedType))); return *this; } |
| 411 | |
| 412 | //!offset_ptr -= difference_type. |
| 413 | //!Never throws. |
| 414 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr &operator-= (difference_type offset) BOOST_NOEXCEPT |
| 415 | { this->dec_offset(bytes: offset * difference_type(sizeof(PointedType))); return *this; } |
| 416 | |
| 417 | //!++offset_ptr. |
| 418 | //!Never throws. |
| 419 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator++ (void) BOOST_NOEXCEPT |
| 420 | { this->inc_offset(bytes: difference_type(sizeof(PointedType))); return *this; } |
| 421 | |
| 422 | //!offset_ptr++. |
| 423 | //!Never throws. |
| 424 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator++ (int) BOOST_NOEXCEPT |
| 425 | { |
| 426 | offset_ptr tmp(*this); |
| 427 | this->inc_offset(bytes: sizeof (PointedType)); |
| 428 | return tmp; |
| 429 | } |
| 430 | |
| 431 | //!--offset_ptr. |
| 432 | //!Never throws. |
| 433 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr& operator-- (void) BOOST_NOEXCEPT |
| 434 | { this->dec_offset(bytes: sizeof (PointedType)); return *this; } |
| 435 | |
| 436 | //!offset_ptr--. |
| 437 | //!Never throws. |
| 438 | BOOST_INTERPROCESS_FORCEINLINE offset_ptr operator-- (int) BOOST_NOEXCEPT |
| 439 | { |
| 440 | offset_ptr tmp(*this); |
| 441 | this->dec_offset(bytes: sizeof (PointedType)); |
| 442 | return tmp; |
| 443 | } |
| 444 | |
| 445 | //!safe bool conversion operator. |
| 446 | //!Never throws. |
| 447 | #if defined(BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS) |
| 448 | BOOST_INTERPROCESS_FORCEINLINE operator unspecified_bool_type() const BOOST_NOEXCEPT |
| 449 | { return this->internal.m_offset != 1? &self_t::unspecified_bool_type_func : 0; } |
| 450 | #else |
| 451 | explicit operator bool() const BOOST_NOEXCEPT |
| 452 | { return this->internal.m_offset != 1; } |
| 453 | #endif |
| 454 | |
| 455 | //!Not operator. Not needed in theory, but improves portability. |
| 456 | //!Never throws |
| 457 | BOOST_INTERPROCESS_FORCEINLINE bool operator! () const BOOST_NOEXCEPT |
| 458 | { return this->internal.m_offset == 1; } |
| 459 | |
| 460 | //!Compatibility with pointer_traits |
| 461 | //! |
| 462 | #if defined(BOOST_NO_CXX11_TEMPLATE_ALIASES) |
| 463 | template <class U> |
| 464 | struct rebind |
| 465 | { typedef offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> other; }; |
| 466 | #else |
| 467 | template <class U> |
| 468 | using rebind = offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment>; |
| 469 | #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 470 | typedef offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment> other; |
| 471 | #endif //BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 472 | #endif |
| 473 | |
| 474 | //!Compatibility with pointer_traits |
| 475 | //! |
| 476 | BOOST_INTERPROCESS_FORCEINLINE static offset_ptr pointer_to(reference r) BOOST_NOEXCEPT |
| 477 | { return offset_ptr(&r); } |
| 478 | |
| 479 | //!difference_type + offset_ptr |
| 480 | //!operation |
| 481 | BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(difference_type diff, offset_ptr right) BOOST_NOEXCEPT |
| 482 | { right += diff; return right; } |
| 483 | |
| 484 | //!offset_ptr + difference_type |
| 485 | //!operation |
| 486 | BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator+(offset_ptr left, difference_type diff) BOOST_NOEXCEPT |
| 487 | { left += diff; return left; } |
| 488 | |
| 489 | //!offset_ptr - diff |
| 490 | //!operation |
| 491 | BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(offset_ptr left, difference_type diff) BOOST_NOEXCEPT |
| 492 | { left -= diff; return left; } |
| 493 | |
| 494 | //!offset_ptr - diff |
| 495 | //!operation |
| 496 | BOOST_INTERPROCESS_FORCEINLINE friend offset_ptr operator-(difference_type diff, offset_ptr right) BOOST_NOEXCEPT |
| 497 | { right -= diff; return right; } |
| 498 | |
| 499 | //!offset_ptr - offset_ptr |
| 500 | //!operation |
| 501 | BOOST_INTERPROCESS_FORCEINLINE friend difference_type operator-(const offset_ptr &pt, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 502 | { return difference_type(pt.get()- pt2.get()); } |
| 503 | |
| 504 | //Comparison |
| 505 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 506 | { return pt1.get() == pt2.get(); } |
| 507 | |
| 508 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 509 | { return pt1.get() != pt2.get(); } |
| 510 | |
| 511 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 512 | { return pt1.get() < pt2.get(); } |
| 513 | |
| 514 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 515 | { return pt1.get() <= pt2.get(); } |
| 516 | |
| 517 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 518 | { return pt1.get() > pt2.get(); } |
| 519 | |
| 520 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 521 | { return pt1.get() >= pt2.get(); } |
| 522 | |
| 523 | //Comparison to raw ptr to support literal 0 |
| 524 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 525 | { return pt1 == pt2.get(); } |
| 526 | |
| 527 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 528 | { return pt1 != pt2.get(); } |
| 529 | |
| 530 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 531 | { return pt1 < pt2.get(); } |
| 532 | |
| 533 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 534 | { return pt1 <= pt2.get(); } |
| 535 | |
| 536 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 537 | { return pt1 > pt2.get(); } |
| 538 | |
| 539 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(pointer pt1, const offset_ptr &pt2) BOOST_NOEXCEPT |
| 540 | { return pt1 >= pt2.get(); } |
| 541 | |
| 542 | //Comparison |
| 543 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator== (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 544 | { return pt1.get() == pt2; } |
| 545 | |
| 546 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator!= (const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 547 | { return pt1.get() != pt2; } |
| 548 | |
| 549 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 550 | { return pt1.get() < pt2; } |
| 551 | |
| 552 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator<=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 553 | { return pt1.get() <= pt2; } |
| 554 | |
| 555 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 556 | { return pt1.get() > pt2; } |
| 557 | |
| 558 | BOOST_INTERPROCESS_FORCEINLINE friend bool operator>=(const offset_ptr &pt1, pointer pt2) BOOST_NOEXCEPT |
| 559 | { return pt1.get() >= pt2; } |
| 560 | |
| 561 | BOOST_INTERPROCESS_FORCEINLINE friend void swap(offset_ptr &left, offset_ptr &right) BOOST_NOEXCEPT |
| 562 | { |
| 563 | pointer ptr = right.get(); |
| 564 | right = left; |
| 565 | left = ptr; |
| 566 | } |
| 567 | |
| 568 | private: |
| 569 | template<class T2> |
| 570 | BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<true>) BOOST_NOEXCEPT |
| 571 | { //no need to pointer adjustment |
| 572 | this->internal.m_offset = ipcdetail::offset_ptr_to_offset_from_other<OffsetType>(this, &ptr, ptr.get_offset()); |
| 573 | } |
| 574 | |
| 575 | template<class T2> |
| 576 | BOOST_INTERPROCESS_FORCEINLINE void assign(const offset_ptr<T2, DifferenceType, OffsetType, OffsetAlignment> &ptr, ipcdetail::bool_<false>) BOOST_NOEXCEPT |
| 577 | { //we must convert to raw before calculating the offset |
| 578 | this->internal.m_offset = ipcdetail::offset_ptr_to_offset<OffsetType>(static_cast<PointedType*>(ptr.get()), this); |
| 579 | } |
| 580 | |
| 581 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 582 | BOOST_INTERPROCESS_FORCEINLINE void inc_offset(DifferenceType bytes) BOOST_NOEXCEPT |
| 583 | { internal.m_offset += OffsetType(bytes); } |
| 584 | |
| 585 | BOOST_INTERPROCESS_FORCEINLINE void dec_offset(DifferenceType bytes) BOOST_NOEXCEPT |
| 586 | { internal.m_offset -= OffsetType(bytes); } |
| 587 | |
| 588 | ipcdetail::offset_ptr_internal<OffsetType, OffsetAlignment> internal; |
| 589 | |
| 590 | public: |
| 591 | BOOST_INTERPROCESS_FORCEINLINE const OffsetType &priv_offset() const BOOST_NOEXCEPT |
| 592 | { return internal.m_offset; } |
| 593 | |
| 594 | BOOST_INTERPROCESS_FORCEINLINE OffsetType &priv_offset() BOOST_NOEXCEPT |
| 595 | { return internal.m_offset; } |
| 596 | |
| 597 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 598 | }; |
| 599 | |
| 600 | //!operator<< |
| 601 | //!for offset ptr |
| 602 | template<class E, class T, class W, class X, class Y, std::size_t Z> |
| 603 | inline std::basic_ostream<E, T> & operator<< |
| 604 | (std::basic_ostream<E, T> & os, offset_ptr<W, X, Y, Z> const & p) |
| 605 | { return os << p.get_offset(); } |
| 606 | |
| 607 | //!operator>> |
| 608 | //!for offset ptr |
| 609 | template<class E, class T, class W, class X, class Y, std::size_t Z> |
| 610 | inline std::basic_istream<E, T> & operator>> |
| 611 | (std::basic_istream<E, T> & is, offset_ptr<W, X, Y, Z> & p) |
| 612 | { return is >> p.get_offset(); } |
| 613 | |
| 614 | //!Simulation of static_cast between pointers. Never throws. |
| 615 | template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2> |
| 616 | BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 617 | static_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT |
| 618 | { |
| 619 | return boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 620 | (r, boost::interprocess::ipcdetail::static_cast_tag()); |
| 621 | } |
| 622 | |
| 623 | //!Simulation of const_cast between pointers. Never throws. |
| 624 | template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2> |
| 625 | BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 626 | const_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT |
| 627 | { |
| 628 | return boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 629 | (r, boost::interprocess::ipcdetail::const_cast_tag()); |
| 630 | } |
| 631 | |
| 632 | //!Simulation of dynamic_cast between pointers. Never throws. |
| 633 | template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2> |
| 634 | BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 635 | dynamic_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT |
| 636 | { |
| 637 | return boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 638 | (r, boost::interprocess::ipcdetail::dynamic_cast_tag()); |
| 639 | } |
| 640 | |
| 641 | //!Simulation of reinterpret_cast between pointers. Never throws. |
| 642 | template<class T1, class P1, class O1, std::size_t A1, class T2, class P2, class O2, std::size_t A2> |
| 643 | BOOST_INTERPROCESS_FORCEINLINE boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 644 | reinterpret_pointer_cast(const boost::interprocess::offset_ptr<T2, P2, O2, A2> & r) BOOST_NOEXCEPT |
| 645 | { |
| 646 | return boost::interprocess::offset_ptr<T1, P1, O1, A1> |
| 647 | (r, boost::interprocess::ipcdetail::reinterpret_cast_tag()); |
| 648 | } |
| 649 | |
| 650 | } //namespace interprocess { |
| 651 | |
| 652 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 653 | |
| 654 | ///has_trivial_destructor<> == true_type specialization for optimizations |
| 655 | template <class T, class P, class O, std::size_t A> |
| 656 | struct has_trivial_destructor< ::boost::interprocess::offset_ptr<T, P, O, A> > |
| 657 | { |
| 658 | static const bool value = true; |
| 659 | }; |
| 660 | |
| 661 | namespace move_detail { |
| 662 | |
| 663 | ///has_trivial_destructor<> == true_type specialization for optimizations |
| 664 | template <class T, class P, class O, std::size_t A> |
| 665 | struct is_trivially_destructible< ::boost::interprocess::offset_ptr<T, P, O, A> > |
| 666 | { |
| 667 | static const bool value = true; |
| 668 | }; |
| 669 | |
| 670 | } //namespace move_detail { |
| 671 | |
| 672 | namespace interprocess { |
| 673 | |
| 674 | //!to_raw_pointer() enables boost::mem_fn to recognize offset_ptr. |
| 675 | //!Never throws. |
| 676 | template <class T, class P, class O, std::size_t A> |
| 677 | BOOST_INTERPROCESS_FORCEINLINE T * to_raw_pointer(boost::interprocess::offset_ptr<T, P, O, A> const & p) BOOST_NOEXCEPT |
| 678 | { return ipcdetail::to_raw_pointer(p); } |
| 679 | |
| 680 | } //namespace interprocess |
| 681 | |
| 682 | |
| 683 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 684 | } //namespace boost { |
| 685 | |
| 686 | #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED) |
| 687 | |
| 688 | namespace boost{ |
| 689 | |
| 690 | //This is to support embedding a bit in the pointer |
| 691 | //for intrusive containers, saving space |
| 692 | namespace intrusive { |
| 693 | |
| 694 | //Predeclaration to avoid including header |
| 695 | template<class VoidPointer, std::size_t N> |
| 696 | struct max_pointer_plus_bits; |
| 697 | |
| 698 | template<std::size_t OffsetAlignment, class P, class O, std::size_t A> |
| 699 | struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void, P, O, A>, OffsetAlignment> |
| 700 | { |
| 701 | //The offset ptr can embed one bit less than the alignment since it |
| 702 | //uses offset == 1 to store the null pointer. |
| 703 | static const std::size_t value = ::boost::interprocess::ipcdetail::ls_zeros<OffsetAlignment>::value - 1; |
| 704 | }; |
| 705 | |
| 706 | //Predeclaration |
| 707 | template<class Pointer, std::size_t NumBits> |
| 708 | struct pointer_plus_bits; |
| 709 | |
| 710 | template<class T, class P, class O, std::size_t A, std::size_t NumBits> |
| 711 | struct pointer_plus_bits<boost::interprocess::offset_ptr<T, P, O, A>, NumBits> |
| 712 | { |
| 713 | typedef boost::interprocess::offset_ptr<T, P, O, A> pointer; |
| 714 | //Bits are stored in the lower bits of the pointer except the LSB, |
| 715 | //because this bit is used to represent the null pointer. |
| 716 | static const O Mask = ((static_cast<O>(1) << NumBits) - static_cast<O>(1)) << 1; |
| 717 | BOOST_STATIC_ASSERT(0 ==(Mask&1)); |
| 718 | |
| 719 | //We must ALWAYS take argument "n" by reference as a copy of a null pointer |
| 720 | //with a bit (e.g. offset == 3) would be incorrectly copied and interpreted as non-null. |
| 721 | |
| 722 | BOOST_INTERPROCESS_FORCEINLINE static pointer get_pointer(const pointer &n) BOOST_NOEXCEPT |
| 723 | { |
| 724 | pointer p; |
| 725 | O const tmp_off = n.priv_offset() & ~Mask; |
| 726 | p.priv_offset() = boost::interprocess::ipcdetail::offset_ptr_to_offset_from_other(&p, &n, tmp_off); |
| 727 | return p; |
| 728 | } |
| 729 | |
| 730 | BOOST_INTERPROCESS_FORCEINLINE static void set_pointer(pointer &n, const pointer &p) BOOST_NOEXCEPT |
| 731 | { |
| 732 | BOOST_ASSERT(0 == (get_bits)(p)); |
| 733 | O const stored_bits = n.priv_offset() & Mask; |
| 734 | n = p; |
| 735 | n.priv_offset() |= stored_bits; |
| 736 | } |
| 737 | |
| 738 | BOOST_INTERPROCESS_FORCEINLINE static std::size_t get_bits(const pointer &n) BOOST_NOEXCEPT |
| 739 | { |
| 740 | return std::size_t((n.priv_offset() & Mask) >> 1u); |
| 741 | } |
| 742 | |
| 743 | BOOST_INTERPROCESS_FORCEINLINE static void set_bits(pointer &n, std::size_t const b) BOOST_NOEXCEPT |
| 744 | { |
| 745 | BOOST_ASSERT(b < (std::size_t(1) << NumBits)); |
| 746 | O tmp = n.priv_offset(); |
| 747 | tmp &= ~Mask; |
| 748 | tmp |= O(b << 1u); |
| 749 | n.priv_offset() = tmp; |
| 750 | } |
| 751 | }; |
| 752 | |
| 753 | } //namespace intrusive |
| 754 | |
| 755 | //Predeclaration |
| 756 | template<class T, class U> |
| 757 | struct pointer_to_other; |
| 758 | |
| 759 | //Backwards compatibility with pointer_to_other |
| 760 | template <class PointedType, class DifferenceType, class OffsetType, std::size_t OffsetAlignment, class U> |
| 761 | struct pointer_to_other |
| 762 | < ::boost::interprocess::offset_ptr<PointedType, DifferenceType, OffsetType, OffsetAlignment>, U > |
| 763 | { |
| 764 | typedef ::boost::interprocess::offset_ptr<U, DifferenceType, OffsetType, OffsetAlignment> type; |
| 765 | }; |
| 766 | |
| 767 | } //namespace boost{ |
| 768 | #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED |
| 769 | |
| 770 | #include <boost/interprocess/detail/config_end.hpp> |
| 771 | |
| 772 | #if defined(BOOST_GCC) && (BOOST_GCC >= 40600) |
| 773 | #pragma GCC diagnostic pop |
| 774 | #endif |
| 775 | |
| 776 | #endif //#ifndef BOOST_INTERPROCESS_OFFSET_PTR_HPP |
| 777 | |