具有较大对齐方式的Win32内存分配 [英] Win32 memory allocation with large alignment

查看:191
本文介绍了具有较大对齐方式的Win32内存分配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要分配具有较大对齐方式(也可能在兆字节范围内)的较大内存区域(兆字节). VirtualAlloc系列功能似乎没有提供执行此操作的选项.

I need to allocate large regions of memory (megabytes) with large alignments (also potentially in the megabyte range). The VirtualAlloc family of functions don't seem to provide options to do this.

在Linux上我要做的就是映射更大的区域-足够大以保证其中包含具有所需对齐方式的足够大的区域-然后在映射的开始和结尾处对区域进行映射.不需要的大区域.

What I do on Linux to achieve this is to mmap a larger region - large enough to guarantee that a sufficiently large region with the required alignment will be contained in it - and then munmap the regions at the beginning and the end of the large region that are not needed.

作为一个例子,假设我需要4兆字节,在1兆字节的边界上对齐(即,最低20位为零的区域的开始).我会映射5兆字节.假设我得到的区域是0x44ff000-0x49ff000.在该区域内包含区域0x4500000-0x4900000,该区域在1兆字节边界上对齐.然后,我将munmap 0x44ff000-0x4500000和0x4900000-0x49ff000.

As an example, let's say I need 4 megabytes, aligned on a 1 megabyte boundary (i.e. the start of the region having zeroes in the lowest 20 bits). I'd mmap 5 megabytes. Let's say I get the region 0x44ff000-0x49ff000. Within that region is contained the region 0x4500000-0x4900000, which is aligned on a 1 megabyte boundary. I would then munmap 0x44ff000-0x4500000 and 0x4900000-0x49ff000.

我可以在Win32上做类似的事情吗?如果我将VirtualProtect与PAGE_NOACCESS一起使用,是否可以释放内存?有更好的解决方案吗?

Can I do something similar on Win32? If I use VirtualProtect with PAGE_NOACCESS, will the memory be freed? Is there a better solution?

推荐答案

是的,您可以使用相同的技术. VirtualAllocMEM_RESERVE一样大.找到适当对齐的子范围,然后使用MEM_COMMIT在子范围上再次调用VirtualAlloc.

Yes, you can use the same technique. VirtualAlloc a large range as MEM_RESERVE. Find the sub-range that is appropriately aligned and call VirtualAlloc a second time on the sub-range with MEM_COMMIT.

这篇关于具有较大对齐方式的Win32内存分配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆