Skip to content
Snippets Groups Projects
Commit 22cc696a authored by Markus Blatt's avatar Markus Blatt
Browse files

Damn it. I forgot half the check-in.

New version without reinterpret casts. Please test!

[[Imported from SVN: r5730]]
parent 561c6fb5
No related branches found
No related tags found
No related merge requests found
......@@ -10,6 +10,7 @@
#include <typeinfo>
#include <iostream>
#include <cassert>
#include <new>
template<std::size_t size, typename T>
int testPool();
......@@ -192,7 +193,7 @@ namespace Dune
* @brief Get a new or recycled object
* @return A pointer to the object memory.
*/
inline T *allocate();
inline void* allocate();
/**
* @brief Free an object.
* @param o The pointer to memory block of the object.
......@@ -463,8 +464,8 @@ namespace Dune
dune_static_assert((chunkSize - (alignment - 1)) % alignment == 0, "Library Error: compiler cannot calculate!");
dune_static_assert(elements>=1, "Library Error: we need to hold at least one element!");
dune_static_assert(elements*alignedSize<=chunkSize, "Library Error: aligned elements must fit into chuck!");
/*std::cout<<"s= "<<S<<" : T: "<<sizeof(T)<<" Reference: "<<sizeof(Reference)<<" union: "<<unionSize<<" alignment: "<<alignment<<
"aligned: "<<alignedSize<<" chunk: "<< chunkSize<<" elements: "<<elements<<std::endl;*/
/* std::cout<<"s= "<<S<<" : T: "<<sizeof(T)<<" Reference: "<<sizeof(Reference)<<" union: "<<unionSize<<" alignment: "<<alignment<<
"aligned: "<<alignedSize<<" chunk: "<< chunkSize<<" elements: "<<elements<<std::endl;*/
}
template<class T, std::size_t S>
......@@ -505,23 +506,24 @@ namespace Dune
newChunk->next_ = chunks_;
chunks_ = newChunk;
char* start = reinterpret_cast<char *>(chunks_->memory_);
char* last = &start[(elements-1)*alignedSize];
char* start = chunks_->memory_;
char* last = &start[elements*alignedSize];
Reference* ref = new (start) (Reference);
head_ = ref;
for(char* element=start; element<last; element=element+alignedSize) {
reinterpret_cast<Reference*>(element)->next_
= reinterpret_cast<Reference*>(element+alignedSize);
for(char* element=start+alignedSize; element<last; element=element+alignedSize) {
Reference* next = new (element) (Reference);
ref->next_ = next;
ref = next;
}
reinterpret_cast<Reference*>(last)->next_=0;
head_ = reinterpret_cast<Reference*>(start);
ref->next_=0;
}
template<class T, std::size_t S>
inline void Pool<T,S>::free(void* b)
{
if(b) {
Reference* freed = reinterpret_cast<Reference*>(b);
Reference* freed = static_cast<Reference*>(b);
freed->next_ = head_;
head_ = freed;
//--allocated_;
......@@ -530,7 +532,7 @@ namespace Dune
}
template<class T, std::size_t S>
inline T* Pool<T,S>::allocate()
inline void* Pool<T,S>::allocate()
{
if(!head_)
grow();
......@@ -538,7 +540,7 @@ namespace Dune
Reference* p = head_;
head_ = p->next_;
//++allocated_;
return reinterpret_cast<T*>(p);
return p;
}
template<class T, std::size_t s>
......@@ -551,8 +553,10 @@ namespace Dune
template<class T, std::size_t s>
inline T* PoolAllocator<T,s>::allocate(std::size_t n, const T* hint)
{
assert(n==1); //<=(Pool<T,s>::elements));
return memoryPool_.allocate();
if(n==1)
return static_cast<T*>(memoryPool_.allocate());
else
throw std::bad_alloc();
}
template<class T, std::size_t s>
......
......@@ -108,8 +108,8 @@ 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<0,T>();
ret += testPool<size,T>();
ret += testPool<5*size,T>();
ret += testPool<11*size,T>();
ret += testPool<33*size,T>();
......
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