[Gc] Marking memory as traceable

Christian Gudrian christian at gudrian.org
Tue Apr 20 02:42:56 PDT 2010


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.
*/
template<class T>
class TraceablePointer
{
public:
    TraceablePointer( T * obj = 0 )
    {
       _o = (T**)GC_malloc_uncollectable( sizeof( T* ) );
       *_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;
    }

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

Christian



More information about the Gc mailing list