diff --git a/dune/common/Makefile.am b/dune/common/Makefile.am index 0ddb2f6a1d908ca526e8c02083388d28abf70c1e..95b0e47c6204e0d5e6cec1f1d5afd0b46024f26c 100644 --- a/dune/common/Makefile.am +++ b/dune/common/Makefile.am @@ -23,7 +23,7 @@ commoninclude_HEADERS = alignment.hh array.hh \ static_assert.hh smallobject.hh version.hh \ float_cmp.cc float_cmp.hh nullptr.hh \ forloop.hh function.hh interfaces.hh \ - gmpfield.hh mpiguard.hh + gmpfield.hh mpiguard.hh polyallocator.hh if EXPRESSIONTEMPLATES commoninclude_HEADERS += exprtmpl.hh exprtmpl/scalar.inc exprtmpl/exprexpr.inc diff --git a/dune/common/polyallocator.hh b/dune/common/polyallocator.hh new file mode 100644 index 0000000000000000000000000000000000000000..f8c72b221ad5d3ad36653aed40ad80cc92cf088c --- /dev/null +++ b/dune/common/polyallocator.hh @@ -0,0 +1,38 @@ +// -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- +// vi: set et ts=4 sw=2 sts=2: +#ifndef DUNE_POLYALLOCATOR_HH +#define DUNE_POLYALLOCATOR_HH + +namespace Dune +{ + + // PolyAllocator + // ------------- + + struct PolyAllocator + { + template< class T > + T *allocate ( size_t n = 1 ) + { + return static_cast< T * >( operator new( n * sizeof( T ) ) ); + } + + template< class T > void deallocate ( T *p ) + { + operator delete( p ); + } + + template< class T > void construct ( T *p, const T &value ) + { + new( p ) T( value ); + } + + template< class T > void destroy ( T *p ) + { + p->~T(); + } + }; + +} + +#endif // #ifndef DUNE_POLYALLOCATOR_HH diff --git a/dune/common/smallobject.hh b/dune/common/smallobject.hh index 3f4d0b33cb7fb01aa74c2bc3fe6452812833be4d..a71d445405890e9fe8bd9a39b714fed00b189610 100644 --- a/dune/common/smallobject.hh +++ b/dune/common/smallobject.hh @@ -156,6 +156,20 @@ namespace Dune + // SmallObjectPolyAllocator + // ------------------------ + + struct SmallObjectPolyAllocator + { + template< class T > T *allocate ( size_t n = 1 ); + template< class T > void deallocate ( T *p ); + + template< class T > void construct ( T *p, const T &value ) { new( p ) T( value ); } + template< class T > void destroy ( T *p ) { p->~T(); } + }; + + + // Implementation of SmallObjectAllocator // -------------------------------------- @@ -163,7 +177,7 @@ namespace Dune inline typename SmallObjectAllocator< T >::pointer SmallObjectAllocator< T >::allocate ( size_type n, SmallObjectAllocator< void >::const_pointer hint ) { - return reinterpret_cast< pointer >( SmallObjectPool::allocate( n * sizeof( T ) ) ); + return static_cast< pointer >( SmallObjectPool::allocate( n * sizeof( T ) ) ); } @@ -188,6 +202,23 @@ namespace Dune return false; } + + + // Implementation of SmallObjectPolyAllocator + // ------------------------------------------ + + template< class T > + inline T *SmallObjectPolyAllocator::allocate ( size_t n ) + { + return static_cast< T * >( SmallObjectPool::allocate( n * sizeof( T ) ) ); + } + + template< class T > + inline void SmallObjectPolyAllocator::deallocate ( T *p ) + { + SmallObjectPool::free( p ); + } + } #endif // #ifndef DUNE_SMALLOBJECT_HH