From cd41426fe9bedcd982a3f81d17d8e4a8ed276b20 Mon Sep 17 00:00:00 2001 From: Peter Dimov Date: Thu, 30 Jan 2003 14:20:22 +0000 Subject: [PATCH] Dave's quick_allocator added, #define BOOST_SP_USE_QUICK_ALLOCATOR to make shared_ptr use it. [SVN r17087] --- include/boost/detail/shared_count.hpp | 33 +++++++++++--- test/shared_ptr_alloc_test.cpp | 66 ++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 9 deletions(-) diff --git a/include/boost/detail/shared_count.hpp b/include/boost/detail/shared_count.hpp index cb9e713..0e2783c 100644 --- a/include/boost/detail/shared_count.hpp +++ b/include/boost/detail/shared_count.hpp @@ -18,19 +18,24 @@ #include -#ifndef BOOST_NO_AUTO_PTR -# include +#if defined(BOOST_SP_USE_STD_ALLOCATOR) && defined(BOOST_SP_USE_QUICK_ALLOCATOR) +# error BOOST_SP_USE_STD_ALLOCATOR and BOOST_SP_USE_QUICK_ALLOCATOR are incompatible. #endif #include #include #include -#include // for std::less -#include // for std::exception -#include // for std::bad_alloc -#include // for std::type_info in get_deleter -#include // for std::size_t +#if defined(BOOST_SP_USE_QUICK_ALLOCATOR) +#include +#endif + +#include // std::auto_ptr, std::allocator +#include // std::less +#include // std::exception +#include // std::bad_alloc +#include // std::type_info in get_deleter +#include // std::size_t #ifdef __BORLANDC__ # pragma warn -8026 // Functions with excep. spec. are not expanded inline @@ -274,6 +279,20 @@ public: std::allocator().deallocate(static_cast(p), 1); } +#endif + +#if defined(BOOST_SP_USE_QUICK_ALLOCATOR) + + void * operator new(std::size_t) + { + return quick_allocator::alloc(); + } + + void operator delete(void * p) + { + quick_allocator::dealloc(p); + } + #endif }; diff --git a/test/shared_ptr_alloc_test.cpp b/test/shared_ptr_alloc_test.cpp index 079dde7..5a3e17a 100644 --- a/test/shared_ptr_alloc_test.cpp +++ b/test/shared_ptr_alloc_test.cpp @@ -1,7 +1,7 @@ // // 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 // is granted provided this copyright notice appears in all copies. @@ -11,10 +11,12 @@ #include #include +#include #include #include #include +#include #include int const n = 1024 * 1024; @@ -33,7 +35,7 @@ template void test(T * = 0) t = std::clock() - t; - std::cout << static_cast(t) / CLOCKS_PER_SEC << '\n'; + std::cout << " " << static_cast(t) / CLOCKS_PER_SEC << " seconds.\n"; } class X @@ -62,6 +64,48 @@ private: int n_; }; +class Y +{ +public: + + explicit Y(int n): n_(n) + { + } + + void * operator new(std::size_t n) + { + return boost::detail::quick_allocator::alloc(n); + } + + void operator delete(void * p, std::size_t n) + { + boost::detail::quick_allocator::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() { std::cout << BOOST_COMPILER "\n"; @@ -80,6 +124,12 @@ int main() std::cout << "BOOST_SP_USE_STD_ALLOCATOR: (not defined)\n"; #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 allocations + deallocations:\n"; test(); @@ -91,4 +141,16 @@ int main() test(); test(); test(); + + std::cout << n << " shared_ptr allocations + deallocations:\n"; + + test(); + test(); + test(); + + std::cout << n << " shared_ptr allocations + deallocations:\n"; + + test(); + test(); + test(); }