Skip to content
Snippets Groups Projects
Commit 1cc73042 authored by Martin Nolte's avatar Martin Nolte
Browse files

fix a memory leak (though within a singleton, it occurred only on program termination)

[[Imported from SVN: r5278]]
parent 7047d2b9
No related branches found
No related tags found
No related merge requests found
...@@ -3,6 +3,7 @@ ...@@ -3,6 +3,7 @@
#ifndef DUNE_SMALLOBJECT_HH #ifndef DUNE_SMALLOBJECT_HH
#define DUNE_SMALLOBJECT_HH #define DUNE_SMALLOBJECT_HH
#include <cassert>
#include <new> #include <new>
namespace Dune namespace Dune
...@@ -36,7 +37,14 @@ namespace Dune ...@@ -36,7 +37,14 @@ namespace Dune
~SmallObjectPool () ~SmallObjectPool ()
{ {
for( unsigned int i = 0; i < maxBlocks; ++i ) for( unsigned int i = 0; i < maxBlocks; ++i )
delete list_[ i ]; {
for( Block *next = list_[ i ]; next != 0; )
{
Block *current = next;
next = current->next;
delete[] current;
}
}
} }
static SmallObjectPool &instance () static SmallObjectPool &instance ()
...@@ -47,6 +55,7 @@ namespace Dune ...@@ -47,6 +55,7 @@ namespace Dune
static Block *&list ( unsigned int blocks ) static Block *&list ( unsigned int blocks )
{ {
assert( blocks < maxBlocks );
return instance().list_[ blocks ]; return instance().list_[ blocks ];
} }
...@@ -54,25 +63,27 @@ namespace Dune ...@@ -54,25 +63,27 @@ namespace Dune
static void *allocate ( unsigned int size ) static void *allocate ( unsigned int size )
{ {
const unsigned int blocks = (size + (blockSize-1)) / blockSize; const unsigned int blocks = (size + (blockSize-1)) / blockSize;
if( blocks > maxBlocks ) if( blocks >= maxBlocks )
return 0; return 0;
Block *blockPtr = list( blocks ); Block *&next = list( blocks );
if( blockPtr != 0 ) Block *current = next;
list( blocks ) = blockPtr->next; if( current != 0 )
next = current->next;
else else
blockPtr = new Block[ blocks+1 ]; current = new Block[ blocks+1 ];
blockPtr->blocks = blocks; current->blocks = blocks;
return blockPtr+1; return current + 1;
} }
static void free ( void *ptr ) static void free ( void *ptr )
{ {
if( ptr != 0 ) if( ptr != 0 )
{ {
Block *blockPtr = reinterpret_cast< Block * >( ptr ) - 1; Block *current = reinterpret_cast< Block * >( ptr ) - 1;
const unsigned int blocks = blockPtr->blocks; const unsigned int blocks = current->blocks;
blockPtr->next = list( blocks ); Block *&next = list( blocks );
list( blocks ) = blockPtr; current->next = next;
next = current;
} }
} }
}; };
......
...@@ -37,7 +37,7 @@ int main ( int argc, char **argv ) ...@@ -37,7 +37,7 @@ int main ( int argc, char **argv )
{ {
Timer timer; Timer timer;
const unsigned long iterations = 1 << 26; const unsigned long iterations = 1 << 30;
std :: cout << "Performing " << iterations << " iterations." << std :: endl; std :: cout << "Performing " << iterations << " iterations." << std :: endl;
timer.reset(); timer.reset();
......
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