Files
mandy/runtime/shims/xmem.cpp
NotRexed db0dc8bd5e 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>
2026-11-20 15:22:16 -05:00

49 lines
1.9 KiB
C++

// xmem.dll: the GDK's tracked allocator. Mapped onto the CRT's aligned heap and VirtualQuery.
//
// TODO: decode the XALLOC_ATTRIBUTES alignment and memory-type fields. Until that's checked against real call
// sites, every block is 64-byte aligned, which covers cache-line and SIMD alignment.
#include <windows.h>
#include <malloc.h>
#include <psapi.h>
#include <cstdint>
#include <cstring>
#include "shims.h"
namespace {
constexpr size_t kAlign = 64;
void* __stdcall XMemAlloc(size_t size, uint64_t /*attributes*/) { return _aligned_malloc(size ? size : 1, kAlign); }
void __stdcall XMemFree(void* p, uint64_t /*attributes*/) { _aligned_free(p); }
// Same shape as VirtualQuery on the console (MEMORY_BASIC_INFORMATION out, returns bytes written).
SIZE_T __stdcall XMemVirtualQuery(const void* address, MEMORY_BASIC_INFORMATION* info, SIZE_T length) {
return VirtualQuery(address, info, length);
}
// GDK: HRESULT XMemGetWorkingSetStatistics(XMEM_WORKING_SET_STATISTICS*). The struct's exact layout isn't known
// yet, so it's zeroed up to a conservative size and filled with the process's working set in the first field.
// Revisit once the exe's call sites show which fields are read.
long __stdcall XMemGetWorkingSetStatistics(void* stats) {
if (!stats) return static_cast<long>(0x80070057); // E_INVALIDARG
std::memset(stats, 0, 32);
PROCESS_MEMORY_COUNTERS pmc{};
if (GetProcessMemoryInfo(GetCurrentProcess(), &pmc, sizeof(pmc))) {
uint64_t ws = pmc.WorkingSetSize;
std::memcpy(stats, &ws, sizeof(ws));
}
return 0; // S_OK
}
} // namespace
SHIM_TABLE(xmem,
{"xmem.dll", "XMemAlloc", reinterpret_cast<void*>(&XMemAlloc)},
{"xmem.dll", "XMemFree", reinterpret_cast<void*>(&XMemFree)},
{"xmem.dll", "XMemVirtualQuery", reinterpret_cast<void*>(&XMemVirtualQuery)},
{"xmem.dll", "XMemGetWorkingSetStatistics", reinterpret_cast<void*>(&XMemGetWorkingSetStatistics)},
);