[Dune] [Dune-Commit] dune-common r5954 - trunk/dune/common
Martin Nolte
nolte at mathematik.uni-freiburg.de
Wed Apr 7 13:40:34 CEST 2010
Hi Oli,
actually I copied this from GCC's implementation of the standard allocator.
Don't ask me how it works, though...
Yours,
Martin
On 04/07/2010 11:58 AM, Oliver Sander wrote:
> Hi Martin!
> Thanks for putting all this work into the allocator issue. From looking at
> the code below I see that you use new/delete to implement the allocator
> methods allocate/deallocate. This is not standard-conforming, as the stl
> methods allocate/deallocate only reserve/free memory, but do not call
> constructors/destructors. Hence malloc/free would be the appropriate
> way.
>
> Please consider this mail void if I am telling you things that you already
> know.
>
> best,
> Oliver
>
> mnolte at dune-project.org schrieb:
>> Author: mnolte
>> Date: 2010-04-07 09:36:52 +0200 (Wed, 07 Apr 2010)
>> New Revision: 5954
>>
>> Added:
>> trunk/dune/common/polyallocator.hh
>> Modified:
>> trunk/dune/common/Makefile.am
>> trunk/dune/common/smallobject.hh
>> Log:
>> add an allocator that can create object of arbitrary type (otherwise
>> similar
>> to STL)
>>
>> This will need some further discussion, of course. It is intended for
>> FS #766,
>> i.e., use an allocator to construct polymorphic objects in
>> GenericGeometries.
>>
>>
>>
>> Added: trunk/dune/common/polyallocator.hh
>> ===================================================================
>> --- trunk/dune/common/polyallocator.hh (rev 0)
>> +++ trunk/dune/common/polyallocator.hh 2010-04-07 07:36:52 UTC (rev 5954)
>> @@ -0,0 +1,36 @@
>> +#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
>>
>> Modified: trunk/dune/common/smallobject.hh
>> ===================================================================
>> --- trunk/dune/common/smallobject.hh 2010-04-06 07:04:52 UTC (rev 5953)
>> +++ trunk/dune/common/smallobject.hh 2010-04-07 07:36:52 UTC (rev 5954)
>> @@ -154,6 +154,20 @@
>>
>>
>>
>> + // 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
>> // --------------------------------------
>>
>> @@ -161,7 +175,7 @@
>> 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 ) ) );
>> }
>>
>>
>> @@ -186,6 +200,23 @@
>> 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
>>
>>
>> _______________________________________________
>> Dune-Commit mailing list
>> Dune-Commit at dune-project.org
>> http://lists.dune-project.org/mailman/listinfo/dune-commit
>
>
--
Martin Nolte <nolte at mathematik.uni-freiburg.de>
Universität Freiburg phone: +49-761-203-5642
Abteilung für angewandte Mathematik fax: +49-761-203-5632
Hermann-Herder-Straße 10
79104 Freiburg, Germany
More information about the Dune
mailing list