Skip to content
Snippets Groups Projects
Commit 59549e9b authored by Christian Engwer's avatar Christian Engwer
Browse files

a) work around clang++ bug 8146

  - move testPool to a struct
  - make this truct friend
b) fix indentation and add editor hints

[[Imported from SVN: r6218]]
parent 816d9c16
No related branches found
No related tags found
No related merge requests found
...@@ -12,11 +12,12 @@ ...@@ -12,11 +12,12 @@
#include <cassert> #include <cassert>
#include <new> #include <new>
template<std::size_t size, typename T>
int testPool();
//forward declarations. //forward declarations.
// we need to know the test function to declare it friend
template<std::size_t size, typename T>
struct testPoolMain;
namespace Dune namespace Dune
{ {
...@@ -83,7 +84,8 @@ namespace Dune ...@@ -83,7 +84,8 @@ namespace Dune
template<class T, std::size_t s> template<class T, std::size_t s>
class Pool 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>&); //friend std::ostream& std::operator<<<>(std::ostream&,Pool<T,s>&);
template< class, std::size_t > friend class PoolAllocator; template< class, std::size_t > friend class PoolAllocator;
...@@ -102,7 +104,6 @@ namespace Dune ...@@ -102,7 +104,6 @@ namespace Dune
typedef T MemberType; typedef T MemberType;
enum enum
{ {
/** /**
* @brief The size of a union of Reference and MemberType. * @brief The size of a union of Reference and MemberType.
*/ */
......
...@@ -20,83 +20,86 @@ struct UnAligned ...@@ -20,83 +20,86 @@ struct UnAligned
template<std::size_t size, typename T> template<std::size_t size, typename T>
int testPool() struct testPoolMain
{ {
int ret=0; static int test()
{
Pool<T,size> pool; int ret=0;
int elements = Pool<T,size>::elements;
//int poolSize = Pool<T,size>::size;
//int chunkSize = Pool<T,size>::chunkSize;
//int alignedSize = Pool<T,size>::alignedSize;
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 unsigned long* oelements = new unsigned long[10*elements];
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;
Chunk* currentChunk = pool.chunks_; typedef typename Pool<T,size>::Chunk Chunk;
assert(element==reinterpret_cast<unsigned long>(currentChunk->memory_)); //Fill 10 chunks
unsigned long end = reinterpret_cast<unsigned long>(currentChunk->chunk_)+Pool<T,size>::chunkSize; 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_)) Chunk* currentChunk = pool.chunks_;
{
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;
}
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++) if(element< reinterpret_cast<unsigned long>(currentChunk->chunk_))
{ {
element = reinterpret_cast<unsigned long>(pool.allocate()); std::cerr <<" buffer overflow during first alloc: "<<reinterpret_cast<unsigned long>(currentChunk->chunk_)
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_)
<<">"<<element<<"+"<<sizeof(T)<<std::endl; <<">"<<element<<"+"<<sizeof(T)<<std::endl;
return ++ret; return ++ret;
} }
if(end < element + sizeof(T)) { 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; return ++ret;
} }
if(oelements[chunk*elements+i-1]+sizeof(T)>element) { oelements[chunk*elements]=element;
std::cerr<<"allocated elements overlap!"<<std::endl;
return ++ret; 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) for(int i=0; i < elements*10; ++i)
pool.free(reinterpret_cast<T*>(oelements+i)); pool.free(reinterpret_cast<T*>(oelements+i));
delete[] oelements; delete[] oelements;
return ret; return ret;
} }
};
template<typename T> template<typename T>
int testPool() int testPool()
...@@ -108,14 +111,15 @@ int testPool() ...@@ -108,14 +111,15 @@ int testPool()
std::cout<<"Checking "<<typeid(T).name()<<" sizeof="<<sizeof(T)<<" with size "<< size<< std::cout<<"Checking "<<typeid(T).name()<<" sizeof="<<sizeof(T)<<" with size "<< size<<
" alignment="<<AlignmentOf<T>::value<<std::endl; " alignment="<<AlignmentOf<T>::value<<std::endl;
ret += testPool<0,T>(); ret += testPoolMain<0,T>::test();
ret += testPool<size,T>(); ret += testPoolMain<size,T>::test();
ret += testPool<5*size,T>(); ret += testPoolMain<5*size,T>::test();
ret += testPool<11*size,T>(); ret += testPoolMain<11*size,T>::test();
ret += testPool<33*size,T>(); ret += testPoolMain<33*size,T>::test();
return ret; return ret;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int ret=0; int ret=0;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment