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

bring back smallobject.hh (for my personal use only)

[[Imported from SVN: r6902]]
parent 4d35beaa
No related branches found
No related tags found
No related merge requests found
......@@ -69,6 +69,7 @@ commoninclude_HEADERS = \
reservedvector.hh \
shared_ptr.hh \
singleton.hh \
smallobject.hh \
static_assert.hh \
stdstreams.hh \
timer.hh \
......
// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
// vi: set et ts=4 sw=2 sts=2:
#ifndef DUNE_SMALLOBJECT_HH
#define DUNE_SMALLOBJECT_HH
#include <cassert>
#include <memory>
#include <new>
namespace Dune
{
// SmallObjectPool
// ---------------
class SmallObjectPool
{
union Block
{
Block *next;
unsigned int blocks;
};
public:
enum { blockSize = sizeof( Block ) };
enum { maxBlocks = (1 << 10) - 1 };
enum { maxSize = maxBlocks * blockSize };
private:
Block *list_[ maxBlocks ];
SmallObjectPool ()
{
for( unsigned int i = 0; i < maxBlocks; ++i )
list_[ i ] = 0;
}
~SmallObjectPool ()
{
for( unsigned int i = 0; i < maxBlocks; ++i )
{
for( Block *next = list_[ i ]; next != 0; )
{
Block *current = next;
next = current->next;
delete[] current;
}
}
}
static SmallObjectPool &instance ()
{
static SmallObjectPool inst;
return inst;
}
static Block *&list ( unsigned int blocks )
{
assert( blocks < maxBlocks );
return instance().list_[ blocks ];
}
public:
static void *allocate ( unsigned int size )
{
const unsigned int blocks = (size + (blockSize-1)) / blockSize;
if( blocks >= maxBlocks )
return 0;
Block *&next = list( blocks );
Block *current = next;
if( current != 0 )
next = current->next;
else
current = new Block[ blocks+1 ];
current->blocks = blocks;
return current + 1;
}
static void free ( void *p )
{
if( p != 0 )
{
Block *current = reinterpret_cast< Block * >( p ) - 1;
const unsigned int blocks = current->blocks;
Block *&next = list( blocks );
current->next = next;
next = current;
}
}
};
// SmallObject
// -----------
struct SmallObject
{
void *operator new ( size_t size )
{
return SmallObjectPool::allocate( size );
}
void operator delete ( void *p )
{
SmallObjectPool::free( p );
}
};
// SmallObjectAllocator
// --------------------
template< class T >
struct SmallObjectAllocator;
template<>
struct SmallObjectAllocator< void >
{
typedef void value_type;
typedef void *pointer;
typedef const void *const_pointer;
template< class U > struct rebind { typedef SmallObjectAllocator< U > other; };
};
template< class T >
struct SmallObjectAllocator
{
typedef T value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
typedef T *pointer;
typedef const T *const_pointer;
typedef T &reference;
typedef const T &const_reference;
template< class U > struct rebind { typedef SmallObjectAllocator< U > other; };
SmallObjectAllocator () throw () {}
template< class U > SmallObjectAllocator ( const SmallObjectAllocator< U > & ) throw () {}
~SmallObjectAllocator () throw () {}
pointer address ( reference r ) const { return &r; }
const_pointer address ( const_reference r ) const { return &r; }
pointer allocate ( size_type n, SmallObjectAllocator< void >::const_pointer hint = 0 );
void deallocate ( pointer p, size_type n ) { SmallObjectPool::free( p ); }
void construct ( pointer p, const T &value ) { new( p ) T( value ); }
void destroy ( pointer p ) { p->~T(); }
size_type max_size () const throw () { return (SmallObjectPool::maxSize / sizeof( T )); }
};
// Implementation of SmallObjectAllocator
// --------------------------------------
template< class T >
inline typename SmallObjectAllocator< T >::pointer
SmallObjectAllocator< T >::allocate ( size_type n, SmallObjectAllocator< void >::const_pointer hint )
{
return static_cast< pointer >( SmallObjectPool::allocate( n * sizeof( T ) ) );
}
template< class T >
bool operator== ( const SmallObjectAllocator< T > &, const SmallObjectAllocator< T > & ) throw()
{
return true;
}
template< class T >
bool operator!= ( const SmallObjectAllocator< T > &, const SmallObjectAllocator< T > & ) throw()
{
return false;
}
} // namespace Dune
#endif // #ifndef DUNE_SMALLOBJECT_HH
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