summaryrefslogtreecommitdiff
path: root/boost/interprocess/detail/utilities.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/interprocess/detail/utilities.hpp')
-rw-r--r--boost/interprocess/detail/utilities.hpp72
1 files changed, 66 insertions, 6 deletions
diff --git a/boost/interprocess/detail/utilities.hpp b/boost/interprocess/detail/utilities.hpp
index fcb211fb09..351b76cce4 100644
--- a/boost/interprocess/detail/utilities.hpp
+++ b/boost/interprocess/detail/utilities.hpp
@@ -1,6 +1,6 @@
//////////////////////////////////////////////////////////////////////////////
//
-// (C) Copyright Ion Gaztanaga 2005-2011.
+// (C) Copyright Ion Gaztanaga 2005-2012.
// (C) Copyright Gennaro Prota 2003 - 2004.
//
// Distributed under the Boost Software License, Version 1.0.
@@ -14,7 +14,7 @@
#ifndef BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
#define BOOST_INTERPROCESS_DETAIL_UTILITIES_HPP
-#if (defined _MSC_VER) && (_MSC_VER >= 1200)
+#if defined(_MSC_VER)
# pragma once
#endif
@@ -22,7 +22,7 @@
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/interprocess_fwd.hpp>
-#include <boost/move/move.hpp>
+#include <boost/move/utility_core.hpp>
#include <boost/type_traits/has_trivial_destructor.hpp>
#include <boost/interprocess/detail/min_max.hpp>
#include <boost/interprocess/detail/type_traits.hpp>
@@ -30,9 +30,11 @@
#include <boost/interprocess/detail/mpl.hpp>
#include <boost/interprocess/containers/version_type.hpp>
#include <boost/intrusive/pointer_traits.hpp>
-#include <boost/move/move.hpp>
+#include <boost/move/utility_core.hpp>
+#include <boost/static_assert.hpp>
#include <utility>
#include <algorithm>
+#include <climits>
namespace boost {
namespace interprocess {
@@ -86,7 +88,10 @@ inline SizeType get_truncated_size_po2(SizeType orig_size, SizeType multiple)
template <std::size_t OrigSize, std::size_t RoundTo>
struct ct_rounded_size
{
- static const std::size_t value = ((OrigSize-1)/RoundTo+1)*RoundTo;
+ BOOST_STATIC_ASSERT((RoundTo != 0));
+ static const std::size_t intermediate_value = (OrigSize-1)/RoundTo+1;
+ BOOST_STATIC_ASSERT(intermediate_value <= std::size_t(-1)/RoundTo);
+ static const std::size_t value = intermediate_value*RoundTo;
};
// Gennaro Prota wrote this. Thanks!
@@ -133,6 +138,61 @@ addressof(T& v)
&const_cast<char&>(reinterpret_cast<const volatile char &>(v)));
}
+template<class SizeType>
+struct sqrt_size_type_max
+{
+ static const SizeType value = (SizeType(1) << (sizeof(SizeType)*(CHAR_BIT/2)))-1;
+};
+
+template<class SizeType>
+inline bool multiplication_overflows(SizeType a, SizeType b)
+{
+ const SizeType sqrt_size_max = sqrt_size_type_max<SizeType>::value;
+ return //Fast runtime check
+ ( (a | b) > sqrt_size_max &&
+ //Slow division check
+ b && a > SizeType(-1)/b
+ );
+}
+
+template<std::size_t SztSizeOfType, class SizeType>
+inline bool size_overflows(SizeType count)
+{
+ //Compile time-check
+ BOOST_STATIC_ASSERT(SztSizeOfType <= SizeType(-1));
+ //Runtime check
+ return multiplication_overflows(SizeType(SztSizeOfType), count);
+}
+
+template<class RawPointer>
+class pointer_size_t_caster
+{
+ public:
+ BOOST_STATIC_ASSERT(sizeof(std::size_t) == sizeof(void*));
+
+ explicit pointer_size_t_caster(std::size_t sz)
+ : m_ptr(reinterpret_cast<RawPointer>(sz))
+ {}
+
+ explicit pointer_size_t_caster(RawPointer p)
+ : m_ptr(p)
+ {}
+
+ std::size_t size() const
+ { return reinterpret_cast<std::size_t>(m_ptr); }
+
+ RawPointer pointer() const
+ { return m_ptr; }
+
+ private:
+ RawPointer m_ptr;
+};
+
+
+template<class SizeType>
+inline bool sum_overflows(SizeType a, SizeType b)
+{ return SizeType(-1) - a < b; }
+
//Anti-exception node eraser
template<class Cont>
class value_eraser
@@ -140,7 +200,7 @@ class value_eraser
public:
value_eraser(Cont & cont, typename Cont::iterator it)
: m_cont(cont), m_index_it(it), m_erase(true){}
- ~value_eraser()
+ ~value_eraser()
{ if(m_erase) m_cont.erase(m_index_it); }
void release() { m_erase = false; }