1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2008.
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
17 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
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/interprocess/detail/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/interprocess/detail/move.hpp>
37 namespace interprocess
{
40 template<class SmartPtr
>
43 typedef typename
SmartPtr::value_type value_type
;
44 typedef value_type
*pointer
;
45 static pointer
get (const SmartPtr
&smartptr
)
46 { return smartptr
.get();}
50 struct smart_ptr_type
<T
*>
53 typedef value_type
*pointer
;
54 static pointer
get (pointer ptr
)
58 //!Overload for smart pointers to avoid ADL problems with get_pointer
60 inline typename smart_ptr_type
<Ptr
>::pointer
61 get_pointer(const Ptr
&ptr
)
62 { return smart_ptr_type
<Ptr
>::get(ptr
); }
64 //!To avoid ADL problems with swap
66 inline void do_swap(T
& x
, T
& y
)
72 //Rounds "orig_size" by excess to round_to bytes
73 inline std::size_t get_rounded_size(std::size_t orig_size
, std::size_t round_to
)
75 return ((orig_size
-1)/round_to
+1)*round_to
;
78 //Truncates "orig_size" to a multiple of "multiple" bytes.
79 inline std::size_t get_truncated_size(std::size_t orig_size
, std::size_t multiple
)
81 return orig_size
/multiple
*multiple
;
84 //Rounds "orig_size" by excess to round_to bytes. round_to must be power of two
85 inline std::size_t get_rounded_size_po2(std::size_t orig_size
, std::size_t round_to
)
87 return ((orig_size
-1)&(~(round_to
-1))) + round_to
;
90 //Truncates "orig_size" to a multiple of "multiple" bytes. multiple must be power of two
91 inline std::size_t get_truncated_size_po2(std::size_t orig_size
, std::size_t multiple
)
93 return (orig_size
& (~(multiple
-1)));
96 template <std::size_t OrigSize
, std::size_t RoundTo
>
97 struct ct_rounded_size
99 enum { value
= ((OrigSize
-1)/RoundTo
+1)*RoundTo
};
102 // Gennaro Prota wrote this. Thanks!
103 template <int p
, int n
= 4>
104 struct ct_max_pow2_less
106 enum { c
= 2*n
< p
};
108 static const std::size_t value
=
109 c
? (ct_max_pow2_less
< c
*p
, 2*c
*n
>::value
) : n
;
113 struct ct_max_pow2_less
<0, 0>
115 static const std::size_t value
= 0;
118 } //namespace detail {
120 //!Trait class to detect if an index is a node
121 //!index. This allows more efficient operations
122 //!when deallocating named objects.
123 template <class Index
>
126 enum { value
= false };
129 //!Trait class to detect if an index is an intrusive
130 //!index. This will embed the derivation hook in each
131 //!allocation header, to provide memory for the intrusive
133 template <class Index
>
134 struct is_intrusive_index
136 enum { value
= false };
139 template <typename T
> T
*
142 return reinterpret_cast<T
*>(
143 &const_cast<char&>(reinterpret_cast<const volatile char &>(v
)));
146 //Anti-exception node eraser
151 value_eraser(Cont
& cont
, typename
Cont::iterator it
)
152 : m_cont(cont
), m_index_it(it
), m_erase(true){}
154 { if(m_erase
) m_cont
.erase(m_index_it
); }
156 void release() { m_erase
= false; }
160 typename
Cont::iterator m_index_it
;
164 } //namespace interprocess {
165 } //namespace boost {
167 #include <boost/interprocess/detail/config_end.hpp>
169 #endif //#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP