25
25
26
26
namespace SharedUtil
27
27
{
28
- #ifdef _WIN32
29
- static HANDLE g_process = GetCurrentProcess();
30
- #endif
31
-
32
28
bool TryGetProcessMemoryStats (ProcessMemoryStats& out)
33
29
{
34
30
out = {};
@@ -44,14 +40,15 @@ namespace SharedUtil
44
40
45
41
static std::vector<char > workingSetBuffer (2048 * sizeof (PSAPI_WORKING_SET_BLOCK) + sizeof (PSAPI_WORKING_SET_INFORMATION));
46
42
auto workingSetInfo = reinterpret_cast <PSAPI_WORKING_SET_INFORMATION*>(workingSetBuffer.data ());
47
- BOOL success = QueryWorkingSet (g_process, workingSetBuffer.data (), workingSetBuffer.size ());
43
+ HANDLE process = GetCurrentProcess ();
44
+ BOOL success = QueryWorkingSet (process, workingSetBuffer.data (), workingSetBuffer.size ());
48
45
49
46
if (!success && GetLastError () == ERROR_BAD_LENGTH)
50
47
{
51
48
workingSetInfo->NumberOfEntries += 64 ; // Insurance in case the number of entries changes.
52
49
workingSetBuffer.resize (workingSetInfo->NumberOfEntries * sizeof (PSAPI_WORKING_SET_BLOCK) + sizeof (PSAPI_WORKING_SET_INFORMATION));
53
50
workingSetInfo = reinterpret_cast <PSAPI_WORKING_SET_INFORMATION*>(workingSetBuffer.data ());
54
- success = QueryWorkingSet (g_process , workingSetBuffer.data (), workingSetBuffer.size ());
51
+ success = QueryWorkingSet (process , workingSetBuffer.data (), workingSetBuffer.size ());
55
52
}
56
53
57
54
if (success)
@@ -84,7 +81,7 @@ namespace SharedUtil
84
81
{
85
82
PROCESS_MEMORY_COUNTERS_EX memoryInfo{};
86
83
87
- if (!GetProcessMemoryInfo (g_process , reinterpret_cast <PPROCESS_MEMORY_COUNTERS>(&memoryInfo), sizeof (memoryInfo)))
84
+ if (!GetProcessMemoryInfo (process , reinterpret_cast <PPROCESS_MEMORY_COUNTERS>(&memoryInfo), sizeof (memoryInfo)))
88
85
return false ;
89
86
90
87
out.residentMemorySize = memoryInfo.WorkingSetSize ;
0 commit comments