Apply patch for [CVE-2012-2677][boost] ordered_malloc() overflow
[external/boost.git] / boost / interprocess / offset_ptr.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
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)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_OFFSET_PTR_HPP
12 #define BOOST_OFFSET_PTR_HPP
13
14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20
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>
28 #include <ostream>
29 #include <istream>
30 #include <iterator>
31
32 //!\file
33 //!Describes a smart pointer that stores the offset between this pointer and
34 //!target pointee, called offset_ptr.
35
36 namespace boost {
37
38 //Predeclarations
39 template <class T>
40 struct has_trivial_constructor;
41
42 template <class T>
43 struct has_trivial_destructor;
44
45 namespace interprocess {
46
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>
54 class offset_ptr
55 {
56    /// @cond
57    typedef offset_ptr<PointedType>           self_t;
58
59    void unspecified_bool_type_func() const {}
60    typedef void (self_t::*unspecified_bool_type)() const;
61
62    #if defined(_MSC_VER) && (_MSC_VER >= 1400)
63    __declspec(noinline) //this workaround is needed for msvc > 8.0
64    #endif
65    void set_offset(const PointedType *ptr)
66    {
67       //offset == 1 && ptr != 0 is not legal for this pointer
68       if(!ptr){
69          internal.m_offset = 1;
70       }
71       else{
72          internal.m_offset = (const char*)ptr - (const char*)(this);
73          BOOST_ASSERT(internal.m_offset != 1);
74       }
75    }
76
77    #if defined(_MSC_VER) && (_MSC_VER >= 1400)
78    __declspec(noinline) //this workaround is needed for msvc-8.0 and msvc-9.0
79    #endif
80    void* get_pointer() const
81    {  return (internal.m_offset == 1) ? 0 : (const_cast<char*>(reinterpret_cast<const char*>(this)) + internal.m_offset); }
82
83    void inc_offset(std::ptrdiff_t bytes)
84    {  internal.m_offset += bytes;   }
85
86    void dec_offset(std::ptrdiff_t bytes)
87    {  internal.m_offset -= bytes;   }
88
89    union internal_type{
90       std::ptrdiff_t m_offset; //Distance between this object and pointed address
91       PointedType *aliasing_helper;
92    } internal;
93    /// @endcond
94
95    public:
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;
102
103    public:   //Public Functions
104
105    //!Constructor from raw pointer (allows "0" pointer conversion).
106    //!Never throws.
107    offset_ptr(pointer ptr = 0) {  this->set_offset(ptr); }
108
109    //!Constructor from other pointer.
110    //!Never throws.
111    template <class T>
112    offset_ptr(T *ptr) 
113    {  pointer p (ptr);  (void)p; this->set_offset(p); }
114
115    //!Constructor from other offset_ptr
116    //!Never throws.
117    offset_ptr(const offset_ptr& ptr) 
118    {  this->set_offset(ptr.get());   }
119
120    //!Constructor from other offset_ptr. If pointers of pointee types are 
121    //!convertible, offset_ptrs will be convertibles. Never throws.
122    template<class T2>
123    offset_ptr(const offset_ptr<T2> &ptr) 
124    {  pointer p(ptr.get());  (void)p; this->set_offset(p);   }
125
126    //!Emulates static_cast operator.
127    //!Never throws.
128    template<class Y>
129    offset_ptr(const offset_ptr<Y> & r, detail::static_cast_tag)
130    {  this->set_offset(static_cast<PointedType*>(r.get()));   }
131
132    //!Emulates const_cast operator.
133    //!Never throws.
134    template<class Y>
135    offset_ptr(const offset_ptr<Y> & r, detail::const_cast_tag)
136    {  this->set_offset(const_cast<PointedType*>(r.get()));   }
137
138    //!Emulates dynamic_cast operator.
139    //!Never throws.
140    template<class Y>
141    offset_ptr(const offset_ptr<Y> & r, detail::dynamic_cast_tag)
142    {  this->set_offset(dynamic_cast<PointedType*>(r.get()));   }
143
144    //!Emulates reinterpret_cast operator.
145    //!Never throws.
146    template<class Y>
147    offset_ptr(const offset_ptr<Y> & r, detail::reinterpret_cast_tag)
148    {  this->set_offset(reinterpret_cast<PointedType*>(r.get()));   }
149
150    //!Obtains raw pointer from offset.
151    //!Never throws.
152    pointer get()const
153    {  return static_cast<pointer>(this->get_pointer());   }
154
155    std::ptrdiff_t get_offset() const
156    {  return internal.m_offset;  }
157
158    //!Pointer-like -> operator. It can return 0 pointer.
159    //!Never throws.
160    pointer operator->() const           
161    {  return this->get(); }
162
163    //!Dereferencing operator, if it is a null offset_ptr behavior 
164    //!   is undefined. Never throws.
165    reference operator* () const           
166    {
167       pointer p = this->get();
168       reference r = *p;
169       return r;
170    }
171
172    //!Indexing operator.
173    //!Never throws.
174    reference operator[](std::ptrdiff_t idx) const   
175    {  return this->get()[idx];  }
176
177    //!Assignment from pointer (saves extra conversion).
178    //!Never throws.
179    offset_ptr& operator= (pointer from)
180    {  this->set_offset(from); return *this;  }
181
182    //!Assignment from other offset_ptr.
183    //!Never throws.
184    offset_ptr& operator= (const offset_ptr & pt)
185    {  pointer p(pt.get());  (void)p; this->set_offset(p);  return *this;  }
186
187    //!Assignment from related offset_ptr. If pointers of pointee types 
188    //!   are assignable, offset_ptrs will be assignable. Never throws.
189    template <class T2>
190    offset_ptr& operator= (const offset_ptr<T2> & pt)
191    {  pointer p(pt.get()); this->set_offset(p);  return *this;  }
192  
193    //!offset_ptr + std::ptrdiff_t.
194    //!Never throws.
195    offset_ptr operator+ (std::ptrdiff_t offset) const   
196    {  return offset_ptr(this->get()+offset);   }
197
198    //!offset_ptr - std::ptrdiff_t.
199    //!Never throws.
200    offset_ptr operator- (std::ptrdiff_t offset) const   
201    {  return offset_ptr(this->get()-offset);   }
202
203    //!offset_ptr += std::ptrdiff_t.
204    //!Never throws.
205    offset_ptr &operator+= (std::ptrdiff_t offset)
206    {  this->inc_offset(offset * sizeof (PointedType));   return *this;  }
207
208    //!offset_ptr -= std::ptrdiff_t.
209    //!Never throws.
210    offset_ptr &operator-= (std::ptrdiff_t offset)
211    {  this->dec_offset(offset * sizeof (PointedType));   return *this;  }
212
213    //!++offset_ptr.
214    //!Never throws.
215    offset_ptr& operator++ (void) 
216    {  this->inc_offset(sizeof (PointedType));   return *this;  }
217
218    //!offset_ptr++.
219    //!Never throws.
220    offset_ptr operator++ (int)
221    {  offset_ptr temp(*this); ++*this; return temp; }
222
223    //!--offset_ptr.
224    //!Never throws.
225    offset_ptr& operator-- (void) 
226    {  this->dec_offset(sizeof (PointedType));   return *this;  }
227
228    //!offset_ptr--.
229    //!Never throws.
230    offset_ptr operator-- (int)
231    {  offset_ptr temp(*this); --*this; return temp; }
232
233    //!safe bool conversion operator.
234    //!Never throws.
235    operator unspecified_bool_type() const  
236    {  return this->get()? &self_t::unspecified_bool_type_func : 0;   }
237
238    //!Not operator. Not needed in theory, but improves portability. 
239    //!Never throws
240    bool operator! () const
241    {  return this->get() == 0;   }
242 /*
243    friend void swap (offset_ptr &pt, offset_ptr &pt2)
244    {  
245       value_type *ptr = pt.get();
246       pt = pt2;
247       pt2 = ptr;
248    }
249 */
250 };
251
252 //!offset_ptr<T1> == offset_ptr<T2>.
253 //!Never throws.
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();  }
258
259 //!offset_ptr<T1> != offset_ptr<T2>.
260 //!Never throws.
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();  }
265
266 //!offset_ptr<T1> < offset_ptr<T2>.
267 //!Never throws.
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();  }
272
273 //!offset_ptr<T1> <= offset_ptr<T2>.
274 //!Never throws.
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();  }
279
280 //!offset_ptr<T1> > offset_ptr<T2>.
281 //!Never throws.
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();  }
286
287 //!offset_ptr<T1> >= offset_ptr<T2>.
288 //!Never throws.
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();  }
293
294 //!operator<<
295 //!for offset ptr
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();   }
300
301 //!operator>> 
302 //!for offset ptr
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();  }
307
308 //!std::ptrdiff_t + offset_ptr
309 //!operation
310 template<class T>
311 inline offset_ptr<T> operator+(std::ptrdiff_t diff, const offset_ptr<T>& right)
312 {  return right + diff;  }
313
314 //!offset_ptr - offset_ptr
315 //!operation
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();   }
319
320 //!swap specialization 
321 //!for offset_ptr
322 template<class T>
323 inline void swap (boost::interprocess::offset_ptr<T> &pt, 
324                   boost::interprocess::offset_ptr<T> &pt2)
325 {  
326    typename offset_ptr<T>::value_type *ptr = pt.get();
327    pt = pt2;
328    pt2 = ptr;
329 }
330
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)
335 {  
336    return boost::interprocess::offset_ptr<T>
337             (r, boost::interprocess::detail::static_cast_tag());  
338 }
339
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)
344 {  
345    return boost::interprocess::offset_ptr<T>
346             (r, boost::interprocess::detail::const_cast_tag());  
347 }
348
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)
353 {  
354    return boost::interprocess::offset_ptr<T>
355             (r, boost::interprocess::detail::dynamic_cast_tag());  
356 }
357
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)
362 {  
363    return boost::interprocess::offset_ptr<T>
364             (r, boost::interprocess::detail::reinterpret_cast_tag());  
365 }
366
367 }  //namespace interprocess {
368
369 /// @cond
370
371 //!has_trivial_constructor<> == true_type specialization for optimizations
372 template <class T>
373 struct has_trivial_constructor< boost::interprocess::offset_ptr<T> > 
374 {
375    enum { value = true };
376 };
377
378 ///has_trivial_destructor<> == true_type specialization for optimizations
379 template <class T>
380 struct has_trivial_destructor< boost::interprocess::offset_ptr<T> > 
381 {
382    enum { value = true };
383 };
384
385 //#if !defined(_MSC_VER) || (_MSC_VER >= 1400)
386 namespace interprocess {
387 //#endif
388 //!get_pointer() enables boost::mem_fn to recognize offset_ptr. 
389 //!Never throws.
390 template<class T>
391 inline T * get_pointer(boost::interprocess::offset_ptr<T> const & p)
392 {  return p.get();   }
393 //#if !defined(_MSC_VER) || (_MSC_VER >= 1400)
394 }  //namespace interprocess
395 //#endif
396
397 /// @endcond
398 }  //namespace boost {
399
400 /// @cond
401
402 namespace boost{
403
404 //This is to support embedding a bit in the pointer
405 //for intrusive containers, saving space
406 namespace intrusive {
407
408 //Predeclaration to avoid including header
409 template<class VoidPointer, std::size_t N>
410 struct max_pointer_plus_bits;
411
412 template<std::size_t Alignment>
413 struct max_pointer_plus_bits<boost::interprocess::offset_ptr<void>, Alignment>
414 {
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;
418 };
419
420 //Predeclaration
421 template<class Pointer, std::size_t NumBits>
422 struct pointer_plus_bits;
423
424 template<class T, std::size_t NumBits>
425 struct pointer_plus_bits<boost::interprocess::offset_ptr<T>, NumBits>
426 {
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; 
431
432    static pointer get_pointer(const pointer &n)
433    {  return reinterpret_cast<T*>(std::size_t(n.get()) & ~std::size_t(Mask));  }
434
435    static void set_pointer(pointer &n, pointer p)
436    {
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)));
440    }
441
442    static std::size_t get_bits(const pointer &n)
443    {  return(std::size_t(n.get()) & std::size_t(Mask)) >> 1u;  }
444
445    static void set_bits(pointer &n, std::size_t b)
446    {
447       BOOST_ASSERT(b < (std::size_t(1) << NumBits));
448       n = reinterpret_cast<T*>(std::size_t(get_pointer(n).get()) | (b << 1u));
449    }
450 };
451
452 }  //namespace intrusive
453 }  //namespace boost{
454 /// @endcond
455
456 #include <boost/interprocess/detail/config_end.hpp>
457
458 #endif //#ifndef BOOST_OFFSET_PTR_HPP
459