forked from boostorg/smart_ptr
Add overloads of allocate_shared_noinit to complement make_shared_noinit
[SVN r81858]
This commit is contained in:
@ -209,6 +209,42 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#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
|
#endif
|
||||||
|
@ -96,6 +96,12 @@
|
|||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
shared_ptr<T[N]> <a href="#functions">make_shared_noinit</a>();
|
shared_ptr<T[N]> <a href="#functions">make_shared_noinit</a>();
|
||||||
|
|
||||||
|
template<typename T, typename A>
|
||||||
|
shared_ptr<T[]> <a href="#functions">allocate_shared_noinit</a>(const A& allocator, size_t size);
|
||||||
|
|
||||||
|
template<typename T, typename A>
|
||||||
|
shared_ptr<T[N]> <a href="#functions">allocate_shared_noinit</a>(const A& allocator);
|
||||||
}</pre>
|
}</pre>
|
||||||
<h2><a name="functions">Free Functions</a></h2>
|
<h2><a name="functions">Free Functions</a></h2>
|
||||||
<pre>template<typename T, typename... Args>
|
<pre>template<typename T, typename... Args>
|
||||||
@ -197,13 +203,17 @@ template<typename T, typename A>
|
|||||||
fixed size array.</p>
|
fixed size array.</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<pre>template<typename T>
|
<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>
|
<blockquote>
|
||||||
<p><b>Description:</b> This overload does not perform value
|
<p><b>Description:</b> This overload does not perform value
|
||||||
initialization of elements.</p>
|
initialization of elements.</p>
|
||||||
</blockquote>
|
</blockquote>
|
||||||
<pre>template<typename T>
|
<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>
|
<blockquote>
|
||||||
<p><b>Description:</b> This overload of the utility above is used for a
|
<p><b>Description:</b> This overload of the utility above is used for a
|
||||||
fixed size array.</p>
|
fixed size array.</p>
|
||||||
|
@ -38,5 +38,15 @@ int main() {
|
|||||||
BOOST_TEST(type::instances == 3);
|
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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
@ -121,5 +121,79 @@ int main() {
|
|||||||
BOOST_TEST(type::instances == 0);
|
BOOST_TEST(type::instances == 0);
|
||||||
}
|
}
|
||||||
#endif
|
#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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
@ -59,5 +59,19 @@ int main() {
|
|||||||
BOOST_TEST(type::instances == 0);
|
BOOST_TEST(type::instances == 0);
|
||||||
}
|
}
|
||||||
#endif
|
#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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
@ -100,5 +100,61 @@ int main() {
|
|||||||
BOOST_TEST(type::instances == 0);
|
BOOST_TEST(type::instances == 0);
|
||||||
}
|
}
|
||||||
#endif
|
#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();
|
return boost::report_errors();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user