使大量分配在第一次接触时自动提交
如何在 Windows 中分配内存,但在第一次触摸之前才提交内存?
我看到 VirtualAlloc 允许我保留一定范围的内存,但随后我需要在使用前手动提交该内存的部分。我希望当我第一次引用它时自动提交。
另外,如果可能的话,我不希望在提交之前将内存清零。
(顺便说一句,这可以在 Linux 上通过设置 /dev/zero 块的私有内存映射来完成)
How can I allocate memory in Windows but not have it committed until first touch?
I see that VirtualAlloc
lets me reserve a range of memory, but I then need to manually commit sections of that memory before use. I'd like the committing to be automatic when I first reference it.
Also, if possible, I do not want the memory zeroed before being committed.
(As an aside, this can be done on Linux by setting up a private memory mapping of a chunk of /dev/zero)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用自己设计的智能指针,该指针在第一次取消引用时提交它(当然通过重载取消引用和间接运算符)。
这是一个示例智能指针,它在第一次取消引用(延迟实例化)时创建给定对象的实例。同样的想法,基本上:
You can use a smart-pointer of your own design which commits it upon first dereference (by of course overloading the dereference & indirection operators).
This is an example smart pointer which creates an instance of a given object upon first dereference (lazy instantiation). Same idea, basically:
据推测,延迟分配地址支持是目标,而不是在第一次使用时专门承诺?如果是这样,Windows 似乎会自动为您执行此操作!
http://msdn.microsoft.com/en- us/library/aa366803(v=vs.85).aspx
相关引用:
至于新页面被重置为零,我相信这是一个安全功能,无法避免。 (系统有一个特殊的线程,可以在出现死区时间时将页面归零,因此如果幸运的话,总会有一个归零的页面立即可供在必要时使用。)
Presumably the deferred allocation of backing for the addresses is the goal, rather than necessarily specifically committing on first use? If so, it looks as if Windows will do that for you automatically!
http://msdn.microsoft.com/en-us/library/aa366803(v=vs.85).aspx
Relevant-looking quotation:
As for new pages being reset to zero, I believe this is a security feature and can't be avoided. (The system has a special thread that zeros pages when there is some dead time, so with any luck there will always be a zeroed page immediately ready for use when necessary.)