1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012.
4 // (C) Copyright Gennaro Prota 2003 - 2004.
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)
10 // See http://www.boost.org/libs/interprocess for documentation.
12 //////////////////////////////////////////////////////////////////////////////
14 #ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
15 #define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
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>
40 namespace interprocess {
44 inline T* to_raw_pointer(T* p)
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->()); }
52 //!To avoid ADL problems with swap
54 inline void do_swap(T& x, T& y)
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)
64 return ((orig_size-1)/round_to+1)*round_to;
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)
71 return orig_size/multiple*multiple;
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)
78 return ((orig_size-1)&(~(round_to-1))) + round_to;
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)
85 return (orig_size & (~(multiple-1)));
88 template <std::size_t OrigSize, std::size_t RoundTo>
89 struct ct_rounded_size
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;
97 // Gennaro Prota wrote this. Thanks!
98 template <int p, int n = 4>
99 struct ct_max_pow2_less
101 static const std::size_t c = 2*n < p;
103 static const std::size_t value =
104 c ? (ct_max_pow2_less< c*p, 2*c*n>::value) : n;
108 struct ct_max_pow2_less<0, 0>
110 static const std::size_t value = 0;
113 } //namespace ipcdetail {
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>
121 static const bool value = false;
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
128 template <class Index>
129 struct is_intrusive_index
131 static const bool value = false;
134 template <typename T> T*
137 return reinterpret_cast<T*>(
138 &const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
141 template<class SizeType>
142 struct sqrt_size_type_max
144 static const SizeType value = (SizeType(1) << (sizeof(SizeType)*(CHAR_BIT/2)))-1;
147 template<class SizeType>
148 inline bool multiplication_overflows(SizeType a, SizeType b)
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
158 template<std::size_t SztSizeOfType, class SizeType>
159 inline bool size_overflows(SizeType count)
162 BOOST_STATIC_ASSERT(SztSizeOfType <= SizeType(-1));
164 return multiplication_overflows(SizeType(SztSizeOfType), count);
167 template<class RawPointer>
168 class pointer_size_t_caster
171 BOOST_STATIC_ASSERT(sizeof(std::size_t) == sizeof(void*));
173 explicit pointer_size_t_caster(std::size_t sz)
174 : m_ptr(reinterpret_cast<RawPointer>(sz))
177 explicit pointer_size_t_caster(RawPointer p)
181 std::size_t size() const
182 { return reinterpret_cast<std::size_t>(m_ptr); }
184 RawPointer pointer() const
192 template<class SizeType>
193 inline bool sum_overflows(SizeType a, SizeType b)
194 { return SizeType(-1) - a < b; }
196 //Anti-exception node eraser
201 value_eraser(Cont & cont, typename Cont::iterator it)
202 : m_cont(cont), m_index_it(it), m_erase(true){}
204 { if(m_erase) m_cont.erase(m_index_it); }
206 void release() { m_erase = false; }
210 typename Cont::iterator m_index_it;
214 } //namespace interprocess {
215 } //namespace boost {
217 #include <boost/interprocess/detail/config_end.hpp>
219 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP