MCPcopy Create free account
hub / github.com/WinDurango/WinDurango / EraVirtualFreeEx

Function EraVirtualFreeEx

projects/WinDurango.KernelX/src/kernelx.cpp:353–392  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

351 if (!hMap)
352 {
353 SetLastError(ERROR_OUTOFMEMORY);
354 return nullptr;
355 }
356
357 for (SIZE_T dwOffset = 0; dwOffset < dwSize; dwOffset += dwPageSize)
358 {
359 LPVOID PageAddress = (LPBYTE)lpAddress + dwOffset;
360 MapViewOfFile3(hMap, hProcess, PageAddress, dwOffset, dwPageSize, MEM_REPLACE_PLACEHOLDER, PAGE_READWRITE,
361 nullptr, 0);
362 VirtualAlloc2(hProcess, PageAddress, dwPageSize, MEM_COMMIT, flProtect & PAGE_MASK, nullptr, 0);
363 }
364
365 CloseHandle(hMap);
366 }
367
368 return lpAddress;
369}
370
371EXTERN_C LPVOID __stdcall EraVirtualAlloc(LPVOID lpAddress, SIZE_T dwSize, DWORD flAllocationType, DWORD flProtect)
372{
373 return EraVirtualAllocEx(GetCurrentProcess(), lpAddress, dwSize, flAllocationType, flProtect);
374}
375
376EXTERN_C BOOL __stdcall EraVirtualFreeEx(HANDLE hProcess, LPVOID lpAddress, SIZE_T dwSize, DWORD dwFreeType)
377{
378 LPVOID AllocationBase;
379 MAPPABLE_MEM Mappable;
380 AcquireSRWLockShared(&XwpMappableLock);
381 AllocationBase = MappableQuery(lpAddress, &Mappable);
382 ReleaseSRWLockShared(&XwpMappableLock);
383
384 // If we have a mappable allocation, we need to ensure it is unmapped first.
385 if (AllocationBase)
386 {
387 ULONG_PTR PageSize = Mappable.Is4MBPages ? PAGE_SIZE_4MB : PAGE_SIZE_64K;
388 ULONG_PTR BasePage = (ULONG_PTR)lpAddress & ~(PageSize - 1);
389
390 if (dwFreeType == MEM_RELEASE && (AllocationBase != lpAddress || dwSize))
391 return FALSE;
392
393 if (dwFreeType == MEM_RELEASE || (dwFreeType == MEM_DECOMMIT && !dwSize))
394 dwSize = Mappable.RegionSize - ((ULONG_PTR)BasePage - (ULONG_PTR)AllocationBase);
395

Callers 1

EraVirtualFreeFunction · 0.85

Calls 1

MappableQueryFunction · 0.85

Tested by

no test coverage detected