如何使用一个 VirtualFree 调用删除多个相邻的虚拟内存分配?

How to delete several adjacent virtual memory allocations with one VirtualFree call?

本文关键字:删除 分配 虚拟内存 调用 何使用 VirtualFree 一个      更新时间:2023-10-16

下面是一个简短的测试代码示例来说明我的问题:

SYSTEM_INFO si;
GetSystemInfo(&si);
//Go by allocation granularity to have a chance to reserve adjacent memory chunks
size_t szAlloc = si.dwAllocationGranularity;
//Try to alloc two adjacent memory chunks
BYTE* pp1 = (BYTE *)::VirtualAlloc(NULL, szAlloc, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
ASSERT(pp1);
BYTE* pp2 = (BYTE *)::VirtualAlloc(pp1 + szAlloc, szAlloc, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
ASSERT(pp2);
//We should be able to write between them
WORD ui = 0xAB;
BYTE* p = pp1 + szAlloc - sizeof(ui) / 2;
*(WORD*)p = ui;
//Release both at once
BOOL bb = ::VirtualFree(pp1, szAlloc + szAlloc, MEM_RELEASE);
int err = ::GetLastError();

如何通过一次调用释放两个内存块?或者将它们合并成一个块,如果不允许一次释放?(我问的原因是在我的实际代码中,可以不止 2 个相邻的块。

附言。在上面的测试代码中,VirtualFree失败,错误代码ERROR_INVALID_ADDRESS

你不能完全按照自己的意愿去做。虽然我不确定你为什么要这样做 - 如果您需要释放多个分配,请分别释放它们。

但是,我认为您可以通过保留单个大内存(通过auto ptr = VirtualAlloc(nullptr, ..., MEM_RESERVE, ...);)来部分实现您想要的,然后在您想要通过VirtualAlloc((uint_t*)ptr+offset, ..., MEM_COMMIT, ...);时分段提交较大分配的位。

如果您这样做,那么您可以通过一次调用VirtualFree(ptr, 0, MEM_RELEASE)释放整个大型预留(包括您单独提交的所有部分)