Add support and tests for C++11 minimal allocators

This commit is contained in:
Peter Dimov
2014-02-05 00:17:34 +02:00
parent e8595a05af
commit 90e74511f7
7 changed files with 524 additions and 6 deletions

View File

@ -225,16 +225,35 @@ public:
#endif #endif
{ {
typedef sp_counted_impl_pda<P, D, A> impl_type; typedef sp_counted_impl_pda<P, D, A> impl_type;
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
#else
typedef typename A::template rebind< impl_type >::other A2; typedef typename A::template rebind< impl_type >::other A2;
#endif
A2 a2( a ); A2 a2( a );
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
try try
{ {
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
pi_ = pi;
std::allocator_traits<A2>::construct( a2, pi, p, d, a );
#else
pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) ); pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
new( static_cast< void* >( pi_ ) ) impl_type( p, d, a ); ::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
#endif
} }
catch(...) catch(...)
{ {
@ -248,13 +267,30 @@ public:
throw; throw;
} }
#else
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
pi_ = pi;
#else #else
pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) ); pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
#endif
if( pi_ != 0 ) if( pi_ != 0 )
{ {
new( static_cast< void* >( pi_ ) ) impl_type( p, d, a ); #if !defined( BOOST_NO_CXX11_ALLOCATOR )
std::allocator_traits<A2>::construct( a2, pi, p, d, a );
#else
::new( static_cast< void* >( pi_ ) ) impl_type( p, d, a );
#endif
} }
else else
{ {
@ -273,16 +309,35 @@ public:
#endif #endif
{ {
typedef sp_counted_impl_pda< P, D, A > impl_type; typedef sp_counted_impl_pda< P, D, A > impl_type;
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
typedef typename std::allocator_traits<A>::template rebind_alloc< impl_type > A2;
#else
typedef typename A::template rebind< impl_type >::other A2; typedef typename A::template rebind< impl_type >::other A2;
#endif
A2 a2( a ); A2 a2( a );
#ifndef BOOST_NO_EXCEPTIONS #ifndef BOOST_NO_EXCEPTIONS
try try
{ {
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
pi_ = pi;
std::allocator_traits<A2>::construct( a2, pi, p, a );
#else
pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) ); pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
new( static_cast< void* >( pi_ ) ) impl_type( p, a ); ::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
#endif
} }
catch(...) catch(...)
{ {
@ -296,13 +351,30 @@ public:
throw; throw;
} }
#else
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
impl_type * pi = std::allocator_traits<A2>::allocate( a2, 1 );
pi_ = pi;
#else #else
pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) ); pi_ = a2.allocate( 1, static_cast< impl_type* >( 0 ) );
#endif
if( pi_ != 0 ) if( pi_ != 0 )
{ {
new( static_cast< void* >( pi_ ) ) impl_type( p, a ); #if !defined( BOOST_NO_CXX11_ALLOCATOR )
std::allocator_traits<A2>::construct( a2, pi, p, a );
#else
::new( static_cast< void* >( pi_ ) ) impl_type( p, a );
#endif
} }
else else
{ {

View File

@ -224,8 +224,16 @@ public:
virtual void destroy() // nothrow virtual void destroy() // nothrow
{ {
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
typedef typename std::allocator_traits<A>::template rebind_alloc< this_type > A2;
#else
typedef typename A::template rebind< this_type >::other A2; typedef typename A::template rebind< this_type >::other A2;
#endif
A2 a2( a_ ); A2 a2( a_ );
this->~this_type(); this->~this_type();

View File

@ -79,6 +79,9 @@ import testing ;
[ run sp_zero_compare_test.cpp ] [ run sp_zero_compare_test.cpp ]
[ run sp_nullptr_test.cpp ] [ run sp_nullptr_test.cpp ]
[ run sa_nullptr_test.cpp ] [ run sa_nullptr_test.cpp ]
[ run shared_ptr_alloc3_test.cpp ]
[ run shared_ptr_alloc11_test.cpp ]
[ run allocate_shared_alloc11_test.cpp ]
[ compile-fail array_fail_spa_sp_c.cpp ] [ compile-fail array_fail_spa_sp_c.cpp ]
[ compile-fail array_fail_sp_spa_c.cpp ] [ compile-fail array_fail_sp_spa_c.cpp ]

View File

@ -0,0 +1,241 @@
// allocate_shared_alloc11_test.cpp
//
// allocate_shared with a minimal C++11 allocator
//
// Copyright 2007-2009, 2014 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/detail/lightweight_test.hpp>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/config.hpp>
#include <cstddef>
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
template< class T > class cxx11_allocator
{
public:
typedef T value_type;
cxx11_allocator()
{
}
template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
{
}
T * allocate( std::size_t n )
{
return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
}
void deallocate( T * p, std::size_t n )
{
::operator delete( p );
}
};
class X
{
private:
X( X const & );
X & operator=( X const & );
void * operator new( std::size_t n )
{
BOOST_ERROR( "private X::new called" );
return ::operator new( n );
}
void operator delete( void * p )
{
BOOST_ERROR( "private X::delete called" );
::operator delete( p );
}
public:
static int instances;
int v;
explicit X( int a1 = 0, int a2 = 0, int a3 = 0, int a4 = 0, int a5 = 0, int a6 = 0, int a7 = 0, int a8 = 0, int a9 = 0 ): v( a1+a2+a3+a4+a5+a6+a7+a8+a9 )
{
++instances;
}
~X()
{
--instances;
}
};
int X::instances = 0;
int main()
{
{
boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>() );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( *pi == 0 );
}
{
boost::shared_ptr< int > pi = boost::allocate_shared< int >( cxx11_allocator<int>(), 5 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( *pi == 5 );
}
BOOST_TEST( X::instances == 0 );
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>() );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 0 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4+5 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4+5+6 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4+5+6+7 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
{
boost::shared_ptr< X > pi = boost::allocate_shared< X >( cxx11_allocator<void>(), 1, 2, 3, 4, 5, 6, 7, 8, 9 );
boost::weak_ptr<X> wp( pi );
BOOST_TEST( X::instances == 1 );
BOOST_TEST( pi.get() != 0 );
BOOST_TEST( pi->v == 1+2+3+4+5+6+7+8+9 );
pi.reset();
BOOST_TEST( X::instances == 0 );
}
return boost::report_errors();
}
#else // !defined( BOOST_NO_CXX11_ALLOCATOR )
int main()
{
return 0;
}
#endif

View File

@ -0,0 +1,116 @@
#include <boost/config.hpp>
// shared_ptr_alloc11_test.cpp
//
// Test the allocator constructor with a C++11 minimal allocator
//
// Copyright (c) 2005, 2014 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/detail/lightweight_test.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include <cstddef>
#if !defined( BOOST_NO_CXX11_ALLOCATOR )
template< class T > class cxx11_allocator
{
public:
typedef T value_type;
cxx11_allocator()
{
}
template< class Y > cxx11_allocator( cxx11_allocator<Y> const & )
{
}
T * allocate( std::size_t n )
{
return static_cast< T* >( ::operator new( n * sizeof( T ) ) );
}
void deallocate( T * p, std::size_t n )
{
::operator delete( p );
}
};
//
struct D;
struct X
{
static int instances;
X(): deleted_( false )
{
++instances;
}
~X()
{
BOOST_TEST( deleted_ );
--instances;
}
private:
friend struct D;
bool deleted_;
X( X const & );
X & operator=( X const & );
};
int X::instances = 0;
struct D
{
void operator()( X * px ) const
{
px->deleted_ = true;
delete px;
}
};
int main()
{
BOOST_TEST( X::instances == 0 );
boost::shared_ptr<void> pv( new X, D(), cxx11_allocator<X>() );
BOOST_TEST( X::instances == 1 );
pv.reset();
BOOST_TEST( X::instances == 0 );
pv.reset( new X, D(), cxx11_allocator<void>() );
BOOST_TEST( X::instances == 1 );
pv.reset();
BOOST_TEST( X::instances == 0 );
return boost::report_errors();
}
#else // !defined( BOOST_NO_CXX11_ALLOCATOR )
int main()
{
return 0;
}
#endif

View File

@ -91,7 +91,7 @@ public:
::operator delete( p ); ::operator delete( p );
} }
pointer allocate( size_type n, void const * ) pointer allocate( size_type n, void const * = 0 )
{ {
T * p = static_cast< T* >( ::operator new( n * sizeof( T ) ) ); T * p = static_cast< T* >( ::operator new( n * sizeof( T ) ) );
@ -107,7 +107,7 @@ public:
void construct( pointer p, T const & t ) void construct( pointer p, T const & t )
{ {
new( p ) T( t ); ::new( p ) T( t );
} }
void destroy( pointer p ) void destroy( pointer p )

View File

@ -0,0 +1,78 @@
#include <boost/config.hpp>
// shared_ptr_alloc3_test.cpp
//
// Copyright (c) 2005, 2014 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
#include <boost/detail/lightweight_test.hpp>
#include <boost/shared_ptr.hpp>
#include <memory>
#include <cstddef>
//
struct D;
struct X
{
static int instances;
X(): deleted_( false )
{
++instances;
}
~X()
{
BOOST_TEST( deleted_ );
--instances;
}
private:
friend struct D;
bool deleted_;
X( X const & );
X & operator=( X const & );
};
int X::instances = 0;
struct D
{
void operator()( X * px ) const
{
px->deleted_ = true;
delete px;
}
};
int main()
{
BOOST_TEST( X::instances == 0 );
boost::shared_ptr<void> pv( new X, D(), std::allocator<X>() );
BOOST_TEST( X::instances == 1 );
pv.reset();
BOOST_TEST( X::instances == 0 );
pv.reset( new X, D(), std::allocator<void>() );
BOOST_TEST( X::instances == 1 );
pv.reset();
BOOST_TEST( X::instances == 0 );
return boost::report_errors();
}