简体   繁体   English

Boehm 垃圾收集器中的精确模式

[英]Precise mode in Boehm Garbage Collector

I've read on the webpage of Mono that they are using the Boehm GC in precise mode.我在 Mono 的网页上读到他们在精确模式下使用 Boehm GC。 I too use the Boehm GC with C++, however, I have found nothing in its documentation or headers that would indicate a precise mode, much less how to turn it on.我也将 Boehm GC 与 C++ 一起使用,但是,我在其文档或标题中没有发现任何表明精确模式的内容,更不用说如何打开它了。

Any information whether it actually has a precise mode by default and how to turn it on, or it was just some kind of modification by Mono developers?任何信息是否它实际上具有默认的精确模式以及如何打开它,或者它只是 Mono 开发人员的某种修改?

Precise mode in Boehm GC under Mono isn't just GC_MALLOC_ATOMIC . Mono 下的 Boehm GC 中的精确模式不仅仅是GC_MALLOC_ATOMIC It's only true for arrays of fundamental types.这仅适用于基本类型的 arrays。

For managed types, GC_gcj_malloc is used.对于托管类型,使用GC_gcj_malloc Mono's compiler generates an object descriptor for every managed type and it then simply calls GC_gcj_malloc with an argument of size, and a pointer to the managed type's descriptor. Mono 的编译器为每个托管类型生成一个 object 描述符,然后它简单地使用大小参数调用GC_gcj_malloc和指向托管类型描述符的指针。 Boehm GC then refers to the descriptor during mark phase to trace the managed pointers. Boehm GC 然后在标记阶段引用描述符以跟踪托管指针。

You will end up with just the root pointers sitting on the stack as raw pointers ( GC_gcj_malloc returns a void* and there's no way to tell the GC where the pointers are on the stack via some sort of a stack descriptor prior to GC collect).您最终只会将根指针作为原始指针放在堆栈上( GC_gcj_malloc返回一个 void* 并且无法通过 GC 收集之前的某种堆栈描述符告诉 GC 指针在堆栈上的位置)。 This is the reason Mono (prior to SGen) says they scan the stack in conservative mode.这就是 Mono(在 SGen 之前)说他们以保守模式扫描堆栈的原因。

If you want to implement this under C++, you won't be able to simply rely on the C++ compiler to generate the object descriptor for you.如果您想在 C++ 下实现此功能,您将无法简单地依赖 C++ 编译器为您生成 object 描述符。 What I envisioned a long time ago was to write an intermediate compiler that parses all your C++ header files for class definitions that have been marked as managed class (eg _ref class MyManagedObject where _ref is simply a #define to nothing) and generate a header file containing those object descriptors. What I envisioned a long time ago was to write an intermediate compiler that parses all your C++ header files for class definitions that have been marked as managed class (eg _ref class MyManagedObject where _ref is simply a #define to nothing) and generate a header file包含那些 object 描述符。 You would then use the GC_make_descriptor and GC_malloc_explicitly_typed functions to allocate your objects in precise mode rather than GC_gcj_malloc as you would not have control over how your C++ compiler allocates its vtable.然后,您将使用GC_make_descriptorGC_malloc_explicitly_typed函数以精确模式而不是GC_gcj_malloc分配对象,因为您无法控制 C++ 编译器如何分配其 vtable。

*EDIT: See Managed C++ for GCC (open source GPL v3) . *编辑:有关GCC(开源 GPL v3),请参阅托管 C++

The file doc/gcinterface.html from the garbage collector ( archive here ) states:来自垃圾收集器(此处存档)的文件 doc/gcinterface.html 指出:

void * GC_MALLOC_ATOMIC(size_t nbytes) Allocates nbytes of storage. void * GC_MALLOC_ATOMIC(size_t nbytes) 分配 nbytes 的存储空间。 Requires (amortized) time proportional to nbytes.需要与 nbytes 成比例的(摊销)时间。 The resulting object will be automatically deallocated when unreferenced.生成的 object 将在未引用时自动释放。 The client promises that the resulting object will never contain any pointers.客户端承诺生成的 object 永远不会包含任何指针。 The memory is not cleared. memory 未被清除。 This is the preferred way to allocate strings, floating point arrays, bitmaps, etc. More precise information about pointer locations can be communicated to the collector using the interface in gc_typed.h in the distribution.这是分配字符串、浮点 arrays、位图等的首选方式。有关指针位置的更精确信息可以使用分发中的 gc_typed.h 中的接口传达给收集器。

It looks like there is a "precise" interface that can be used.看起来有一个可以使用的“精确”界面。

I believe the precise mode needs support from the compiler to indicate exactly where pointers are stored.我相信精确模式需要编译器的支持才能准确指示指针的存储位置。 Typecasting in C and C++ makes this next to impossible. C 和 C++ 中的类型转换使得这几乎是不可能的。

A managed language, with built in reflection, would make this a lot easier.具有内置反射的托管语言将使这变得容易得多。

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

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