mirror of
https://github.com/boostorg/smart_ptr.git
synced 2025-07-31 05:07:21 +02:00
Dave's quick_allocator added, #define BOOST_SP_USE_QUICK_ALLOCATOR to make shared_ptr use it.
[SVN r17087]
This commit is contained in:
@ -18,19 +18,24 @@
|
|||||||
|
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
|
|
||||||
#ifndef BOOST_NO_AUTO_PTR
|
#if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||||
# include <memory>
|
# error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible.
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#include <boost/checked_delete.hpp>
|
#include <boost/checked_delete.hpp>
|
||||||
#include <boost/throw_exception.hpp>
|
#include <boost/throw_exception.hpp>
|
||||||
#include <boost/detail/lightweight_mutex.hpp>
|
#include <boost/detail/lightweight_mutex.hpp>
|
||||||
|
|
||||||
#include <functional> // for std::less
|
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||||
#include <exception> // for std::exception
|
#include <boost/detail/quick_allocator.hpp>
|
||||||
#include <new> // for std::bad_alloc
|
#endif
|
||||||
#include <typeinfo> // for std::type_info in get_deleter
|
|
||||||
#include <cstddef> // for std::size_t
|
#include <memory> // std::auto_ptr, std::allocator
|
||||||
|
#include <functional> // std::less
|
||||||
|
#include <exception> // std::exception
|
||||||
|
#include <new> // std::bad_alloc
|
||||||
|
#include <typeinfo> // std::type_info in get_deleter
|
||||||
|
#include <cstddef> // std::size_t
|
||||||
|
|
||||||
#ifdef __BORLANDC__
|
#ifdef __BORLANDC__
|
||||||
# pragma warn -8026 // Functions with excep. spec. are not expanded inline
|
# pragma warn -8026 // Functions with excep. spec. are not expanded inline
|
||||||
@ -274,6 +279,20 @@ public:
|
|||||||
std::allocator<this_type>().deallocate(static_cast<this_type *>(p), 1);
|
std::allocator<this_type>().deallocate(static_cast<this_type *>(p), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||||
|
|
||||||
|
void * operator new(std::size_t)
|
||||||
|
{
|
||||||
|
return quick_allocator<this_type>::alloc();
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void * p)
|
||||||
|
{
|
||||||
|
quick_allocator<this_type>::dealloc(p);
|
||||||
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
//
|
//
|
||||||
// shared_ptr_alloc_test.cpp - use to evaluate the impact of count allocations
|
// shared_ptr_alloc_test.cpp - use to evaluate the impact of count allocations
|
||||||
//
|
//
|
||||||
// Copyright (c) 2002 Peter Dimov and Multi Media Ltd.
|
// Copyright (c) 2002, 2003 Peter Dimov
|
||||||
//
|
//
|
||||||
// Permission to copy, use, modify, sell and distribute this software
|
// Permission to copy, use, modify, sell and distribute this software
|
||||||
// is granted provided this copyright notice appears in all copies.
|
// is granted provided this copyright notice appears in all copies.
|
||||||
@ -11,10 +11,12 @@
|
|||||||
|
|
||||||
#include <boost/shared_ptr.hpp>
|
#include <boost/shared_ptr.hpp>
|
||||||
#include <boost/config.hpp>
|
#include <boost/config.hpp>
|
||||||
|
#include <boost/detail/quick_allocator.hpp>
|
||||||
|
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
|
#include <cstddef>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
|
||||||
int const n = 1024 * 1024;
|
int const n = 1024 * 1024;
|
||||||
@ -33,7 +35,7 @@ template<class T> void test(T * = 0)
|
|||||||
|
|
||||||
t = std::clock() - t;
|
t = std::clock() - t;
|
||||||
|
|
||||||
std::cout << static_cast<double>(t) / CLOCKS_PER_SEC << '\n';
|
std::cout << " " << static_cast<double>(t) / CLOCKS_PER_SEC << " seconds.\n";
|
||||||
}
|
}
|
||||||
|
|
||||||
class X
|
class X
|
||||||
@ -62,6 +64,48 @@ private:
|
|||||||
int n_;
|
int n_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class Y
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit Y(int n): n_(n)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
void * operator new(std::size_t n)
|
||||||
|
{
|
||||||
|
return boost::detail::quick_allocator<Y>::alloc(n);
|
||||||
|
}
|
||||||
|
|
||||||
|
void operator delete(void * p, std::size_t n)
|
||||||
|
{
|
||||||
|
boost::detail::quick_allocator<Y>::dealloc(p, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Y(Y const &);
|
||||||
|
Y & operator=(Y const &);
|
||||||
|
|
||||||
|
int n_;
|
||||||
|
};
|
||||||
|
|
||||||
|
class Z: public Y
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
explicit Z(int n): Y(n), m_(n + 1)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
Z(Z const &);
|
||||||
|
Z & operator=(Z const &);
|
||||||
|
|
||||||
|
int m_;
|
||||||
|
};
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
std::cout << BOOST_COMPILER "\n";
|
std::cout << BOOST_COMPILER "\n";
|
||||||
@ -80,6 +124,12 @@ int main()
|
|||||||
std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n";
|
std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n";
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if defined(BOOST_SP_USE_QUICK_ALLOCATOR)
|
||||||
|
std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (defined)\n";
|
||||||
|
#else
|
||||||
|
std::cout << "BOOST_SP_USE_QUICK_ALLOCATOR: (not defined)\n";
|
||||||
|
#endif
|
||||||
|
|
||||||
std::cout << n << " shared_ptr<int> allocations + deallocations:\n";
|
std::cout << n << " shared_ptr<int> allocations + deallocations:\n";
|
||||||
|
|
||||||
test<int>();
|
test<int>();
|
||||||
@ -91,4 +141,16 @@ int main()
|
|||||||
test<X>();
|
test<X>();
|
||||||
test<X>();
|
test<X>();
|
||||||
test<X>();
|
test<X>();
|
||||||
|
|
||||||
|
std::cout << n << " shared_ptr<Y> allocations + deallocations:\n";
|
||||||
|
|
||||||
|
test<Y>();
|
||||||
|
test<Y>();
|
||||||
|
test<Y>();
|
||||||
|
|
||||||
|
std::cout << n << " shared_ptr<Z> allocations + deallocations:\n";
|
||||||
|
|
||||||
|
test<Z>();
|
||||||
|
test<Z>();
|
||||||
|
test<Z>();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user