offset_ptr.hpp 30 KB

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