开发者

Memory page write detection on Windows & Linux

开发者 https://www.devze.com 2023-04-12 00:57 出处:网络
I\'m currently working on a generational garbage collector. This means that only the most recent objects get traversed, surviving objects (= reachable from known roots) being promoted to the older gen

I'm currently working on a generational garbage collector. This means that only the most recent objects get traversed, surviving objects (= reachable from known roots) being promoted to the older generation. This works OK when objects point to other objects of the same or older generations. However, when older objects point to newer ones, a开发者_运维问答nd since only newer objects get traversed, the pointed objects would be incorrectly collected. To avoid this, such objects are marked and traversed explicitly during each GC phase.

Obviously such "parent" objects are mutable, since by construction immutable ones always point to existing objects. So to become a "parent", an object has to be modified after being promoted, so that it points to a newer object.

To know which objects of older generations point to younger generations, I am looking for a way to monitor memory changes transparently. To do so I use memory protection and signal/exception handling. Memory pages are set read-only, which causes a signal/exception to be raised whenever they are written, in which case I set the memory protection back to read-write and log the address somewhere for further processing, and upon return the code responsible for the exception resumes normally. That way, when the GC triggers, I know where to look to find potential parents to traverse.

On Linux I use a combination of mprotect/SIGSEGV signal handling. On Windows I intend to use VirtualProtect but found no equivalent of SIGSEGV handling. So my questions:

  1. How would you do that on Windows? The exception handling API seems rather confusing.

  2. Is there a better way to know which memory areas get modified, so that I don't have to do all this bookkeeping?

My code is written in plain C. At present I require caller code to explicitly mark modified objects, but this is tedious and error-prone so I'm looking for a transparent way to do it.

Thanks in advance, Fred


GetWriteWatch is ideal for this. Documentation here

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号