Implemented Memory::DontNeed on Windows, pages are reset and removed from working set. (#471)

This commit is contained in:
Branimir Karadžić
2026-10-21 17:37:35 -07:00
committed by GitHub
parent 97aa6da7db
commit 49c7d6050f
2 changed files with 30 additions and 1 deletions

View File

@@ -719,7 +719,12 @@ namespace bx
}
}
BX_UNUSED(advise);
if (Memory::DontNeed == advise)
{
VirtualAlloc(_address, _size, MEM_RESET, PAGE_NOACCESS);
VirtualUnlock(_address, _size);
}
return _address;
#else
BX_UNUSED(_address, _size, _alignment, _flags, state, protect, advise, page, granularity);

View File

@@ -132,6 +132,30 @@ TEST_CASE("memoryMap-protection-change", "[os]")
REQUIRE(err.isOk() );
}
TEST_CASE("memoryMap-dont-need", "[os]")
{
const size_t size = 16*bx::memoryPageSize();
bx::Error err;
void* addr = bx::memoryMap(NULL, size, 0, bx::Memory::ReadWrite, &err);
REQUIRE(err.isOk() );
REQUIRE(nullptr != addr);
uint8_t* bytes = (uint8_t*)addr;
bx::memSet(bytes, 0xab, size);
// Pages stay accessible after their contents are dropped.
bx::memoryMap(addr, size, 0, bx::Memory::DontNeed, &err);
REQUIRE(err.isOk() );
bx::memSet(bytes, 0xcd, size);
REQUIRE(0xcd == bytes[0]);
REQUIRE(0xcd == bytes[size-1]);
bx::memoryUnmap(addr, size, &err);
REQUIRE(err.isOk() );
}
TEST_CASE("memoryAllocationGranularity", "[os]")
{
const size_t pageSize = bx::memoryPageSize();