MCPcopy Create free account
hub / github.com/Niko1221/Strata / commit_growth

Function commit_growth

engine/include/strata/platform/commit.hpp:102–154  ·  view source on GitHub ↗

The bytes Windows' page files may still grow by: a page file Windows manages (PagingFiles "?:\pagefile.sys", or a file's sizes "0 0") up to 3x the RAM Windows sees (4 GiB at least) and an eighth of its volume, a set one up to its maximum - each within its volume's free space less a tenth of the volume. 0 off Windows, without a page file, or when the settings cannot be read. STRATA_COMMIT_GROWTH=

Source from the content-addressed store, hash-verified

100/// its maximum - each within its volume's free space less a tenth of the volume. 0 off Windows, without a page file,
101/// or when the settings cannot be read. STRATA_COMMIT_GROWTH=0: 0 (as v1.1.17 counted).
102inline uint64_t commit_growth(uint64_t ram_total) {
103#if defined(_WIN32)
104 if (const char* v = std::getenv("STRATA_COMMIT_GROWTH"); v != nullptr && std::atoi(v) == 0) return 0;
105 // the setting, through advapi32 as loaded (RegGetValueW), so no program links it for this
106 using Get = LONG(WINAPI*)(HKEY, LPCWSTR, LPCWSTR, DWORD, LPDWORD, PVOID, LPDWORD);
107 HMODULE m = LoadLibraryA("advapi32.dll");
108 if (m == nullptr) return 0;
109 const auto get = reinterpret_cast<Get>(reinterpret_cast<void*>(GetProcAddress(m, "RegGetValueW")));
110 const wchar_t* key = L"SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management";
111 DWORD bytes = 0;
112 if (get == nullptr || get(HKEY_LOCAL_MACHINE, key, L"PagingFiles", RRF_RT_REG_MULTI_SZ, nullptr, nullptr, &bytes) !=
113 ERROR_SUCCESS || bytes < 4) {
114 FreeLibrary(m);
115 return 0;
116 }
117 std::vector<wchar_t> buf(bytes / sizeof(wchar_t) + 2, L'\0');
118 const LONG rc = get(HKEY_LOCAL_MACHINE, key, L"PagingFiles", RRF_RT_REG_MULTI_SZ, nullptr, buf.data(), &bytes);
119 FreeLibrary(m);
120 if (rc != ERROR_SUCCESS) return 0;
121 wchar_t sysdrive[8] = L"C:";
122 GetEnvironmentVariableW(L"SystemDrive", sysdrive, 8);
123 uint64_t grow = 0;
124 for (const wchar_t* e = buf.data(); *e != L'\0'; e += std::wcslen(e) + 1) {
125 // "<path> [<initial MB> <maximum MB>]"
126 std::wstring s(e), path = s.substr(0, s.find(L' '));
127 unsigned long long init_mb = 0, max_mb = 0;
128 if (s.size() > path.size()) std::swscanf(s.c_str() + path.size(), L"%llu %llu", &init_mb, &max_mb);
129 const bool managed = path.rfind(L"?:", 0) == 0 || (init_mb == 0 && max_mb == 0);
130 if (path.rfind(L"?:", 0) == 0) path = std::wstring(sysdrive) + path.substr(2);
131 if (path.size() < 3 || path[1] != L':') continue;
132 const std::wstring root = path.substr(0, 2) + L"\\";
133 ULARGE_INTEGER free_b{}, total_b{};
134 if (!GetDiskFreeSpaceExW(root.c_str(), &free_b, &total_b, nullptr)) continue;
135 // its size now, from its directory entry (the file itself is Windows' own: GetFileAttributesEx fails on it)
136 uint64_t now = 0;
137 WIN32_FIND_DATAW fd{};
138 const HANDLE h = FindFirstFileW(path.c_str(), &fd);
139 if (h != INVALID_HANDLE_VALUE) {
140 now = ((uint64_t) fd.nFileSizeHigh << 32) | fd.nFileSizeLow;
141 FindClose(h);
142 }
143 uint64_t most = managed ? (std::max)(3 * ram_total, uint64_t{4} << 30) : (uint64_t) max_mb << 20;
144 if (managed) most = (std::min)(most, total_b.QuadPart / 8);
145 const uint64_t keep = total_b.QuadPart / 10;
146 const uint64_t room = free_b.QuadPart > keep ? free_b.QuadPart - keep : 0;
147 grow += (std::min)(most > now ? most - now : 0, room);
148 }
149 return grow;
150#else
151 (void) ram_total;
152 return 0;
153#endif
154}
155
156} // namespace strata::platform

Callers 8

host_available_memoryFunction · 0.85
log_complement_memoryFunction · 0.85
mainFunction · 0.85
initMethod · 0.85
host_available_memoryFunction · 0.85
log_complement_memoryFunction · 0.85
sample_host_memoryFunction · 0.85
mainFunction · 0.85

Calls 4

getFunction · 0.50
dataMethod · 0.45
findMethod · 0.45
sizeMethod · 0.45

Tested by 1

mainFunction · 0.68