M2Array stores a signed 32-bit self-relative offset. On x64 a separate heap block can lie more than 2 GB from m_data, so relocated low-priority .anim sequence data wrapped and crashed in ProcessEventCallbacks. Add OsAllocNear/OsFreeNear (Win64): reserve a block within 1.5 GB of an anchor. x64 now uses it for .anim buffers (load, cancel, destructor) and for the skin vertex overflow, which again follows the Win32 structure (m_flags |= 8, freed in ~CM2Shared). Texture combo arrays keep growing m_data. All new code is under WOWDECOMP_ARCH_64. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
83 lines
2.4 KiB
C++
83 lines
2.4 KiB
C++
#if defined(WOWDECOMP_ARCH_64)
|
|
|
|
#include <cstdint>
|
|
#include <windows.h>
|
|
#include "Engine/Source/Os/OsMemory.h"
|
|
|
|
static const uintptr_t s_nearRange = 0x60000000;
|
|
|
|
void* OsAllocNear(const void* anchor, size_t size) {
|
|
SYSTEM_INFO info;
|
|
GetSystemInfo(&info);
|
|
uintptr_t granularity = info.dwAllocationGranularity;
|
|
uintptr_t bytes = (size + granularity - 1) & ~(granularity - 1);
|
|
uintptr_t center = reinterpret_cast<uintptr_t>(anchor);
|
|
uintptr_t minimum = reinterpret_cast<uintptr_t>(info.lpMinimumApplicationAddress);
|
|
uintptr_t maximum = reinterpret_cast<uintptr_t>(info.lpMaximumApplicationAddress);
|
|
|
|
if (center > minimum + s_nearRange) {
|
|
minimum = center - s_nearRange;
|
|
}
|
|
|
|
if (center + s_nearRange < maximum) {
|
|
maximum = center + s_nearRange;
|
|
}
|
|
|
|
MEMORY_BASIC_INFORMATION region;
|
|
uintptr_t address = center;
|
|
|
|
while (address < maximum && VirtualQuery(reinterpret_cast<void*>(address), ®ion, sizeof(region))) {
|
|
uintptr_t base = reinterpret_cast<uintptr_t>(region.BaseAddress);
|
|
uintptr_t end = base + region.RegionSize;
|
|
|
|
if (region.State == MEM_FREE) {
|
|
uintptr_t candidate = ((base > address ? base : address) + granularity - 1) & ~(granularity - 1);
|
|
|
|
if (candidate + bytes <= end && candidate + bytes <= maximum) {
|
|
void* ptr = VirtualAlloc(reinterpret_cast<void*>(candidate), bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (ptr) {
|
|
return ptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
address = end;
|
|
}
|
|
|
|
address = center;
|
|
|
|
while (address > minimum && VirtualQuery(reinterpret_cast<void*>(address - 1), ®ion, sizeof(region))) {
|
|
uintptr_t base = reinterpret_cast<uintptr_t>(region.BaseAddress);
|
|
uintptr_t end = base + region.RegionSize;
|
|
|
|
if (end > address) {
|
|
end = address;
|
|
}
|
|
|
|
if (region.State == MEM_FREE && end - base >= bytes) {
|
|
uintptr_t candidate = (end - bytes) & ~(granularity - 1);
|
|
|
|
if (candidate >= base && candidate >= minimum) {
|
|
void* ptr = VirtualAlloc(reinterpret_cast<void*>(candidate), bytes, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
|
|
|
|
if (ptr) {
|
|
return ptr;
|
|
}
|
|
}
|
|
}
|
|
|
|
address = base;
|
|
}
|
|
|
|
return nullptr;
|
|
}
|
|
|
|
void OsFreeNear(void* ptr) {
|
|
if (ptr) {
|
|
VirtualFree(ptr, 0, MEM_RELEASE);
|
|
}
|
|
}
|
|
|
|
#endif
|