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

Carsten Gräser graeser at math.fu-berlin.de
Wed Apr 7 14:13:00 CEST 2010


Hi,
the implementation seems correct, since no constructors or
destructors are called. allocate() and deallocate() use the
global operators new() and delete().

'operator new(size_t n)' returns a void* and 'operator delete(void* p)'
takes a void* argument, hence the pointer is converted to 'void*'
and thus no destructor of T is called.

While it is possible to replace these global functions with
void* result/argument it is not allowed to overload them
for a different type. Hence 'operator delete(T* p)' will
never exist and the conversion to void* is always guaranteed.

In fact the standard even says that the default allocator calls
'operator delete(void*)'

Carsten


Am 07.04.2010 13:58, schrieb Oliver Sander:
> Please ignore this email.  Thanks!  Oliver
> 
> Oliver Sander schrieb:
>> 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
>>>>
>>>>
>>>
>>
>>
> 
> 


-- 
----------------------------------------------------------------------
Carsten Gräser           | phone: +49-30 / 838-75349
Freie Universität Berlin | fax  : +49-30 / 838-54977
Institut für Mathematik  | email: graeser at math.fu-berlin.de
Arnimallee 6             |
14195 Berlin, Germany    | URL  : http://page.mi.fu-berlin.de/graeser
----------------------------------------------------------------------




More information about the Dune mailing list