if (!size || !addr)
return;
- // Make sure that this API is only used to unmap an entire previous mapping.
- // Windows cannot unmap part of a previous mapping. Unfortunately,
- // we can't check that size matches the original size because mbi.RegionSize
- // doesn't describe the size of the full allocation if some of the pages were
- // protected.
MEMORY_BASIC_INFORMATION mbi;
CHECK(VirtualQuery(addr, &mbi, sizeof(mbi)));
- CHECK(mbi.AllocationBase == addr &&
- "Windows cannot unmap part of a previous mapping");
+ // MEM_RELEASE can only be used to unmap whole regions previously mapped with
+ // VirtualAlloc. So we first try MEM_RELEASE since it is better, and if that
+ // fails try MEM_DECOMMIT.
if (VirtualFree(addr, 0, MEM_RELEASE) == 0) {
- Report("ERROR: %s failed to "
- "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
- SanitizerToolName, size, size, addr, GetLastError());
- CHECK("unable to unmap" && 0);
+ if (VirtualFree(addr, size, MEM_DECOMMIT) == 0) {
+ Report("ERROR: %s failed to "
+ "deallocate 0x%zx (%zd) bytes at address %p (error code: %d)\n",
+ SanitizerToolName, size, size, addr, GetLastError());
+ CHECK("unable to unmap" && 0);
+ }
}
}