@@ -611,20 +611,25 @@ struct llama_mmap {
611
611
throw std::runtime_error (format (" MapViewOfFile failed: %s" , llama_format_win_err (error).c_str ()));
612
612
}
613
613
614
- #if _WIN32_WINNT >= _WIN32_WINNT_WIN8
615
614
if (prefetch) {
616
- // Advise the kernel to preload the mapped memory
617
- WIN32_MEMORY_RANGE_ENTRY range;
618
- range.VirtualAddress = addr;
619
- range.NumberOfBytes = (SIZE_T)size;
620
- if (!PrefetchVirtualMemory (GetCurrentProcess (), 1 , &range, 0 )) {
621
- fprintf (stderr, " warning: PrefetchVirtualMemory failed: %s\n " ,
622
- llama_format_win_err (GetLastError ()).c_str ());
615
+ // PrefetchVirtualMemory is only present on Windows 8 and above, so we dynamically load it
616
+ BOOL (WINAPI *pPrefetchVirtualMemory) (HANDLE, ULONG_PTR, PWIN32_MEMORY_RANGE_ENTRY, ULONG);
617
+ HMODULE hKernel32 = GetModuleHandleW (L" kernel32.dll" );
618
+
619
+ // may fail on pre-Windows 8 systems
620
+ pPrefetchVirtualMemory = reinterpret_cast <decltype (pPrefetchVirtualMemory)> (GetProcAddress (hKernel32, " PrefetchVirtualMemory" ));
621
+
622
+ if (pPrefetchVirtualMemory) {
623
+ // advise the kernel to preload the mapped memory
624
+ WIN32_MEMORY_RANGE_ENTRY range;
625
+ range.VirtualAddress = addr;
626
+ range.NumberOfBytes = (SIZE_T)size;
627
+ if (!pPrefetchVirtualMemory (GetCurrentProcess (), 1 , &range, 0 )) {
628
+ fprintf (stderr, " warning: PrefetchVirtualMemory failed: %s\n " ,
629
+ llama_format_win_err (GetLastError ()).c_str ());
630
+ }
623
631
}
624
632
}
625
- #else
626
- #pragma message("warning: You are building for pre-Windows 8; prefetch not supported")
627
- #endif // _WIN32_WINNT >= _WIN32_WINNT_WIN8
628
633
}
629
634
630
635
~llama_mmap () {
0 commit comments