C++;使用clang/llvm的精确垃圾收集器? < >好,所以我想在C++中编写一个精确的“标记扫描”垃圾回收器。我希望能做出一些有助于我的决定,因为我所有的指针都将被包装在一个“RelocObject”中,我将为堆提供一块内存。这看起来像这样: // 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(); }

C++;使用clang/llvm的精确垃圾收集器? < >好,所以我想在C++中编写一个精确的“标记扫描”垃圾回收器。我希望能做出一些有助于我的决定,因为我所有的指针都将被包装在一个“RelocObject”中,我将为堆提供一块内存。这看起来像这样: // 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(); },c++,garbage-collection,clang,C++,Garbage Collection,Clang,提前感谢您的帮助。每当实例化RelocObject时,它的地址可以与sizeof(*derivedRelocObject)一起记录在RelocObject所有权数据库中,该数据库将立即识别哪个Foo属于哪个FooB。你不需要用叮当声。另外,由于Foo将在FooB之后不久创建,因此您的所有权数据库系统可以非常简单,因为“我已创建,这是我的地址和大小”调用的顺序将直接在其拥有的RelocObject实例之前显示拥有的RelocObject记录 每个RelocObject都有一个所有权声明标志,在第一

提前感谢您的帮助。

每当实例化
RelocObject
时,它的地址可以与
sizeof(*derivedRelocObject)
一起记录在
RelocObject
所有权数据库中,该数据库将立即识别哪个
Foo
属于哪个
FooB
。你不需要用叮当声。另外,由于
Foo
将在
FooB
之后不久创建,因此您的所有权数据库系统可以非常简单,因为“我已创建,这是我的地址和大小”调用的顺序将直接在其拥有的
RelocObject
实例之前显示拥有的
RelocObject
记录

每个
RelocObject
都有一个
所有权声明
标志,在第一次使用时初始化为false(这将在构造函数完成之后,因为构造函数中不应执行任何实际工作),因此当第一次使用任何新创建的对象时,它会请求数据库更新其所有权,数据库通过记录的地址队列,可以识别哪些对象属于哪些对象,从列表中清除一些对象,将它们的
所有权声明
标志设置为true,您也可以获得偏移量(如果您仍然需要)



p、 如果您愿意,我可以与您分享我多年前编写的增量垃圾收集器的代码,您可能会觉得这很有用。

那么,您打算如何处理在一个变量中包含指针,在另一个变量中被int覆盖的联合?语言设计似乎阻止了“精确”识别指针的能力。。。。如果您要实现这样一个垃圾收集器,我希望您希望使用Clang来生成与GC相关的所有代码(例如,分配和解除分配[例如,在C++中无法摆脱析构函数]),并且它已经为语言的其余部分生成了代码。在Clang中,您应该可以访问此字段偏移量数据(我不是Clang专家,所以没有详细信息)。但听起来你想在叮当声之外做这些。为什么?@Ira:我不太在意像工会tbh这样的“很好拥有”功能。我能够约束用户的使用模式。本质上我有纯C++的模块(在那里他们可以做任何他们想要的)和被约束的模块——即它们不允许使用原始指针:当然他们可以去使用它们,但是如果他们使用的话,它可能会破坏事物。想想托管和非托管C++:两者都有优点和缺点。@Ira:我让我的Impl::类有私有的actor和dtor,但与垃圾收集器是朋友。我并不真的想要生成叮当声的代码,只需要关于我的类的信息。事实上,我不想在Clang之外做这件事。如果我给人这样的印象,我很抱歉:我只是想让Clang在编译期间将这些信息转储出去。也许我需要改写一下我的问题!首先感谢您的详细回复。我想沿着偏移路线走,因为我不想为类的每个实例存储指针的开销。我只想为每个类生成一个偏移量函数ie GetChildren(this)。不过,我对你如何保持所有权数据库的简单感兴趣,因为你肯定需要至少一个链接列表和一个嵌入其中的自由列表,因为随机创建/删除顺序(我不反对这很简单,顺便说一句,我只对你在组织数据库时可能会用到的任何技巧感兴趣)。@user176168对延迟的响应表示歉意。我创建了一个分区集结构(基本上是一个允许您向分区分配和重新分配对象的结构)。我有5个分区,白、黑、新、删除和灰色(IIRC)。
Reference
结构存储了两个引用。一个是在每个标记周期开始时冻结的参考,另一个是主动更新的参考。
class FooB
{
public:
    int m_a;
    Foo m_ptr;
};