From ac27ac75ac9dc091b8a10c1124b55c0c66b89258 Mon Sep 17 00:00:00 2001 From: vxiiduu <73044267+vxiiduu@users.noreply.github.com> Date: Sat, 12 Aug 2023 22:19:16 +1000 Subject: [PATCH] Enhance Windows 7 compatibility. --- llama-util.h | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/llama-util.h b/llama-util.h index 6e9e39ddb..0c967e100 100644 --- a/llama-util.h +++ b/llama-util.h @@ -273,13 +273,28 @@ struct llama_mmap { #if _WIN32_WINNT >= _WIN32_WINNT_WIN8 if (prefetch) { - // Advise the kernel to preload the mapped memory - WIN32_MEMORY_RANGE_ENTRY range; - range.VirtualAddress = addr; - range.NumberOfBytes = (SIZE_T)size; - if (!PrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) { - fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n", - llama_format_win_err(GetLastError()).c_str()); + // The PrefetchVirtualMemory API is only present on Windows 8 and above, so we + // will dynamically load it using GetProcAddress. + BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG); + HMODULE hKernel32; + + // This call is guaranteed to succeed. + hKernel32 = GetModuleHandleW(L"kernel32.dll"); + + // This call may fail if on a pre-Win8 system. + pPrefetchVirtualMemory = + (BOOL (WINAPI *) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG)) + GetProcAddress(hKernel32, "PrefetchVirtualMemory"); + + if (pPrefetchVirtualMemory) { + // Advise the kernel to preload the mapped memory. + WIN32_MEMORY_RANGE_ENTRY range; + range.VirtualAddress = addr; + range.NumberOfBytes = (SIZE_T)size; + if (!pPrefetchVirtualMemory(GetCurrentProcess(), 1, &range, 0)) { + fprintf(stderr, "warning: PrefetchVirtualMemory failed: %s\n", + llama_format_win_err(GetLastError()).c_str()); + } } } #else