如何解决在 Windows 中与 MAP_GROWSDOWN 等效的 mmap
在 Linux 中,我可以使用带有 MAP_GROWSDOWN
标志的 mmap 为自动增长的堆栈分配内存。引用联机帮助页,
MAP_GROWSDOWN
This flag is used for stacks. It indicates to the kernel
virtual memory system that the mapping should extend
downward in memory. The return address is one page lower
than the memory area that is actually created in the
process's virtual address space. Touching an address in
the "guard" page below the mapping will cause the mapping
to grow by a page. This growth can be repeated until the
mapping grows to within a page of the high end of the next
lower mapping,at which point touching the "guard" page
will result in a SIGSEGV signal.
在 Windows 中有一些等效的技术吗?甚至像要求操作系统通知您页面错误这样的丑陋事情,以便您可以在下面分配一个新页面(并通过要求操作系统摆弄页表使其看起来是连续的)?
解决方法
使用 VirtualAlloc 可以保留一块内存,提交前两页,并使用 PAGE_GUARD 设置两页中较低的页面。当堆栈向下增长并访问保护页时,会抛出一个结构化异常,您可以处理该异常以向下提交下一页并在其上设置 PAGE_GUARD
。
上面类似于Windows进程中堆栈的处理方式。下面是使用 Sysinternals VMMAP.EXE 的堆栈描述。您可以看到它是一个 256KB 的堆栈,其中 32K 已提交,12K 保护页,剩余 212K 保留堆栈。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。