From 49c7d6050f24676ef39b62f4e2076fc5b20f313c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Branimir=20Karad=C5=BEi=C4=87?= Date: Wed, 21 Oct 2026 17:37:35 -0700 Subject: [PATCH] Implemented Memory::DontNeed on Windows, pages are reset and removed from working set. (#471) --- src/os.cpp | 7 ++++++- tests/os_test.cpp | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/os.cpp b/src/os.cpp index 9b06ea2..fe834a0 100644 --- a/src/os.cpp +++ b/src/os.cpp @@ -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); diff --git a/tests/os_test.cpp b/tests/os_test.cpp index 238ea1a..8b4c70e 100644 --- a/tests/os_test.cpp +++ b/tests/os_test.cpp @@ -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();