Windows 和 Windows 上的内存页写入检测Linux

发布于 2024-12-09 09:36:39 字数 763 浏览 1 评论 0原文

我目前正在研究一种分代垃圾收集器。这意味着只有最近的对象才会被遍历,幸存的对象(=从已知根可到达)被提升到老一代。当对象指向同一代或老一代的其他对象时,这可以正常工作。然而,当较旧的对象指向较新的对象时,并且由于仅遍历较新的对象,因此所指向的对象将被错误地收集。为了避免这种情况,在每个 GC 阶段都会显式地标记和遍历此类对象。

显然,这样的“父”对象是可变的,因为通过构造,不可变的对象总是指向现有对象。因此,要成为“父对象”,对象在提升后必须进行修改,以便它指向更新的对象。

为了知道老一代的哪些对象指向年轻一代,我正在寻找一种透明地监视内存变化的方法。为此,我使用内存保护和信号/异常处理。内存页设置为只读,这会导致每当写入时都会引发信号/异常,在这种情况下,我将内存保护设置回读写并将地址记录在某处以进行进一步处理,并在返回负责的代码时异常恢复正常。这样,当 GC 触发时,我就知道去哪里寻找潜在的父级来遍历。

在 Linux 上,我使用 mprotect/SIGSEGV 信号处理的组合。在 Windows 上,我打算使用 VirtualProtect,但发现没有与 SIGSEGV 处理等效的方法。所以我的问题是:

  1. 你会如何在 Windows 上做到这一点?异常处理 API 看起来相当混乱。

  2. 有没有更好的方法来知道哪些内存区域被修改,这样我就不必做所有这些簿记工作?

我的代码是用纯 C 语言编写的。目前,我需要调用者代码显式标记已修改的对象,但这很乏味且容易出错,因此我正在寻找一种透明的方法来实现这一点。

预先感谢,弗雷德

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, and 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

黯然 2024-12-16 09:36:40

GetWriteWatch 对此来说是理想
文档此处

GetWriteWatch is ideal for this.
Documentation here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文