From d45dc977d864c84f73e0cbbf4f0def00b5c03457 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Mon, 24 Jul 2023 12:48:42 +0100 Subject: [PATCH] [*] NT: Further reduce Win32 link-time requirements cont (1948dd0c) --- Source/AuProcAddresses.NT.cpp | 39 +++++++++++++++++- Source/AuProcAddresses.NT.hpp | 52 +++++++++++++++++++++++- Source/Console/ConsoleStd/ConsoleStd.cpp | 5 ++- Source/HWInfo/AuRamInfo.cpp | 23 +++++++---- Source/IO/FS/Async.NT.cpp | 9 +++- Source/IO/FS/FileStream.NT.cpp | 2 +- Source/IO/FS/Watcher.NT.cpp | 9 +++- Source/IO/Net/AuNetStream.NT.cpp | 9 +++- Source/Locale/LocaleGetLocale.cpp | 44 +++++++++++++++----- Source/Threading/Threads/AuOSThread.cpp | 18 +++++++- 10 files changed, 183 insertions(+), 27 deletions(-) diff --git a/Source/AuProcAddresses.NT.cpp b/Source/AuProcAddresses.NT.cpp index 4ad2b8b6..b430f160 100644 --- a/Source/AuProcAddresses.NT.cpp +++ b/Source/AuProcAddresses.NT.cpp @@ -30,6 +30,7 @@ namespace Aurora ADD_LOAD_LIB(BCrypt); ADD_LOAD_LIB(Theme); ADD_LOAD_LIB(Shell); + ADD_LOAD_LIB(PSAPILegacy); #define ADD_GET_PROC(name, proc) \ if (h ## name) \ @@ -51,6 +52,20 @@ namespace Aurora } \ } + #define ADD_GET_PROC_BI2(name, name2, proc, proc2) \ + p ## proc2 = nullptr; \ + if (h ## name) \ + { \ + p ## proc2 = AuReinterpretCast(GetProcAddress(h ## name, #proc)); \ + } \ + if (!p ## proc2) \ + { \ + if (h ## name2) \ + { \ + p ## proc2 = AuReinterpretCast(GetProcAddress(h ## name2, #proc2)); \ + } \ + } + #define ADD_GET_PROC_INTERNAL_MAP(name, proc, symbol) \ if (h ## name) \ { \ @@ -86,7 +101,16 @@ namespace Aurora ADD_GET_PROC(Kernel32, FindFirstStreamW) ADD_GET_PROC(Kernel32, FindNextStreamW) ADD_GET_PROC(Kernel32, FindClose) - + ADD_GET_PROC(Kernel32, CancelIoEx) + ADD_GET_PROC(Kernel32, CancelSynchronousIo) + ADD_GET_PROC(Kernel32, SetFileInformationByHandle) + ADD_GET_PROC(Kernel32, GetLocaleInfoEx) + ADD_GET_PROC(Kernel32, LCIDToLocaleName) + ADD_GET_PROC(Kernel32, GetLocaleInfoW) + ADD_GET_PROC(Kernel32, GetThreadId) + + ADD_GET_PROC_BI2(Kernel32, PSAPILegacy, K32GetProcessMemoryInfo, GetProcessMemoryInfo) + ADD_GET_PROC(Sync, WaitOnAddress) ADD_GET_PROC(Sync, WakeByAddressSingle) ADD_GET_PROC(Sync, WakeByAddressAll) @@ -108,7 +132,7 @@ namespace Aurora ADD_GET_PROC(Theme, SetWindowTheme) ADD_GET_PROC(Shell, SHGetKnownFolderPath) - + #else pWaitOnAddress = WaitOnAddress; pWakeByAddressSingle = WakeByAddressSingle; @@ -116,6 +140,17 @@ namespace Aurora pVirtualAlloc2 = VirtualAlloc2FromApp; pMapViewOfFile3 = MapViewOfFile3FromApp; + pCancelIoEx = CancelIoEx; + pCancelSynchronousIo = CancelSynchronousIo; + + pGetLocaleInfoEx = GetLocaleInfoEx; + + pLCIDToLocaleName = LCIDToLocaleName; + + pSetFileInformationByHandle = SetFileInformationByHandle; + + pFindClose = FindClose; + pGetSystemCpuSetInformation = GetSystemCpuSetInformation; pGetLogicalProcessorInformation = GetLogicalProcessorInformation; diff --git a/Source/AuProcAddresses.NT.hpp b/Source/AuProcAddresses.NT.hpp index eebf221d..f5da648c 100644 --- a/Source/AuProcAddresses.NT.hpp +++ b/Source/AuProcAddresses.NT.hpp @@ -7,6 +7,8 @@ ***/ #pragma once +struct _PROCESS_MEMORY_COUNTERS; + namespace Aurora { void InitNTAddresses(); @@ -20,6 +22,7 @@ namespace Aurora static const wchar_t *kBCryptDllName { L"bcrypt.dll" }; static const wchar_t *kThemeDllName { L"UxTheme.dll" }; static const wchar_t *kShellDllName { L"Shell32.dll" }; + static const wchar_t *kPSAPILegacyDllName { L"psapi.dll" }; struct WIN32_MEMORY_RANGE_ENTRY2 { @@ -277,13 +280,60 @@ namespace Aurora HANDLE hFindFile ); + inline BOOL(__stdcall *pCancelIoEx)( + HANDLE hFile, + LPOVERLAPPED lpOverlapped + ); + + inline BOOL(__stdcall *pCancelSynchronousIo)( + HANDLE hThread + ); + + inline BOOL(__stdcall *pGetProcessMemoryInfo)( + HANDLE Process, + ::_PROCESS_MEMORY_COUNTERS *ppsmemCounters, + DWORD cb + ); + + inline BOOL(__stdcall *pSetFileInformationByHandle)( + HANDLE hFile, + FILE_INFO_BY_HANDLE_CLASS FileInformationClass, + LPVOID lpFileInformation, + DWORD dwBufferSize + ); + + inline int(__stdcall *pGetLocaleInfoEx)( + LPCWSTR lpLocaleName, + LCTYPE LCType, + LPWSTR lpLCData, + int cchData + ); + + inline int(__stdcall *pLCIDToLocaleName)( + LCID Locale, + LPWSTR lpName, + int cchName, + DWORD dwFlags + ); + + inline int(__stdcall *pGetLocaleInfoW)( + LCID Locale, + LCTYPE LCType, + LPWSTR lpLCData, + int cchData + ); + + inline DWORD(__stdcall *pGetThreadId)( + HANDLE hThread + ); + inline HRESULT(__stdcall *pSHGetKnownFolderPath)( const GUID & rfid, DWORD dwFlags, HANDLE hToken, PWSTR * ppszPath ); - + inline bool gUseNativeWaitMutex {}; inline bool gUseNativeWaitCondvar {}; inline bool gUseNativeWaitSemapahore {}; diff --git a/Source/Console/ConsoleStd/ConsoleStd.cpp b/Source/Console/ConsoleStd/ConsoleStd.cpp index 16fb7fe8..240af526 100755 --- a/Source/Console/ConsoleStd/ConsoleStd.cpp +++ b/Source/Console/ConsoleStd/ConsoleStd.cpp @@ -525,9 +525,10 @@ namespace Aurora::Console::ConsoleStd void SignalKillNT() { - if (IS_STREAM_HANDLE_VALID(gWin32Thread)) + if (IS_STREAM_HANDLE_VALID(gWin32Thread) && + pCancelSynchronousIo) { - CancelSynchronousIo(gWin32Thread); + pCancelSynchronousIo(gWin32Thread); } if (IS_STREAM_HANDLE_VALID(gTerminateConsole)) diff --git a/Source/HWInfo/AuRamInfo.cpp b/Source/HWInfo/AuRamInfo.cpp index 9d3b62f3..d9dfc456 100644 --- a/Source/HWInfo/AuRamInfo.cpp +++ b/Source/HWInfo/AuRamInfo.cpp @@ -37,7 +37,9 @@ namespace Aurora::HWInfo #if defined(AURORA_IS_MODERNNT_DERIVED) PROCESS_MEMORY_COUNTERS pm; - if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pm), sizeof(PROCESS_MEMORY_COUNTERS))) + if (pGetProcessMemoryInfo(GetCurrentProcess(), + reinterpret_cast(&pm), + sizeof(PROCESS_MEMORY_COUNTERS))) { return RamStat {pm.WorkingSetSize, max}; } @@ -114,14 +116,21 @@ namespace Aurora::HWInfo PROCESS_MEMORY_COUNTERS_EX pmc; PROCESS_MEMORY_COUNTERS pm; - if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pmc), sizeof(PROCESS_MEMORY_COUNTERS_EX))) + if (pGetProcessMemoryInfo) { - return RamStat {pmc.PrivateUsage, max}; - } + if (pGetProcessMemoryInfo(GetCurrentProcess(), + reinterpret_cast(&pmc), + sizeof(PROCESS_MEMORY_COUNTERS_EX))) + { + return RamStat { pmc.PrivateUsage, max }; + } - if (GetProcessMemoryInfo(GetCurrentProcess(), reinterpret_cast(&pm), sizeof(PROCESS_MEMORY_COUNTERS))) - { - return RamStat {pm.WorkingSetSize, max}; + if (pGetProcessMemoryInfo(GetCurrentProcess(), + reinterpret_cast(&pm), + sizeof(PROCESS_MEMORY_COUNTERS))) + { + return RamStat { pm.WorkingSetSize, max }; + } } return RamStat {}; diff --git a/Source/IO/FS/Async.NT.cpp b/Source/IO/FS/Async.NT.cpp index 03d3854f..322febf9 100644 --- a/Source/IO/FS/Async.NT.cpp +++ b/Source/IO/FS/Async.NT.cpp @@ -491,7 +491,14 @@ namespace Aurora::IO::FS { this->isIrredeemable_ = true; this->bHasFailed = true; - ::CancelIoEx(this->pHandle_->handle, &this->overlap); + if (pCancelIoEx) + { + pCancelIoEx(this->pHandle_->handle, &this->overlap); + } + else + { + ::CancelIo(this->pHandle_->handle); + } ::SetEvent(this->event); this->dwOsErrorCode = ERROR_ABANDONED_WAIT_0; } diff --git a/Source/IO/FS/FileStream.NT.cpp b/Source/IO/FS/FileStream.NT.cpp index 3444f705..5167eb58 100644 --- a/Source/IO/FS/FileStream.NT.cpp +++ b/Source/IO/FS/FileStream.NT.cpp @@ -188,7 +188,7 @@ namespace Aurora::IO::FS { FILE_DISPOSITION_INFO rm {}; rm.DeleteFile = true; - if (!SetFileInformationByHandle(this->handle_, _FILE_INFO_BY_HANDLE_CLASS::FileDispositionInfo, &rm, sizeof(rm))) + if (!(pSetFileInformationByHandle && pSetFileInformationByHandle(this->handle_, _FILE_INFO_BY_HANDLE_CLASS::FileDispositionInfo, &rm, sizeof(rm)))) { SysPushErrorIO("Couldn't delete temporary file {}", this->path_); } diff --git a/Source/IO/FS/Watcher.NT.cpp b/Source/IO/FS/Watcher.NT.cpp index 6ca5c37b..a255bd5d 100644 --- a/Source/IO/FS/Watcher.NT.cpp +++ b/Source/IO/FS/Watcher.NT.cpp @@ -438,7 +438,14 @@ namespace Aurora::IO::FS void NTWatchObject::Cancel() { - CancelIoEx(this->hFileHandle, &this->ntOverlapped); + if (pCancelIoEx) + { + pCancelIoEx(this->hFileHandle, &this->ntOverlapped); + } + else + { + ::CancelIo(this->hFileHandle); + } AuWin32CloseHandle(this->hFileHandle); } diff --git a/Source/IO/Net/AuNetStream.NT.cpp b/Source/IO/Net/AuNetStream.NT.cpp index e7c907d4..9ec8f845 100644 --- a/Source/IO/Net/AuNetStream.NT.cpp +++ b/Source/IO/Net/AuNetStream.NT.cpp @@ -392,7 +392,14 @@ namespace Aurora::IO::Net { this->bIsIrredeemable = true; this->bHasFailed = true; - ::CancelIoEx((HANDLE)this->GetSocket(), &this->overlap); + if (pCancelIoEx) + { + pCancelIoEx((HANDLE)this->GetSocket(), &this->overlap); + } + else + { + ::CancelIo((HANDLE)this->GetSocket()); + } this->dwOsErrorCode = ERROR_ABANDONED_WAIT_0; } else diff --git a/Source/Locale/LocaleGetLocale.cpp b/Source/Locale/LocaleGetLocale.cpp index ea9c0482..b8608cee 100644 --- a/Source/Locale/LocaleGetLocale.cpp +++ b/Source/Locale/LocaleGetLocale.cpp @@ -94,19 +94,43 @@ namespace Aurora::Locale int ret; wchar_t name[LOCALE_NAME_MAX_LENGTH] = { 0 }; - ret = LCIDToLocaleName(LOCALE_USER_DEFAULT, name, LOCALE_NAME_MAX_LENGTH, LOCALE_ALLOW_NEUTRAL_NAMES); - SysAssert(ret, "Couldn't acquire win32 locale information"); + if (pLCIDToLocaleName) + { + ret = pLCIDToLocaleName(LOCALE_USER_DEFAULT, name, LOCALE_NAME_MAX_LENGTH, LOCALE_ALLOW_NEUTRAL_NAMES); + SysAssert(ret, "Couldn't acquire win32 locale information"); + } - wchar_t language[LOCALE_NAME_MAX_LENGTH] = { 0 }; - ret = GetLocaleInfoEx(name, LOCALE_SISO639LANGNAME, language, LOCALE_NAME_MAX_LENGTH); - SysAssert(ret, "Couldn't acquire win32 provided ISO 639 map of {}", ConvertFromWChar(name)); + { + wchar_t language[LOCALE_NAME_MAX_LENGTH] = { 0 }; + if (pGetLocaleInfoEx) + { + ret = pGetLocaleInfoEx(name, LOCALE_SISO639LANGNAME, language, LOCALE_NAME_MAX_LENGTH); + SysAssert(ret, "Couldn't acquire win32 provided ISO 639 map of {}", ConvertFromWChar(name)); + } + else if (pGetLocaleInfoW) + { + ret = pGetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SISO639LANGNAME, language, LOCALE_NAME_MAX_LENGTH); + SysAssert(ret, "Couldn't acquire win32 provided ISO 639 map of {}", ConvertFromWChar(name)); + } - wchar_t country[LOCALE_NAME_MAX_LENGTH] = { 0 }; - ret = GetLocaleInfoEx(name, LOCALE_SISO3166CTRYNAME, country, LOCALE_NAME_MAX_LENGTH); - SysAssert(ret, "Couldn't acquire win32 provided ISO 3166 map of {}", ConvertFromWChar(name)); + gLanguageCode = ConvertFromWChar(language); + } - gCountryCode = ConvertFromWChar(country); - gLanguageCode = ConvertFromWChar(language); + { + wchar_t country[LOCALE_NAME_MAX_LENGTH] = { 0 }; + if (pGetLocaleInfoEx) + { + ret = pGetLocaleInfoEx(name, LOCALE_SISO3166CTRYNAME, country, LOCALE_NAME_MAX_LENGTH); + SysAssert(ret, "Couldn't acquire win32 provided ISO 3166 map of {}", ConvertFromWChar(name)); + } + else if (pGetLocaleInfoW) + { + ret = pGetLocaleInfoW(LOCALE_USER_DEFAULT, LOCALE_SISO3166CTRYNAME, country, LOCALE_NAME_MAX_LENGTH); + SysAssert(ret, "Couldn't acquire win32 provided ISO 3166 map of {}", ConvertFromWChar(name)); + } + + gCountryCode = ConvertFromWChar(country); + } SetCodesetCommonGuessWin32(); } diff --git a/Source/Threading/Threads/AuOSThread.cpp b/Source/Threading/Threads/AuOSThread.cpp index c236922b..9bcc1082 100644 --- a/Source/Threading/Threads/AuOSThread.cpp +++ b/Source/Threading/Threads/AuOSThread.cpp @@ -646,7 +646,19 @@ namespace Aurora::Threading::Threads THREADNAME_INFO info; info.dwType = 0x1000; info.szName = this->name_.c_str(); - info.dwThreadID = ::GetThreadId(this->handle_); + if (pGetThreadId) + { + info.dwThreadID = pGetThreadId(this->handle_); + } + else if (this->unixThreadId_ == GetCurrentThreadId()) + { + info.dwThreadID = this->unixThreadId_; + } + else + { + return; + } + info.dwFlags = 0; auto raise = AuStaticCast([](THREADNAME_INFO &info) @@ -712,6 +724,10 @@ namespace Aurora::Threading::Threads this->unixThreadId_ = 0; // !!!! #endif + #if defined(AURORA_PLATFORM_WIN32) + this->unixThreadId_ = GetCurrentThreadId(); + #endif + if (this->tls_) { SetThreadKey(this->tls_);