本文介绍了我怎么可以用C语言实现的跨preTED语言提供垃圾收集?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 如果我是执行收集用C间preTED语言垃圾,我怎么能去提供precise(即不保守)的垃圾收集不写我自己的垃圾收集器?是否有图书馆可用于此?如果是这样,哪一个?我明白我必须保持我的末尾被垃圾收集器跟踪的任何对象的某些变量。If I were to implement a garbage collected interpreted language in C, how can I go about providing precise (i.e. not conservative) garbage collection without writing my own garbage collector? Are there libraries available for this? If so, which ones? I understand that I would have to maintain certain invariants on my end for any objects tracked by the garbage collector.推荐答案如果你想有一个的 precise 的GC(而不是保守,喜欢的贝姆的GC ,它执行在实践中相当不错),你应该跟踪本地指针(与GC-ED数据)的变量,否则只能用调用GC当你确定没有这样的局部变量(顺便说一句,在 GCC编译器有这样一个几乎是空的调用堆栈标记和放大器;扫垃圾收集 - 由一些专门的 gengtype C ++ code发电机;这是GGC >通行证)。当然,你也应该跟踪全球(包括静态或线程本地)指针(以GC-ED数据)的变量也。If you want a precise GC (not a conservative one, like Boehm's GC, which performs quite well in practice) you should track local pointer (to GC-ed data) variables, or else invoke the GC only with a nearly empty call stack when you are sure there are no such local variables (btw, the GCC compiler has such a mark&sweep garbage collector - with marking routines generated by some specialized gengtype C++ code generator; that GGC is invoked only between passes). Of course you should also track global (including static, or thread local) pointer (to GC-ed data) variables also.另外,有一些字节code虚拟机(如 OCaml的或的 NekoVM 有),那么本地GC-ED变量是那些在堆栈和/或字节code VM的寄存器,你触发GC在你的虚拟机间preTER具体的和精心挑选的要点。 (请参见这个解释 ocaml的GC的)。Alternatively, have some bytecode virtual machine (like OCaml or NekoVM have), then the local GC-ed variables are those in the stacks and/or registers of your bytecode VM, and you trigger your GC at specific and carefully chosen points of your VM interpreter. (See this explanation of Ocaml GC).您应该阅读有关垃圾收集技术,看的 GC手​​册。You should read more about Garbage Collection techniques, see the GC handbook.如果您的GC是复制世代,你需要实现写屏障(处理旧数据指向新区域的突变)。你可以使用我的旧 Qish GC(我不保持多了),或 Ravenbrook的MPS ,或者自己写代复制GC(这并不难在理论上,但是在调试的GC在实践中是一个噩梦,所以它是一个大量的工作)。If your GC is copying generational, you need to implement write barriers (to handle mutation of old data pointing to new zone). You could use my old Qish GC (which I don't maintain much anymore), or Ravenbrook's MPS, or write your own generational copying GC (this is not that hard in theory, but debugging GCs is a nightmare in practice, so it is a lot of work).您可能需要使用一些技巧宏(就像我的Qish一样),以帮助保持你的局部变量。请参阅的融融垃圾收集 ocaml的文档为例部分(或往里Qish)。You probably want to use some macro tricks (like my Qish does) to help keeping your local variables. See the Living in harmony with the garbage collector section of Ocaml documentation as an example (or look inside Qish).注意,一个代复制GC并不友好,以处理手工编写C code(因为你需要明确地保持局部指针,因为你需要写屏障时要记住的旧值修改为有指针新一代)。如果你想这样做,你的C code应该在 A-正常形态(你不能code X = F(G(Y),Z); ,但你需要code 温度= G (Y); X = F(温度,Z); 并添加温度作为一个局部变量,假定 X ,是,以Z 是本地GC-ED变量和两个˚F和先按g 返回GC-ED指针)。在实践中容易得多,产生C code。看到我的 MELT 领域特定语言(扩展和定制的 GCC )为例。Notice that a generational copying GC is not friendly to handle in manually written C code (because you need to explicitly keep local pointers, and because you need a write barrier to remember when an old value is modified to have a pointer to the new generation). If you want to do that, your C code should be in A-normal form (you cannot code x=f(g(y),z); but you need to code temp=g(y); x=f(temp,z); and add temp as a local variable, assuming that x, y, z are local GC-ed variables and that both f and g return a GC-ed pointer). In practice it is much easier to generate the C code. See my MELT domain specific language (to extend and customize GCC) as an example.如果你的语言是真正的多线程(数增变线程并行分配),然后编写一个GC变得相当棘手。这可能需要工作几个月(而可能是一场噩梦调试)。If your language is genuinely multithreaded (several mutator threads allocating in parallel), then coding a GC becomes quite tricky. It might take several months of work (and is probably a nightmare to debug).其实,我今天会建议使用伯姆的GC(请注意,这是多线程友好)。一个天真的标志和放大器;手扫codeD GC可能不会超过伯姆的GC更快。而你将无法(我不推荐)使用GGC,垃圾收集器内部GCC(其中,IMNSHO,不是很好;很多年前,这是一个肮脏的黑客设计)。Actually, I would today recommend using Boehm's GC (notice that it is multithread friendly). A naive mark&sweep handcoded GC would probably not be faster than Boehm's GC. And you won't be able (and I don't recommend) to use GGC, the garbage collector internal to GCC (which, IMNSHO, is not very good; it was a dirty hack design many years ago).顺便说一句,你可能会考虑的定制的-e.g.与 MELT - GCC编译器(通过添加一些特定应用 __ __属性或的#pragma ),以帮助您的GC。一段时间的努力,你可能会产生一些标记例程,等等。然而,这种做法可能是相当痛苦的(我真的不知道)。注意MELT(自由软件,GPL第三版+)包含了复制代GC的老一代是GGC堆,所以你可以至少的的code melt-runtime.cc BTW, you might consider customizing -e.g. with MELT- the GCC compiler (by adding some application specific __attribute__ or #pragma) to help your GC. With some work, you could generate some of the marking routines, etc. However, that approach might be quite painful (I really don't know). Notice that MELT (free software, GPLv3+) contains a copying generational GC whose old generation is the GGC heap, so you could at least look inside the code of melt-runtime.cc PS。我还建议Queinnec的书: Lisp的小块;它拥有约GC和他们的编程语言方面,一些有趣的材料,这是一个伟大的书,当你正在实施一个跨preTER阅读。斯科特的书编程语言语用学也值得一读。PS. I also recommend Queinnec's book: Lisp In Small Pieces; it has some interesting material about GC and their connection to programming languages, and it is a great book to read when you are implementing an interpreter. Scott's book on Programming Languages Pragmatics is also worth reading. 这篇关于我怎么可以用C语言实现的跨preTED语言提供垃圾收集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-27 16:13