简体   繁体   English

使用clang / llvm的C ++精确垃圾收集器?

[英]C++ precise garbage collector using clang/llvm?

Ok so I'm wanting to write a precise 'mark and sweep' garbage collector in C++. 好的,我想在C ++中编写一个精确的'标记和扫描'垃圾收集器。 I have hopefully made some decisions that can help me as in all my pointers will be wrapped in a 'RelocObject' and I'll have a single block of memory for the heap. 我希望做出一些可以帮助我的决定,因为我的所有指针都将包含在'RelocObject'中,并且我将为堆提供单个内存块。 This looks something like this: 这看起来像这样:

// This class acts as an indirection to the actual object in memory so that it can be      
// relocated in the sweep phase of garbage collector
class MemBlock
{
public:
    void* Get( void ) { return m_ptr; }

private:
    MemBlock( void ) : m_ptr( NULL ){}

    void* m_ptr;
};

// This is of the same size as the above class and is directly cast to it, but is     
// typed so that we can easily debug the underlying object
template<typename _Type_>
class TypedBlock
{
public:
    _Type_* Get( void ) { return m_pObject; }

private:
    TypedBlock( void ) : m_pObject( NULL ){}

    // Pointer to actual object in memory
    _Type_* m_pObject;
};

// This is our wrapper class that every pointer is wrapped in 
template< typename _Type_ >
class RelocObject
{
public:

    RelocObject( void ) : m_pRef( NULL ) {}

    static RelocObject New( void )
    {
        RelocObject ref( (TypedBlock<_Type_>*)Allocator()->Alloc( this, sizeof(_Type_), __alignof(_Type_) ) );
        new ( ref.m_pRef->Get() ) _Type_();
        return ref;
    }

    ~RelocObject(){}

    _Type_*     operator->  ( void ) const 
    { 
        assert( m_pRef && "ERROR! Object is null\n" ); 
        return (_Type_*)m_pRef->Get(); 
    }

    // Equality
    bool operator ==(const RelocObject& rhs) const { return m_pRef->Get() == rhs.m_pRef->Get(); }
    bool operator !=(const RelocObject& rhs) const { return m_pRef->Get() != rhs.m_pRef->Get(); }

    RelocObject&    operator=   ( const RelocObject& rhs ) 
    {
        if(this == &rhs) return *this;
        m_pRef = rhs.m_pRef;
        return *this; 
    }

private:

    RelocObject( TypedBlock<_Type_>* pRef ) : m_pRef( pRef ) 
    {
        assert( m_pRef && "ERROR! Can't construct a null object\n");
    }

    RelocObject*    operator&   ( void ) { return this; }
    _Type_&     operator*   ( void ) const { return *(_Type_*)m_pRef->Get(); }

    // SS: 
    TypedBlock<_Type_>* m_pRef;
};

// We would use it like so...
typedef RelocObject<Impl::Foo> Foo;

void main( void )
{
    Foo foo = Foo::New();
}

So in order to find the 'root' RelocObjects when I allocate in 'RelocObject::New' I pass in the 'this' pointer of the RelocObject into the allocator(garbage collector). 因此,当我在'RelocObject :: New'中分配时,为了找到'root'RelocObjects,我将RelocObject的'this'指针传递给allocator(垃圾收集器)。 The allocator then checks to see if the 'this' pointer is in the range of the memory block for the heap and if it is then I can assume its not a root. 然后,分配器检查“this”指针是否在堆的内存块范围内,如果是,那么我可以假设它不是根。

So the issue comes when I want to trace from the roots through the child objects using the zero or more RelocObjects located inside each child object. 因此,当我想使用位于每个子对象内的零个或多个RelocObjects从子对象跟踪子对象时,问题就出现了。

I want to find the RelocObjects in a class (ie a child object) using a 'precise' method. 我想使用'精确'方法在类(即子对象)中找到RelocObjects。 I could use a reflection approach and make the user Register where in each class his or her RelocObjects are. 我可以使用反射方法并使用户在每个类中注册他或她的RelocObjects。 However this would be very error prone and so I'd like to do this automatically. 然而,这将非常容易出错,因此我想自动执行此操作。

So instead I'm looking to use Clang to find the offsets of the RelocObjects within the classes at compile time and then load this information at program start and use this in the mark phase of the garbage collector to trace through and mark the child objects. 因此,我希望在编译时使用Clang在类中找到RelocObjects的偏移量,然后在程序启动时加载此信息,并在垃圾收集器的标记阶段使用它来跟踪并标记子对象。

So my question is can Clang help? 所以我的问题是Clang能帮忙吗? I've heard you can gather all kinds of type information during compilation using its compile time hooks. 我听说你可以使用编译时钩子在编译期间收集各种类型的信息。 If so what should I look for in Clang ie are there any examples of doing this kind of thing? 如果是这样,我应该在Clang中寻找什么,即有没有做这种事情的例子?

Just to be explicit: I want to use Clang to automatically find the offset of 'Foo' (which is a typedef of RelocObject) in FooB without the user providing any 'hints' ie they just write: 只是为了明确:我想使用Clang在FooB中自动找到'Foo'的偏移量(这是RelocObject的typedef)而用户没有提供任何“提示”,即他们只是写:

class FooB
{
public:
    int m_a;
    Foo m_ptr;
};

Thanks in advance for any help. 在此先感谢您的帮助。

Whenever a RelocObject is instantiated, it's address can be recorded in a RelocObject ownership database along with sizeof(*derivedRelocObject) which will immediately identify which Foo belongs to which FooB . 每当RelocObject被实例化时,它的地址可以与sizeof(*derivedRelocObject)一起记录在RelocObject所有权数据库中,这将立即识别哪个Foo属于哪个FooB You don't need Clang for that. 你不需要Clang。 Also since Foo will be created shortly after FooB , your ownership database system can be very simple as the order of "I've been created, here's my address and size" calls will show the owning RelocObject record directly before the RelocObject instance's that it owns. 此外,由于Foo将在FooB之后不久创建,因此您的所有权数据库系统可以非常简单,因为“我已经创建,这里是我的地址和大小”调用的RelocObject将直接在它拥有的RelocObject实例之前显示拥有的RelocObject记录。

Each RelocObject has a ownership_been_declared flag initialized as false, upon first use (which would be after the constructors have completed, since no real work should be done in the constructor), so when any of those newly created objects is first used it requests that the database update it's ownership, the database goes through it's queue of recorded addresses and can identify which objects belong to which, clear some from it's list, setting their ownership_been_declared flag to true and you will have the offsets too (if you still need them). 每个RelocObjectownership_been_declared标志初始化为假,在第一次使用(这将是后构造函数已完成,因为没有真正的工作应该在构造函数中完成),所以当其中任何新创建的对象的第一次使用它请求数据库更新它的所有权,数据库通过它的记录地址队列,并可以识别哪些对象属于哪个,清除它的列表中的一些,将他们的ownership_been_declared标志设置为true,你也将有偏移(如果你仍然需要它们)。


ps if you like I can share my code for an Incremental Garbage Collector I wrote many years ago, which you might find helpful. ps如果你愿意我可以分享我多年前写的增量垃圾收集器的代码,你可能会发现它很有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM