|
Boost-Commit : |
Subject: [Boost-commit] svn:boost r81858 - in trunk: boost/smart_ptr libs/smart_ptr libs/smart_ptr/test
From: glenfe_at_[hidden]
Date: 2012-12-11 12:42:49
Author: glenfe
Date: 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
New Revision: 81858
URL: http://svn.boost.org/trac/boost/changeset/81858
Log:
Add overloads of allocate_shared_noinit to complement make_shared_noinit
Text files modified:
trunk/boost/smart_ptr/allocate_shared_array.hpp | 36 +++++++++++++++++++
trunk/libs/smart_ptr/make_shared_array.html | 14 ++++++-
trunk/libs/smart_ptr/test/allocate_shared_array_esft_test.cpp | 10 +++++
trunk/libs/smart_ptr/test/allocate_shared_array_test.cpp | 74 ++++++++++++++++++++++++++++++++++++++++
trunk/libs/smart_ptr/test/allocate_shared_array_throws_test.cpp | 14 +++++++
trunk/libs/smart_ptr/test/allocate_shared_arrays_test.cpp | 56 ++++++++++++++++++++++++++++++
6 files changed, 202 insertions(+), 2 deletions(-)
Modified: trunk/boost/smart_ptr/allocate_shared_array.hpp
==============================================================================
--- trunk/boost/smart_ptr/allocate_shared_array.hpp (original)
+++ trunk/boost/smart_ptr/allocate_shared_array.hpp 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -209,6 +209,42 @@
}
#endif
#endif
+ template<typename T, typename A>
+ inline typename boost::detail::sp_if_array<T>::type
+ allocate_shared_noinit(const A& allocator, std::size_t size) {
+ typedef typename boost::detail::array_inner<T>::type T1;
+ typedef typename boost::detail::array_base<T1>::type T2;
+ T1* p1 = 0;
+ T2* p2 = 0;
+ std::size_t n1 = size * boost::detail::array_total<T1>::size;
+ boost::detail::allocate_array_helper<A, T2[]> a1(allocator, n1, &p2);
+ boost::detail::array_deleter<T2[]> d1(n1);
+ boost::shared_ptr<T> s1(p1, d1, a1);
+ boost::detail::array_deleter<T2[]>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<boost::detail::array_deleter<T2[]> >(s1);
+ d2->construct_noinit(p2);
+ return boost::shared_ptr<T>(s1, p1);
+ }
+ template<typename T, typename A>
+ inline typename boost::detail::sp_if_size_array<T>::type
+ allocate_shared_noinit(const A& allocator) {
+ typedef typename boost::detail::array_inner<T>::type T1;
+ typedef typename boost::detail::array_base<T1>::type T2;
+ enum {
+ N = boost::detail::array_total<T>::size
+ };
+ T1* p1 = 0;
+ T2* p2 = 0;
+ boost::detail::allocate_array_helper<A, T2[N]> a1(allocator, &p2);
+ boost::detail::array_deleter<T2[N]> d1;
+ boost::shared_ptr<T> s1(p1, d1, a1);
+ boost::detail::array_deleter<T2[N]>* d2;
+ p1 = reinterpret_cast<T1*>(p2);
+ d2 = get_deleter<boost::detail::array_deleter<T2[N]> >(s1);
+ d2->construct_noinit(p2);
+ return boost::shared_ptr<T>(s1, p1);
+ }
}
#endif
Modified: trunk/libs/smart_ptr/make_shared_array.html
==============================================================================
--- trunk/libs/smart_ptr/make_shared_array.html (original)
+++ trunk/libs/smart_ptr/make_shared_array.html 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -96,6 +96,12 @@
template<typename T>
shared_ptr<T[N]> make_shared_noinit();
+
+ template<typename T, typename A>
+ shared_ptr<T[]> allocate_shared_noinit(const A& allocator, size_t size);
+
+ template<typename T, typename A>
+ shared_ptr<T[N]> allocate_shared_noinit(const A& allocator);
}</pre>
<h2><a name="functions">Free Functions</a></h2>
<pre>template<typename T, typename... Args>
@@ -197,13 +203,17 @@
fixed size array.</p>
</blockquote>
<pre>template<typename T>
- shared_ptr<T[]> make_shared_noinit(size_t size);</pre>
+ shared_ptr<T[]> make_shared_noinit(size_t size);
+template<typename T, typename A>
+ shared_ptr<T[]> make_shared_noinit(const A& allocator, size_t size);</pre>
<blockquote>
<p><b>Description:</b> This overload does not perform value
initialization of elements.</p>
</blockquote>
<pre>template<typename T>
- shared_ptr<T[N]> make_shared_noinit();</pre>
+ shared_ptr<T[N]> make_shared_noinit();
+template<typename T, typename A>
+ shared_ptr<T[N]> make_shared_noinit(const A& allocator);</pre>
<blockquote>
<p><b>Description:</b> This overload of the utility above is used for a
fixed size array.</p>
Modified: trunk/libs/smart_ptr/test/allocate_shared_array_esft_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_array_esft_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_array_esft_test.cpp 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -38,5 +38,15 @@
BOOST_TEST(type::instances == 3);
}
}
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<type[]> a1 = boost::allocate_shared_noinit<type[]>(std::allocator<type>(), 3);
+ try {
+ a1[0].shared_from_this();
+ BOOST_ERROR("shared_from_this did not throw");
+ } catch (...) {
+ BOOST_TEST(type::instances == 3);
+ }
+ }
return boost::report_errors();
}
Modified: trunk/libs/smart_ptr/test/allocate_shared_array_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_array_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_array_test.cpp 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -121,5 +121,79 @@
BOOST_TEST(type::instances == 0);
}
#endif
+ {
+ boost::shared_ptr<int[]> a1 = boost::allocate_shared_noinit<int[]>(std::allocator<int>(), 3);
+ int* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
+ }
+ {
+ boost::shared_ptr<int[3]> a1 = boost::allocate_shared_noinit<int[3]>(std::allocator<int>());
+ int* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
+ }
+ {
+ boost::shared_ptr<const int[]> a1 = boost::allocate_shared_noinit<const int[]>(std::allocator<int>(), 3);
+ const int* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
+ }
+ {
+ boost::shared_ptr<const int[3]> a1 = boost::allocate_shared_noinit<const int[3]>(std::allocator<int>());
+ const int* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<int>::value == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<type[]> a1 = boost::allocate_shared_noinit<type[]>(std::allocator<type>(), 3);
+ type* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
+ BOOST_TEST(type::instances == 3);
+ boost::weak_ptr<type[]> w1 = a1;
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<type[3]> a1 = boost::allocate_shared_noinit<type[3]>(std::allocator<type>());
+ type* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
+ BOOST_TEST(type::instances == 3);
+ boost::weak_ptr<type[3]> w1 = a1;
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<const type[]> a1 = boost::allocate_shared_noinit<const type[]>(std::allocator<type>(), 3);
+ const type* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
+ BOOST_TEST(type::instances == 3);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<const type[3]> a1 = boost::allocate_shared_noinit<const type[3]>(std::allocator<type>());
+ const type* a2 = a1.get();
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(a2 != 0);
+ BOOST_TEST(size_t(a2) % boost::alignment_of<type>::value == 0);
+ BOOST_TEST(type::instances == 3);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
return boost::report_errors();
}
Modified: trunk/libs/smart_ptr/test/allocate_shared_array_throws_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_array_throws_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_array_throws_test.cpp 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -59,5 +59,19 @@
BOOST_TEST(type::instances == 0);
}
#endif
+ BOOST_TEST(type::instances == 0);
+ try {
+ boost::allocate_shared_noinit<type[]>(std::allocator<type>(), 6);
+ BOOST_ERROR("allocate_shared_noinit did not throw");
+ } catch (...) {
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ try {
+ boost::allocate_shared_noinit<type[][2]>(std::allocator<type>(), 3);
+ BOOST_ERROR("allocate_shared_noinit did not throw");
+ } catch (...) {
+ BOOST_TEST(type::instances == 0);
+ }
return boost::report_errors();
}
Modified: trunk/libs/smart_ptr/test/allocate_shared_arrays_test.cpp
==============================================================================
--- trunk/libs/smart_ptr/test/allocate_shared_arrays_test.cpp (original)
+++ trunk/libs/smart_ptr/test/allocate_shared_arrays_test.cpp 2012-12-11 12:42:47 EST (Tue, 11 Dec 2012)
@@ -100,5 +100,61 @@
BOOST_TEST(type::instances == 0);
}
#endif
+ {
+ boost::shared_ptr<int[][2][2]> a1 = boost::allocate_shared_noinit<int[][2][2]>(std::allocator<int>(), 2);
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ }
+ {
+ boost::shared_ptr<int[2][2][2]> a1 = boost::allocate_shared_noinit<int[2][2][2]>(std::allocator<int>());
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ }
+ {
+ boost::shared_ptr<const int[][2][2]> a1 = boost::allocate_shared_noinit<const int[][2][2]>(std::allocator<int>(), 2);
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ }
+ {
+ boost::shared_ptr<const int[2][2][2]> a1 = boost::allocate_shared_noinit<const int[2][2][2]>(std::allocator<int>());
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<type[][2][2]> a1 = boost::allocate_shared_noinit<type[][2][2]>(std::allocator<type>(), 2);
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(type::instances == 8);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<type[2][2][2]> a1 = boost::allocate_shared_noinit<type[2][2][2]>(std::allocator<type>());
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(type::instances == 8);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<const type[][2][2]> a1 = boost::allocate_shared_noinit<const type[][2][2]>(std::allocator<type>(), 2);
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(type::instances == 8);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
+ BOOST_TEST(type::instances == 0);
+ {
+ boost::shared_ptr<const type[2][2][2]> a1 = boost::allocate_shared_noinit<const type[2][2][2]>(std::allocator<type>());
+ BOOST_TEST(a1.get() != 0);
+ BOOST_TEST(a1.use_count() == 1);
+ BOOST_TEST(type::instances == 8);
+ a1.reset();
+ BOOST_TEST(type::instances == 0);
+ }
return boost::report_errors();
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk