comparison DEPENDENCIES/generic/include/boost/interprocess/detail/utilities.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
comparison
equal deleted inserted replaced
15:663ca0da4350 16:2665513ce2d3
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) && (_MSC_VER >= 1200)
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/move.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/move.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 explicit pointer_size_t_caster(std::size_t sz)
172 : m_ptr(reinterpret_cast<RawPointer>(sz))
173 {}
174
175 explicit pointer_size_t_caster(RawPointer p)
176 : m_ptr(p)
177 {}
178
179 std::size_t size() const
180 { return reinterpret_cast<std::size_t>(m_ptr); }
181
182 RawPointer pointer() const
183 { return m_ptr; }
184
185 private:
186 RawPointer m_ptr;
187 };
188
189
190 template<class SizeType>
191 inline bool sum_overflows(SizeType a, SizeType b)
192 { return SizeType(-1) - a < b; }
193
194 //Anti-exception node eraser
195 template<class Cont>
196 class value_eraser
197 {
198 public:
199 value_eraser(Cont & cont, typename Cont::iterator it)
200 : m_cont(cont), m_index_it(it), m_erase(true){}
201 ~value_eraser()
202 { if(m_erase) m_cont.erase(m_index_it); }
203
204 void release() { m_erase = false; }
205
206 private:
207 Cont &m_cont;
208 typename Cont::iterator m_index_it;
209 bool m_erase;
210 };
211
212 } //namespace interprocess {
213 } //namespace boost {
214
215 #include <boost/interprocess/detail/config_end.hpp>
216
217 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
218