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