如何使用一个 VirtualFree 调用删除多个相邻的虚拟内存分配?
How to delete several adjacent virtual memory allocations with one VirtualFree call?
下面是一个简短的测试代码示例来说明我的问题:
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)
释放整个大型预留(包括您单独提交的所有部分)
相关文章:
- 将数组的地址分配给变量并删除
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 在程序变得低效之前,允许多少新的[]和删除[]分配是否有限制?
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 如何在C++中为增加但记住删除先前对象的对象分配唯一标识符
- 删除分配的 (?) 指针时"Pointer being freed was not allocated"
- 如何在析构函数中删除C++中动态分配的数组?
- 无法删除布尔动态内存分配
- 为什么删除分配的阵列会导致内存错误?
- 使用卷曲支架从堆栈中删除分配
- 破坏者未删除分配的内存
- 有没有一种方法可以全局填充新分配/删除分配
- 函数返回后,如何删除分配的变量
- 如何删除分配了新的对象
- 将指针移动到其他位置后删除分配的内存
- 创建新的 CMFCDesktopAlertWnd 时,何时删除分配的内存
- 假设规则和删除分配
- 分配内存,并在删除分配之前定义NULL
- 删除分配给对象的所有内存
- 在删除分配的内存时调试断言失败