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

Removed bug. allocator wrote beyond allocated memory.

[[Imported from SVN: r1224]]
parent 4ea100ee
No related branches found
No related tags found
No related merge requests found
......@@ -5,6 +5,7 @@
#define DUNE_COMMON_POOLALLOCATOR_HH
#include "alignment.hh"
#include <typeinfo>
namespace Dune
{
......@@ -49,8 +50,8 @@ namespace Dune
/**
* @brief The aligned size of the type.
*
* This size is bigger than sizeof of the type and multiple of
* the algnment requirement.
* This size is bigger than sizeof of the type and a multiple of
* the alignment requirement.
*/
alignedSize = (sizeof(MemberType) % AlignmentOf<MemberType>::value == 0) ?
sizeof(MemberType) :
......@@ -72,6 +73,7 @@ namespace Dune
*/
elements = (chunkSize / alignedSize)
};
private:
struct Reference
{
......@@ -98,6 +100,7 @@ namespace Dune
Chunk()
{
unsigned long lmemory = reinterpret_cast<unsigned long>(chunk_);
if(lmemory % AlignmentOf<MemberType>::value != 0)
lmemory = (lmemory / AlignmentOf<MemberType>::value + 1)
* AlignmentOf<MemberType>::value;
......@@ -124,6 +127,9 @@ namespace Dune
*/
inline void free(void* o);
inline void print();
private:
// Prevent Copying!
......@@ -255,17 +261,16 @@ namespace Dune
template<class T, int S>
inline Pool<T,S>::Pool()
{
head_ = 0;
chunks_ = 0;
}
: head_(0), chunks_(0)
{}
template<class T, int S>
inline Pool<T,S>::~Pool()
{
// delete the allocated chunks.
Chunk *current=chunks_;
while(current)
while(current!=0)
{
Chunk *tmp = current;
current = current->next_;
......@@ -273,6 +278,17 @@ namespace Dune
}
}
template<class T, int S>
inline void Pool<T,S>::print()
{
Chunk* current=chunks_;
while(current) {
std::cout<<current<<" ";
current=current->next_;
}
std::cout<<current<<" ";
}
template<class T, int S>
inline void Pool<T,S>::grow()
{
......@@ -281,7 +297,7 @@ namespace Dune
chunks_ = newChunk;
char* start = reinterpret_cast<char *>(chunks_->memory_);
char* last = &start[elements*alignedSize];
char* last = &start[(elements-1)*alignedSize];
for(char* element=start; element<last; element+=alignedSize)
reinterpret_cast<Reference*>(element)->next_
......
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