diff --git a/dune/common/poolallocator.hh b/dune/common/poolallocator.hh index 574fd5f60fd07d583a7b070f9b8c8e8e4f0849a0..81485a930b6e6962cf415642b4c6e2a0f5970ce0 100644 --- a/dune/common/poolallocator.hh +++ b/dune/common/poolallocator.hh @@ -12,11 +12,12 @@ #include <cassert> #include <new> -template<std::size_t size, typename T> -int testPool(); - //forward declarations. +// we need to know the test function to declare it friend +template<std::size_t size, typename T> +struct testPoolMain; + namespace Dune { @@ -83,7 +84,8 @@ namespace Dune template<class T, std::size_t s> class Pool { - friend int ::testPool<s,T>(); + // make the test function friend + friend struct ::testPoolMain<s,T>; //friend std::ostream& std::operator<<<>(std::ostream&,Pool<T,s>&); template< class, std::size_t > friend class PoolAllocator; @@ -102,7 +104,6 @@ namespace Dune typedef T MemberType; enum { - /** * @brief The size of a union of Reference and MemberType. */ diff --git a/dune/common/test/poolallocatortest.cc b/dune/common/test/poolallocatortest.cc index 6cc780aed1c24a4e43eca326fc9b53fb370fee9a..32f0e03509a39e8104e65cf11c8bc6f9fa90be20 100644 --- a/dune/common/test/poolallocatortest.cc +++ b/dune/common/test/poolallocatortest.cc @@ -20,83 +20,86 @@ struct UnAligned template<std::size_t size, typename T> -int testPool() +struct testPoolMain { - int ret=0; - - Pool<T,size> pool; - - int elements = Pool<T,size>::elements; - //int poolSize = Pool<T,size>::size; - //int chunkSize = Pool<T,size>::chunkSize; - //int alignedSize = Pool<T,size>::alignedSize; + static int test() + { + int ret=0; - unsigned long* oelements = new unsigned long[10*elements]; + Pool<T,size> pool; - typedef typename Pool<T,size>::Chunk Chunk; + int elements = Pool<T,size>::elements; + //int poolSize = Pool<T,size>::size; + //int chunkSize = Pool<T,size>::chunkSize; + //int alignedSize = Pool<T,size>::alignedSize; - //Fill 10 chunks - for(int chunk=0; chunk < 10; ++chunk) { - //std::cout<< std::endl<<"Chunk "<<chunk<<" "; - unsigned long element = reinterpret_cast<unsigned long>(pool.allocate()); - void* celement = reinterpret_cast<void*>(element); - //std::cout << element<<" "<< celement<<", "<<std::endl; + unsigned long* oelements = new unsigned long[10*elements]; - Chunk* currentChunk = pool.chunks_; + typedef typename Pool<T,size>::Chunk Chunk; - assert(element==reinterpret_cast<unsigned long>(currentChunk->memory_)); - unsigned long end = reinterpret_cast<unsigned long>(currentChunk->chunk_)+Pool<T,size>::chunkSize; + //Fill 10 chunks + for(int chunk=0; chunk < 10; ++chunk) { + //std::cout<< std::endl<<"Chunk "<<chunk<<" "; + unsigned long element = reinterpret_cast<unsigned long>(pool.allocate()); + void* celement = reinterpret_cast<void*>(element); + //std::cout << element<<" "<< celement<<", "<<std::endl; - if(element< reinterpret_cast<unsigned long>(currentChunk->chunk_)) - { - std::cerr <<" buffer overflow during first alloc: "<<reinterpret_cast<unsigned long>(currentChunk->chunk_) - <<">"<<element<<"+"<<sizeof(T)<<std::endl; - return ++ret; - } - - if(end < element + sizeof(T)) { - std::cerr <<" buffer overflow during first alloc: "<<end<<"<"<<element<<"+"<<sizeof(T)<<std::endl; - return ++ret; - } + Chunk* currentChunk = pool.chunks_; - oelements[chunk*elements]=element; + assert(element==reinterpret_cast<unsigned long>(currentChunk->memory_)); + unsigned long end = reinterpret_cast<unsigned long>(currentChunk->chunk_)+Pool<T,size>::chunkSize; - for(int i=1; i < elements; i++) - { - element = reinterpret_cast<unsigned long>(pool.allocate()); - celement = reinterpret_cast<void*>(element); - - // std::cout << element<<" "<<celement<<", "<<std::endl; - - if(element< reinterpret_cast<unsigned long>(currentChunk->chunk_)) { - std::cerr <<" buffer underflow during first alloc: "<<reinterpret_cast<unsigned long>(currentChunk->chunk_) + if(element< reinterpret_cast<unsigned long>(currentChunk->chunk_)) + { + std::cerr <<" buffer overflow during first alloc: "<<reinterpret_cast<unsigned long>(currentChunk->chunk_) <<">"<<element<<"+"<<sizeof(T)<<std::endl; return ++ret; } if(end < element + sizeof(T)) { - std::cerr <<" buffer overflow during "<<i<<" alloc: "<<end<<"<"<<element+sizeof(T)<<std::endl; + std::cerr <<" buffer overflow during first alloc: "<<end<<"<"<<element<<"+"<<sizeof(T)<<std::endl; return ++ret; - } - if(oelements[chunk*elements+i-1]+sizeof(T)>element) { - std::cerr<<"allocated elements overlap!"<<std::endl; - return ++ret; - } + oelements[chunk*elements]=element; + + for(int i=1; i < elements; i++) + { + element = reinterpret_cast<unsigned long>(pool.allocate()); + celement = reinterpret_cast<void*>(element); - oelements[chunk*elements+i]=element; + // std::cout << element<<" "<<celement<<", "<<std::endl; + + if(element< reinterpret_cast<unsigned long>(currentChunk->chunk_)) { + std::cerr <<" buffer underflow during first alloc: "<<reinterpret_cast<unsigned long>(currentChunk->chunk_) + <<">"<<element<<"+"<<sizeof(T)<<std::endl; + return ++ret; + } + + if(end < element + sizeof(T)) { + std::cerr <<" buffer overflow during "<<i<<" alloc: "<<end<<"<"<<element+sizeof(T)<<std::endl; + return ++ret; + + } + + if(oelements[chunk*elements+i-1]+sizeof(T)>element) { + std::cerr<<"allocated elements overlap!"<<std::endl; + return ++ret; + } + + oelements[chunk*elements+i]=element; + } } - } - for(int i=0; i < elements*10; ++i) - pool.free(reinterpret_cast<T*>(oelements+i)); - delete[] oelements; + for(int i=0; i < elements*10; ++i) + pool.free(reinterpret_cast<T*>(oelements+i)); + delete[] oelements; - return ret; -} + return ret; + } +}; template<typename T> int testPool() @@ -108,14 +111,15 @@ int testPool() std::cout<<"Checking "<<typeid(T).name()<<" sizeof="<<sizeof(T)<<" with size "<< size<< " alignment="<<AlignmentOf<T>::value<<std::endl; - ret += testPool<0,T>(); - ret += testPool<size,T>(); - ret += testPool<5*size,T>(); - ret += testPool<11*size,T>(); - ret += testPool<33*size,T>(); + ret += testPoolMain<0,T>::test(); + ret += testPoolMain<size,T>::test(); + ret += testPoolMain<5*size,T>::test(); + ret += testPoolMain<11*size,T>::test(); + ret += testPoolMain<33*size,T>::test(); return ret; } + int main(int argc, char **argv) { int ret=0;