Re[2]: [Gc] Marking memory as traceable

Ivan Maidanski ivmai at mail.ru
Tue Apr 20 03:04:19 PDT 2010


Tue, 20 Apr 2010 11:42:56 +0200 Christian Gudrian <christian at gudrian.org>:

> Am 20.04.2010 10:25, schrieb Ivan Maidanski:
> > Another way, is to define a class which wraps a single traceable 
> > reference using GC_malloc_uncollectable(ptr_size)
> 
> Good suggestion!  That's what I did.  Here's what I've come up with in 
> case anyone is interested:
> 
> // ----------------------------------------------------------------------
> /**
>     Wrapper class for a pointer to garbage collected memory for the use
>     in areas which are not covered by the collector's static roots.

Better to say: in the areas which are not traced by the collector.

> */

#include "gc.h"

> template<class T>
> class TraceablePointer
> {
> public:
>     TraceablePointer( T * obj = 0 )
>     {
>        _o = (T**)GC_malloc_uncollectable( sizeof( T* ) );

It's preferred to use GC_MALLOC_UNCOLLECTABLE

>        *_o = obj;
>     }
> 
>     TraceablePointer( const TraceablePointer<T> & src )
>     {
>        _o = (T**)GC_malloc_uncollectable( sizeof( T* ) );
>        *_o = *src._o;
>     }
> 
>     ~TraceablePointer()
>     {
>        GC_free( _o );
>     }
> 
>     T * operator->() const
>     {
>        return *_o;
>     }
> 
>     void operator = ( T * rhs )
>     {
>        *_o = rhs;
>     }
> 
>     void operator = ( const TraceablePointer<T> & rhs )
>     {
>        *_o = *rhs._o;
>     }
> 
>     bool operator == ( const TraceablePointer<T> & rhs )const
>     {
>        return *_o == *rhs._o;
>     }
> 
>     bool operator == ( const T * rhs )const
>     {
>        return *_o == rhs;
>     }
> 
>     T * operator * () const
>     {
>        return *_o;
>     }
> 
>     operator T * () const
>     {
>        return *_o;
>     }

Sorry, I'm not too good in C++: could you explain me (or point to a spec) the difference between the last two operator definitions, please.

> 
> private:
>     T ** _o;
> };
> // ----------------------------------------------------------------------

Locks good, IMHO.
It's up to Hans whether we should have such thing in BDWGC or not.

> 
> Christian



More information about the Gc mailing list