1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_OFFSET_PTR_HPP
12 #define BOOST_OFFSET_PTR_HPP
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #include <boost/interprocess/interprocess_fwd.hpp>
22 #include <boost/interprocess/detail/utilities.hpp>
23 #include <boost/interprocess/detail/cast_tags.hpp>
24 #include <boost/interprocess/detail/mpl.hpp>
25 #include <boost/pointer_cast.hpp>
26 #include <boost/assert.hpp>
27 #include <boost/assert.hpp>
33 //!Describes a smart pointer that stores the offset between this pointer and
34 //!target pointee, called offset_ptr.
40 struct has_trivial_constructor;
43 struct has_trivial_destructor;
45 namespace interprocess {
47 //!A smart pointer that stores the offset between between the pointer and the
48 //!the object it points. This allows offset allows special properties, since
49 //!the pointer is independent from the address address of the pointee, if the
50 //!pointer and the pointee are still separated by the same offset. This feature
51 //!converts offset_ptr in a smart pointer that can be placed in shared memory and
52 //!memory mapped files mapped in different addresses in every process.
53 template <class PointedType>
57 typedef offset_ptr<PointedType> self_t;
59 void unspecified_bool_type_func() const {}
60 typedef void (self_t::*unspecified_bool_type)() const;
62 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
63 __declspec(noinline) //this workaround is needed for msvc > 8.0
65 void set_offset(const PointedType *ptr)
67 //offset == 1 && ptr != 0 is not legal for this pointer
69 internal.m_offset = 1;
72 internal.m_offset = (const char*)ptr - (const char*)(this);
73 BOOST_ASSERT(internal.m_offset != 1);
77 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
78 __declspec(noinline) //this workaround is needed for msvc-8.0 and msvc-9.0
80 void* get_pointer() const
81 { return (internal.m_offset == 1) ? 0 : (const_cast<char*>(reinterpret_cast<const char*>(this)) + internal.m_offset); }
83 void inc_offset(std::ptrdiff_t bytes)
84 { internal.m_offset += bytes; }
86 void dec_offset(std::ptrdiff_t bytes)
87 { internal.m_offset -= bytes; }
90 std::ptrdiff_t m_offset; //Distance between this object and pointed address
91 PointedType *aliasing_helper;
96 typedef PointedType * pointer;
97 typedef typename detail::
98 add_reference<PointedType>::type reference;
99 typedef PointedType value_type;
100 typedef std::ptrdiff_t difference_type;
101 typedef std::random_access_iterator_tag iterator_category;
103 public: //Public Functions
105 //!Constructor from raw pointer (allows "0" pointer conversion).
107 offset_ptr(pointer ptr = 0) { this->set_offset(ptr); }
109 //!Constructor from other pointer.
113 { pointer p (ptr); (void)p; this->set_offset(p); }
115 //!Constructor from other offset_ptr
117 offset_ptr(const offset_ptr& ptr)
118 { this->set_offset(ptr.get()); }
120 //!Constructor from other offset_ptr. If pointers of pointee types are
121 //!convertible, offset_ptrs will be convertibles. Never throws.
123 offset_ptr(const offset_ptr<T2> &ptr)
124 { pointer p(ptr.get()); (void)p; this->set_offset(p); }
126 //!Emulates static_cast operator.
129 offset_ptr(const offset_ptr<Y> & r, detail::static_cast_tag)
130 { this->set_offset(static_cast<PointedType*>(r.get())); }
132 //!Emulates const_cast operator.
135 offset_ptr(const offset_ptr<Y> & r, detail::const_cast_tag)
136 { this->set_offset(const_cast<PointedType*>(r.get())); }
138 //!Emulates dynamic_cast operator.
141 offset_ptr(const offset_ptr<Y> & r, detail::dynamic_cast_tag)
142 { this->set_offset(dynamic_cast<PointedType*>(r.get())); }
144 //!Emulates reinterpret_cast operator.
147 offset_ptr(const offset_ptr<Y> & r, detail::reinterpret_cast_tag)
148 { this->set_offset(reinterpret_cast<PointedType*>(r.get())); }
150 //!Obtains raw pointer from offset.
153 { return static_cast<pointer>(this->get_pointer()); }
155 std::ptrdiff_t get_offset() const
156 { return internal.m_offset; }
158 //!Pointer-like -> operator. It can return 0 pointer.
160 pointer operator->() const
161 { return this->get(); }
163 //!Dereferencing operator, if it is a null offset_ptr behavior
164 //! is undefined. Never throws.
165 reference operator* () const
167 pointer p = this->get();
172 //!Indexing operator.
174 reference operator[](std::ptrdiff_t idx) const
175 { return this->get()[idx]; }
177 //!Assignment from pointer (saves extra conversion).
179 offset_ptr& operator= (pointer from)
180 { this->set_offset(from); return *this; }
182 //!Assignment from other offset_ptr.
184 offset_ptr& operator= (const offset_ptr & pt)
185 { pointer p(pt.get()); (void)p; this->set_offset(p); return *this; }
187 //!Assignment from related offset_ptr. If pointers of pointee types
188 //! are assignable, offset_ptrs will be assignable. Never throws.
190 offset_ptr& operator= (const offset_ptr<T2> & pt)
191 { pointer p(pt.get()); this->set_offset(p); return *this; }
193 //!offset_ptr + std::ptrdiff_t.
195 offset_ptr operator+ (std::ptrdiff_t offset) const
196 { return offset_ptr(this->get()+offset); }
198 //!offset_ptr - std::ptrdiff_t.
200 offset_ptr operator- (std::ptrdiff_t offset) const
201 { return offset_ptr(this->get()-offset); }
203 //!offset_ptr += std::ptrdiff_t.
205 offset_ptr &operator+= (std::ptrdiff_t offset)
206 { this->inc_offset(offset * sizeof (PointedType)); return *this; }
208 //!offset_ptr -= std::ptrdiff_t.
210 offset_ptr &operator-= (std::ptrdiff_t offset)
211 { this->dec_offset(offset * sizeof (PointedType)); return *this; }
215 offset_ptr& operator++ (void)
216 { this->inc_offset(sizeof (PointedType)); return *this; }
220 offset_ptr operator++ (int)
221 { offset_ptr temp(*this); ++*this; return temp; }
225 offset_ptr& operator-- (void)
226 { this->dec_offset(sizeof (PointedType)); return *this; }
230 offset_ptr operator-- (int)
231 { offset_ptr temp(*this); --*this; return temp; }
233 //!safe bool conversion operator.
235 operator unspecified_bool_type() const
236 { return this->get()? &self_t::unspecified_bool_type_func : 0; }
238 //!Not operator. Not needed in theory, but improves portability.
240 bool operator! () const
241 { return this->get() == 0; }
243 friend void swap (offset_ptr &pt, offset_ptr &pt2)
245 value_type *ptr = pt.get();
252 //!offset_ptr<T1> == offset_ptr<T2>.
254 template<class T1, class T2>
255 inline bool operator== (const offset_ptr<T1> &pt1,
256 const offset_ptr<T2> &pt2)
257 { return pt1.get() == pt2.get(); }
259 //!offset_ptr<T1> != offset_ptr<T2>.
261 template<class T1, class T2>
262 inline bool operator!= (const offset_ptr<T1> &pt1,
263 const offset_ptr<T2> &pt2)
264 { return pt1.get() != pt2.get(); }
266 //!offset_ptr<T1> < offset_ptr<T2>.
268 template<class T1, class T2>
269 inline bool operator< (const offset_ptr<T1> &pt1,
270 const offset_ptr<T2> &pt2)
271 { return pt1.get() < pt2.get(); }
273 //!offset_ptr<T1> <= offset_ptr<T2>.
275 template<class T1, class T2>
276 inline bool operator<= (const offset_ptr<T1> &pt1,
277 const offset_ptr<T2> &pt2)
278 { return pt1.get() <= pt2.get(); }
280 //!offset_ptr<T1> > offset_ptr<T2>.
282 template<class T1, class T2>
283 inline bool operator> (const offset_ptr<T1> &pt1,
284 const offset_ptr<T2> &pt2)
285 { return pt1.get() > pt2.get(); }
287 //!offset_ptr<T1> >= offset_ptr<T2>.
289 template<class T1, class T2>
290 inline bool operator>= (const offset_ptr<T1> &pt1,
291 const offset_ptr<T2> &pt2)
292 { return pt1.get() >= pt2.get(); }
296 template<class E, class T, class Y>
297 inline std::basic_ostream<E, T> & operator<<
298 (std::basic_ostream<E, T> & os, offset_ptr<Y> const & p)
299 { return os << p.get_offset(); }
303 template<class E, class T, class Y>
304 inline std::basic_istream<E, T> & operator>>
305 (std::basic_istream<E, T> & is, offset_ptr<Y> & p)
306 { return is >> p.get_offset(); }
308 //!std::ptrdiff_t + offset_ptr
311 inline offset_ptr<T> operator+(std::ptrdiff_t diff, const offset_ptr<T>& right)
312 { return right + diff; }
314 //!offset_ptr - offset_ptr
316 template<class T, class T2>
317 inline std::ptrdiff_t operator- (const offset_ptr<T> &pt, const offset_ptr<T2> &pt2)
318 { return pt.get()- pt2.get(); }
320 //!swap specialization
323 inline void swap (boost::interprocess::offset_ptr<T> &pt,
324 boost::interprocess::offset_ptr<T> &pt2)
326 typename offset_ptr<T>::value_type *ptr = pt.get();
331 //!Simulation of static_cast between pointers. Never throws.
332 template<class T, class U>
333 inline boost::interprocess::offset_ptr<T>
334 static_pointer_cast(const boost::interprocess::offset_ptr<U> & r)
336 return boost::interprocess::offset_ptr<T>
337 (r, boost::interprocess::detail::static_cast_tag());
340 //!Simulation of const_cast between pointers. Never throws.
341 template<class T, class U>
342 inline boost::interprocess::offset_ptr<T>
343 const_pointer_cast(const boost::interprocess::offset_ptr<U> & r)
345 return boost::interprocess::offset_ptr<T>
346 (r, boost::interprocess::detail::const_cast_tag());
349 //!Simulation of dynamic_cast between pointers. Never throws.
350 template<class T, class U>
351 inline boost::interprocess::offset_ptr<T>
352 dynamic_pointer_cast(const boost::interprocess::offset_ptr<U> & r)
354 return boost::interprocess::offset_ptr<T>
355 (r, boost::interprocess::detail::dynamic_cast_tag());
358 //!Simulation of reinterpret_cast between pointers. Never throws.
359 template<class T, class U>
360 inline boost::interprocess::offset_ptr<T>
361 reinterpret_pointer_cast(const boost::interprocess::offset_ptr<U> & r)
363 return boost::interprocess::offset_ptr<T>
364 (r, boost::interprocess::detail::reinterpret_cast_tag());
367 } //namespace interprocess {
371 //!has_trivial_constructor<> == true_type specialization for optimizations
373 struct has_trivial_constructor< boost::interprocess::offset_ptr<T> >
375 enum { value = true };
378 ///has_trivial_destructor<> == true_type specialization for optimizations
380 struct has_trivial_destructor< boost::interprocess::offset_ptr<T> >
382 enum { value = true };
385 //#if !defined(_MSC_VER) || (_MSC_VER >= 1400)
386 namespace interprocess {
388 //!get_pointer() enables boost::mem_fn to recognize offset_ptr.
391 inline T * get_pointer(boost::interprocess::offset_ptr<T> const & p)
393 //#if !defined(_MSC_VER) || (_MSC_VER >= 1400)
394 } //namespace interprocess
398 } //namespace boost {
404 //This is to support embedding a bit in the pointer
405 //for intrusive containers, saving space
406 namespace intrusive {
408 //Predeclaration to avoid including header
409 template<class VoidPointer, std::size_t N>
410 struct max_pointer_plus_bits;
412 template<std::size_t Alignment>
413 struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void>, Alignment>
415 //The offset ptr can embed one bit less than the alignment since it
416 //uses offset == 1 to store the null pointer.
417 static const std::size_t value = ::boost::interprocess::detail::ls_zeros<Alignment>::value - 1;
421 template<class Pointer, std::size_t NumBits>
422 struct pointer_plus_bits;
424 template<class T, std::size_t NumBits>
425 struct pointer_plus_bits<boost::interprocess::offset_ptr<T>, NumBits>
427 typedef boost::interprocess::offset_ptr<T> pointer;
428 //Bits are stored in the lower bits of the pointer except the LSB,
429 //because this bit is used to represent the null pointer.
430 static const std::size_t Mask = ((std::size_t(1) << NumBits)-1)<<1u;
432 static pointer get_pointer(const pointer &n)
433 { return reinterpret_cast<T*>(std::size_t(n.get()) & ~std::size_t(Mask)); }
435 static void set_pointer(pointer &n, pointer p)
437 std::size_t pint = std::size_t(p.get());
438 BOOST_ASSERT(0 == (std::size_t(pint) & Mask));
439 n = reinterpret_cast<T*>(pint | (std::size_t(n.get()) & std::size_t(Mask)));
442 static std::size_t get_bits(const pointer &n)
443 { return(std::size_t(n.get()) & std::size_t(Mask)) >> 1u; }
445 static void set_bits(pointer &n, std::size_t b)
447 BOOST_ASSERT(b < (std::size_t(1) << NumBits));
448 n = reinterpret_cast<T*>(std::size_t(get_pointer(n).get()) | (b << 1u));
452 } //namespace intrusive
456 #include <boost/interprocess/detail/config_end.hpp>
458 #endif //#ifndef BOOST_OFFSET_PTR_HPP