From cb5be0f2506335609dd33de38b948e42cde052ef Mon Sep 17 00:00:00 2001 From: Markus Blatt <markus@dr-blatt.de> Date: Thu, 3 Apr 2014 21:44:18 +0200 Subject: [PATCH] [bugfix][release] Added missing copy constructor for Pool. When copying PoolAllocators Bard experienced linking errors to the copy constructor of Pool, which is deactivated. To fix this we added the copy constructor. Note that it does not copy the pool such that ownership of allocated memory chunks is clear. Kudos go to Bard for noticing this and providing the fix. In addition we removed the copy constructor of the specialization for void as the default coy constructor suffices. --- dune/common/poolallocator.hh | 22 +++++++++++++++------- dune/common/test/poolallocatortest.cc | 23 +++++++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/dune/common/poolallocator.hh b/dune/common/poolallocator.hh index 8aa497462..ff873c507 100644 --- a/dune/common/poolallocator.hh +++ b/dune/common/poolallocator.hh @@ -300,12 +300,25 @@ namespace Dune inline PoolAllocator(); /** - * @brief Coopy Constructor. + * @brief Copy Constructor that does not copy the memory pool. */ template<typename U, std::size_t u> inline PoolAllocator(const PoolAllocator<U,u>&) - {} + { + // we allow copying but never copy the pool + // to have a clear ownership of allocated pointers. + } + /// \brief Copy constructor that does not copy the memory pool. + PoolAllocator(const PoolAllocator&) + { + // we allow copying but never copy the pool + // to have a clear ownership of allocated pointers. + // For this behaviour we have to implement + // the copy constructor, because the default + // one would copy the pool and deallocation + // of it would break. + } /** * @brief Allocates objects. * @param n The number of objects to allocate. Has to be one! @@ -384,11 +397,6 @@ namespace Dune { typedef PoolAllocator<U,s> other; }; - - template<typename T, std::size_t t> - PoolAllocator(const PoolAllocator<T,t>&) - {} - }; diff --git a/dune/common/test/poolallocatortest.cc b/dune/common/test/poolallocatortest.cc index 21a513d88..2832dcde4 100644 --- a/dune/common/test/poolallocatortest.cc +++ b/dune/common/test/poolallocatortest.cc @@ -119,6 +119,28 @@ int testPool() return ret; } +int testPoolAllocator() +{ + int ret=0; + PoolAllocator<double,10> pool; + double *d=pool.allocate(1); + PoolAllocator<float,5> pool1=pool; + PoolAllocator<double,10> pool2=pool; + try + { + pool2.deallocate(d,1); + ++ret; + std::cerr<<"ERROR: allocation should not work with copied allocators."<<std::endl; + } + catch(std::bad_alloc) + {} + pool1.allocate(1); + double *d1=pool2.allocate(1); + pool.deallocate(d,1); + pool2.deallocate(d1,1); + pool2.allocate(1); + return ret; +} int main(int, char **) { int ret=0; @@ -131,6 +153,7 @@ int main(int, char **) ret += testPool<Dune::FieldMatrix<double,10,10> >(); + ret+=testPoolAllocator(); std::cout<<AlignmentOf<UnAligned>::value<<" "<<sizeof(UnAligned)<<std::endl; -- GitLab