Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / detail / utilities.hpp
1 //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2005-2012.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
5 //
6 // Distributed under the Boost Software License, Version 1.0.
7 // (See accompanying file LICENSE_1_0.txt or copy at
8 // http://www.boost.org/LICENSE_1_0.txt)
9 //
10 // See http://www.boost.org/libs/interprocess for documentation.
11 //
12 //////////////////////////////////////////////////////////////////////////////
13
14 #ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
15 #define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
16
17 #if defined(_MSC_VER)
18 #  pragma once
19 #endif
20
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
23
24 #include <boost/interprocess/interprocess_fwd.hpp>
25 #include <boost/move/utility_core.hpp>
26 #include <boost/type_traits/has_trivial_destructor.hpp>
27 #include <boost/interprocess/detail/min_max.hpp>
28 #include <boost/interprocess/detail/type_traits.hpp>
29 #include <boost/interprocess/detail/transform_iterator.hpp>
30 #include <boost/interprocess/detail/mpl.hpp>
31 #include <boost/interprocess/containers/version_type.hpp>
32 #include <boost/intrusive/pointer_traits.hpp>
33 #include <boost/move/utility_core.hpp>
34 #include <boost/static_assert.hpp>
35 #include <utility>
36 #include <algorithm>
37 #include <climits>
38
39 namespace boost {
40 namespace interprocess {
41 namespace ipcdetail {
42
43 template <class T>
44 inline T* to_raw_pointer(T* p)
45 {  return p; }
46
47 template <class Pointer>
48 inline typename boost::intrusive::pointer_traits<Pointer>::element_type*
49 to_raw_pointer(const Pointer &p)
50 {  return boost::interprocess::ipcdetail::to_raw_pointer(p.operator->());  }
51
52 //!To avoid ADL problems with swap
53 template <class T>
54 inline void do_swap(T& x, T& y)
55 {
56    using std::swap;
57    swap(x, y);
58 }
59
60 //Rounds "orig_size" by excess to round_to bytes
61 template<class SizeType>
62 inline SizeType get_rounded_size(SizeType orig_size, SizeType round_to)
63 {
64    return ((orig_size-1)/round_to+1)*round_to;
65 }
66
67 //Truncates "orig_size" to a multiple of "multiple" bytes.
68 template<class SizeType>
69 inline SizeType get_truncated_size(SizeType orig_size, SizeType multiple)
70 {
71    return orig_size/multiple*multiple;
72 }
73
74 //Rounds "orig_size" by excess to round_to bytes. round_to must be power of two
75 template<class SizeType>
76 inline SizeType get_rounded_size_po2(SizeType orig_size, SizeType round_to)
77 {
78    return ((orig_size-1)&(~(round_to-1))) + round_to;
79 }
80
81 //Truncates "orig_size" to a multiple of "multiple" bytes. multiple must be power of two
82 template<class SizeType>
83 inline SizeType get_truncated_size_po2(SizeType orig_size, SizeType multiple)
84 {
85    return (orig_size & (~(multiple-1)));
86 }
87
88 template <std::size_t OrigSize, std::size_t RoundTo>
89 struct ct_rounded_size
90 {
91    BOOST_STATIC_ASSERT((RoundTo != 0));
92    static const std::size_t intermediate_value = (OrigSize-1)/RoundTo+1;
93    BOOST_STATIC_ASSERT(intermediate_value <= std::size_t(-1)/RoundTo);
94    static const std::size_t value = intermediate_value*RoundTo;
95 };
96
97 // Gennaro Prota wrote this. Thanks!
98 template <int p, int n = 4>
99 struct ct_max_pow2_less
100 {
101    static const std::size_t c = 2*n < p;
102
103    static const std::size_t value =
104          c ? (ct_max_pow2_less< c*p, 2*c*n>::value) : n;
105 };
106
107 template <>
108 struct ct_max_pow2_less<0, 0>
109 {
110    static const std::size_t value = 0;
111 };
112
113 }  //namespace ipcdetail {
114
115 //!Trait class to detect if an index is a node
116 //!index. This allows more efficient operations
117 //!when deallocating named objects.
118 template <class Index>
119 struct is_node_index
120 {
121    static const bool value = false;
122 };
123
124 //!Trait class to detect if an index is an intrusive
125 //!index. This will embed the derivation hook in each
126 //!allocation header, to provide memory for the intrusive
127 //!container.
128 template <class Index>
129 struct is_intrusive_index
130 {
131    static const bool value = false;
132 };
133
134 template <typename T> T*
135 addressof(T& v)
136 {
137   return reinterpret_cast<T*>(
138        &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
139 }
140
141 template<class SizeType>
142 struct sqrt_size_type_max
143 {
144    static const SizeType value = (SizeType(1) << (sizeof(SizeType)*(CHAR_BIT/2)))-1;
145 };
146
147 template<class SizeType>
148 inline bool multiplication_overflows(SizeType a, SizeType b)
149 {
150    const SizeType sqrt_size_max = sqrt_size_type_max<SizeType>::value;
151    return   //Fast runtime check
152          (  (a | b) > sqrt_size_max &&
153             //Slow division check
154             b && a > SizeType(-1)/b
155          );
156 }
157
158 template<std::size_t SztSizeOfType, class SizeType>
159 inline bool size_overflows(SizeType count)
160 {
161    //Compile time-check
162    BOOST_STATIC_ASSERT(SztSizeOfType <= SizeType(-1));
163    //Runtime check
164    return multiplication_overflows(SizeType(SztSizeOfType), count);
165 }
166
167 template<class RawPointer>
168 class pointer_size_t_caster
169 {
170    public:
171    BOOST_STATIC_ASSERT(sizeof(std::size_t) == sizeof(void*));
172
173    explicit pointer_size_t_caster(std::size_t sz)
174       : m_ptr(reinterpret_cast<RawPointer>(sz))
175    {}
176
177    explicit pointer_size_t_caster(RawPointer p)
178       : m_ptr(p)
179    {}
180
181    std::size_t size() const
182    {   return reinterpret_cast<std::size_t>(m_ptr);   }
183
184    RawPointer pointer() const
185    {   return m_ptr;   }
186
187    private:
188    RawPointer m_ptr;
189 };
190
191
192 template<class SizeType>
193 inline bool sum_overflows(SizeType a, SizeType b)
194 {  return SizeType(-1) - a < b;  }
195
196 //Anti-exception node eraser
197 template<class Cont>
198 class value_eraser
199 {
200    public:
201    value_eraser(Cont & cont, typename Cont::iterator it)
202       : m_cont(cont), m_index_it(it), m_erase(true){}
203    ~value_eraser()
204    {  if(m_erase) m_cont.erase(m_index_it);  }
205
206    void release() {  m_erase = false;  }
207
208    private:
209    Cont                   &m_cont;
210    typename Cont::iterator m_index_it;
211    bool                    m_erase;
212 };
213
214 }  //namespace interprocess {
215 }  //namespace boost {
216
217 #include <boost/interprocess/detail/config_end.hpp>
218
219 #endif   //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
220