diff options
| author | NotRexed <therealnothingfn@gmail.com> | 2026-11-20 15:22:16 -0500 |
|---|---|---|
| committer | NotRexed <therealnothingfn@gmail.com> | 2026-11-20 15:22:16 -0500 |
| commit | db0dc8bd5ebce6e2bc27d47085f7f104eb3037d3 (patch) | |
| tree | 4fb3edca45f8b4dd3368c6ac2a0e4fdc8ed7065c /runtime/shims/xmem.cpp | |
Initial commit: Leonida, build-it-yourself PC port groundwork
Shader pipeline (carve, FSSM unpack, gfx1013 disassembly, RDNA2 to SPIR-V),
asset export via rpf9-explorer's Core (submodule), PE loader and shim
skeleton, and make_game.py, which builds from the user's own copy of the game.
No game data, keys or executables are included.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/shims/xmem.cpp')
| -rw-r--r-- | runtime/shims/xmem.cpp | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/runtime/shims/xmem.cpp b/runtime/shims/xmem.cpp new file mode 100644 index 000000000..d8fd76568 --- /dev/null +++ b/runtime/shims/xmem.cpp | |||
| @@ -0,0 +1,48 @@ | |||
| 1 | // xmem.dll: the GDK's tracked allocator. Mapped onto the CRT's aligned heap and VirtualQuery. | ||
| 2 | // | ||
| 3 | // TODO: decode the XALLOC_ATTRIBUTES alignment and memory-type fields. Until that's checked against real call | ||
| 4 | // sites, every block is 64-byte aligned, which covers cache-line and SIMD alignment. | ||
| 5 | #include <windows.h> | ||
| 6 | #include <malloc.h> | ||
| 7 | #include <psapi.h> | ||
| 8 | |||
| 9 | #include <cstdint> | ||
| 10 | #include <cstring> | ||
| 11 | |||
| 12 | #include "shims.h" | ||
| 13 | |||
| 14 | namespace { | ||
| 15 | |||
| 16 | constexpr size_t kAlign = 64; | ||
| 17 | |||
| 18 | void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligned_malloc(size ? size : 1, kAlign); } | ||
| 19 | |||
| 20 | void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); } | ||
| 21 | |||
| 22 | // Same shape as VirtualQuery on the console (MEMORY_BASIC_INFORMATION out, returns bytes written). | ||
| 23 | SIZE_T __stdcall XMemVirtualQuery(const void* address, MEMORY_BASIC_INFORMATION* info, SIZE_T length) { | ||
| 24 | return VirtualQuery(address, info, length); | ||
| 25 | } | ||
| 26 | |||
| 27 | // GDK: HRESULT XMemGetWorkingSetStatistics(XMEM_WORKING_SET_STATISTICS*). The struct's exact layout isn't known | ||
| 28 | // yet, so it's zeroed up to a conservative size and filled with the process's working set in the first field. | ||
| 29 | // Revisit once the exe's call sites show which fields are read. | ||
| 30 | long __stdcall XMemGetWorkingSetStatistics(void* stats) { | ||
| 31 | if (!stats) return static_cast<long>(0x80070057); // E_INVALIDARG | ||
| 32 | std::memset(stats, 0, 32); | ||
| 33 | PROCESS_MEMORY_COUNTERS pmc{}; | ||
| 34 | if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) { | ||
| 35 | uint64_t ws = pmc.WorkingSetSize; | ||
| 36 | std::memcpy(stats, &ws, sizeof(ws)); | ||
| 37 | } | ||
| 38 | return 0; // S_OK | ||
| 39 | } | ||
| 40 | |||
| 41 | } // namespace | ||
| 42 | |||
| 43 | SHIM_TABLE(xmem, | ||
| 44 | {"xmem.dll", "XMemAlloc", reinterpret_cast<void*>(&XMemAlloc)}, | ||
| 45 | {"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)}, | ||
| 46 | {"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)}, | ||
| 47 | {"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)}, | ||
| 48 | ); | ||