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