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