Triforce v0.9.1

The 16 GB update: idle memory unlocked and handed back to Windows while playing, empty pages under freed locks returned, and a page-file check at launch that warns before a too-small page file can crash the game.
This commit is contained in:
ruckus7382sfgsgdf
2026-12-01 17:47:00 -06:00
parent 822ee913bc
commit dad28e2d4b
11 changed files with 802 additions and 7 deletions

View File

@@ -188,6 +188,89 @@ DWORD WINAPI hos_out_of_memory_box(LPVOID unused) {
return 0;
}
/* ---- ((Triforce, 12-01) The page file checked at the start (pagefile_rule.h has why and the rule) ---------------------------
A line in the log every start; and when the commit limit can never reach what the game needs, a warning that says what to
change, once for the page file's settings as they are (triforce_pagefile_warned.txt beside the game: shown again only when
they change). On a thread of its own: the start waits for nothing. HOS_PAGEFILE_CHECK=0: neither; =2: the warning whatever the
rule says (to see it) */
#if defined(_WIN32)
#include "pagefile_rule.h"
static char pagefile_box_text[1400];
static DWORD WINAPI pagefile_box(LPVOID unused) {
(void)unused;
MessageBoxA(NULL, pagefile_box_text, "Triforce: your page file is too small", MB_OK | MB_ICONWARNING | MB_TOPMOST);
return 0;
}
static DWORD WINAPI pagefile_check_main(LPVOID unused) {
(void)unused;
const char *e = getenv("HOS_PAGEFILE_CHECK");
const int mode = e ? atoi(e) : 1;
if (mode == 0) return 0;
char multi[2048];
DWORD size = sizeof multi - 2;
memset(multi, 0, sizeof multi);
const LONG r = RegGetValueA(HKEY_LOCAL_MACHINE, "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Memory Management",
"PagingFiles", RRF_RT_REG_MULTI_SZ, NULL, multi, &size);
const PfSettings s = pf_parse(r == ERROR_SUCCESS ? multi : "", r == ERROR_SUCCESS ? (size_t)size : 0);
MEMORYSTATUSEX ms;
ms.dwLength = sizeof ms;
if (!GlobalMemoryStatusEx(&ms)) return 0;
const double gb = 1024.0 * 1024.0 * 1024.0;
const double ram = (double)ms.ullTotalPhys / gb, limit = (double)ms.ullTotalPageFile / gb,
in_use = (double)(ms.ullTotalPageFile - ms.ullAvailPageFile) / gb;
double disk = 0;
if (s.kind == PF_KIND_MANAGED && s.drive) {
char root[4] = {s.drive, ':', '\\', 0};
ULARGE_INTEGER avail;
if (GetDiskFreeSpaceExA(root, &avail, NULL, NULL)) disk = (double)avail.QuadPart / gb;
}
const PfVerdict v = pf_decide(&s, ram, limit, in_use, disk);
static const char *kinds[] = {"none", "managed by Windows", "a fixed size"};
hos_log("mem: page file: %s%s%c%s (%s %.1f GB); RAM %.1f GB, commit limit %.1f GB now, %.1f GB in use before the game; the "
"limit can reach %.1f GB, the game needs %.1f GB: %s (HOS_PAGEFILE_CHECK)", kinds[s.kind], s.drive ? " on " : "",
s.drive ? s.drive : ' ', s.drive ? ":" : "", s.kind == PF_KIND_MANAGED ? "its drive has free" : "at most",
s.kind == PF_KIND_FIXED ? s.fixed_max_gb : s.kind == PF_KIND_MANAGED ? disk : 0.0,
ram, limit, in_use, v.reach_gb, v.need_gb,
v.verdict == PF_OK ? "enough" : v.verdict == PF_SHORT_NONE ? "SHORT, no page file" : v.verdict == PF_SHORT_FIXED ?
"SHORT, the fixed page file is too small" : "SHORT, the page file's drive is nearly full");
if (v.verdict == PF_OK && mode != 2) return 0;
/* (once for these settings: the kind, the drive, the fixed size and the RAM) */
char sig[96], path[MAX_PATH + 40], had[96] = "";
snprintf(sig, sizeof sig, "%d %c %.0f %.0f %d", s.kind, s.drive ? s.drive : '-', s.fixed_max_gb, ram, v.verdict);
const DWORD n = hos_game_path_a(path, MAX_PATH);
if (n && n < MAX_PATH) {
char *slash = strrchr(path, '\\');
if (slash) strcpy(slash + 1, "triforce_pagefile_warned.txt");
FILE *f = fopen(path, "rb");
if (f) {
const size_t k = fread(had, 1, sizeof had - 1, f);
had[k] = 0;
fclose(f);
}
if (!strcmp(had, sig) && mode != 2) {
hos_log("mem: page file: warned before for these settings (delete triforce_pagefile_warned.txt to see it again)");
return 0;
}
if ((f = fopen(path, "wb"))) fputs(sig, f), fclose(f);
}
const char *what = v.verdict == PF_SHORT_NONE ? "Windows has no page file"
: v.verdict == PF_SHORT_FIXED ? "Windows' page file is set to a fixed size that is too small"
: "the drive that holds Windows' page file is nearly full, so the page file cannot grow";
snprintf(pagefile_box_text, sizeof pagefile_box_text,
"Triforce can use up to about %.0f GB of memory (RAM plus the page file) at its peak, and %s: this PC can reach "
"%.0f GB, it needs about %.0f GB. When it runs out, the game closes.\n\n"
"To fix it: open Windows' Settings > System > About > Advanced system settings > Performance: Settings > "
"Advanced > Virtual memory: Change, tick \"Automatically manage paging file size for all drives\", then restart "
"the PC.%s\n\n"
"This warning is shown once for these settings.",
24.0, what, v.reach_gb, v.need_gb,
v.verdict == PF_SHORT_DISK ? " Also free up space on that drive (at least 20 GB)." : "");
CloseHandle(CreateThread(NULL, 0, pagefile_box, NULL, 0, NULL));
return 0;
}
void hos_pagefile_check(void) { CloseHandle(CreateThread(NULL, 0, pagefile_check_main, NULL, 0, NULL)); }
#endif
static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
EXCEPTION_RECORD *rec = info->ExceptionRecord;
if (rec->ExceptionCode != EXCEPTION_ACCESS_VIOLATION || rec->NumberParameters < 2) return EXCEPTION_CONTINUE_SEARCH;
@@ -195,6 +278,12 @@ static LONG CALLBACK commit_on_touch(EXCEPTION_POINTERS *info) {
if (p < A64_MEM || p >= A64_MEM + HOS_GUEST_SPACE) return EXCEPTION_CONTINUE_SEARCH;
const uint64_t guest = (uint64_t)(p - A64_MEM);
if (guest < 0x100000) return EXCEPTION_CONTINUE_SEARCH; /* (a null pointer: the game's own fault) */
/* ((Triforce, 12-01) a block of zeros being given back (hos_mem.c zero_release) is read-only for a moment: the write waits
for it, then is tried again */
{
int hos_zero_wait(uint64_t guest);
if (hos_zero_wait(guest)) return EXCEPTION_CONTINUE_EXECUTION;
}
MEMORY_BASIC_INFORMATION mbi;
if (!VirtualQuery(p, &mbi, sizeof mbi)) return EXCEPTION_CONTINUE_SEARCH;
#ifdef _WIN32
@@ -1335,6 +1424,10 @@ static DWORD WINAPI census_main(void *unused) {
}
}
}
{ /* ((Triforce, 12-01) the zero pages under freed imports (hos_mem.c), every pass whatever the RAM */
int64_t hos_mem_zero_give_back(uint64_t max_bytes);
hos_mem_zero_give_back(512ULL << 20);
}
if (!census_line) continue;
hos_mem_census(&c);
char line[1024];
@@ -1351,7 +1444,7 @@ void hos_mem_census_start(void) {
census_line = !(e && atoi(e) <= 0);
census_every = e && atoi(e) > 0 ? atoi(e) : 5;
census_give_back = g ? atoi(g) : 0;
if (!census_line && census_give_back <= 0) return;
if (!census_line && census_give_back <= 0) return; /* ((Triforce, 12-01) the zero give-back runs on this thread too) */
char every[32] = "off";
if (census_line) snprintf(every, sizeof every, "every %d s", census_every);
hos_log("mem: census %s; give-back %s (HOS_MEM_CENSUS, HOS_MEM_GIVE_BACK)", every,