[Dune] [Dune-Commit] dune-common r5954 - trunk/dune/common

Oliver Sander sander at mi.fu-berlin.de
Wed Apr 7 13:56:50 CEST 2010


Interesting. My gcc's implementation is basically

// Define new_allocator as the base class to std::allocator.
#include <ext/new_allocator.h>
#define __glibcxx_base_allocator  __gnu_cxx::new_allocator

  template<typename _Tp>
    class allocator: public __glibcxx_base_allocator<_Tp>
 {...};

Not much to learn from that...

--
Oliver

P.S.:  My gcc is g++ (Debian 4.4.2-9) 4.4.3 20100108 (prerelease)


Martin Nolte schrieb:
> 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
>>
>>
>


-- 
************************************************************************
* Oliver Sander                ** email: sander at mi.fu-berlin.de        *
* Freie Universität Berlin     ** phone: + 49 (30) 838 75348           *
* Institut für Mathematik      ** URL  : page.mi.fu-berlin.de/~sander  *
* Arnimallee 6                 ** -------------------------------------*
* 14195 Berlin, Germany        ** Member of MATHEON (www.matheon.de)   *
************************************************************************





More information about the Dune mailing list