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
Branches
Tags
No related merge requests found
......@@ -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.
*/
......
......@@ -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;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment