Small refactor

This commit is contained in:
tomcw 2024-03-29 19:38:28 +00:00
parent da8359bd9f
commit d9e68e9cdd
1 changed files with 7 additions and 8 deletions

View File

@ -1537,7 +1537,7 @@ static LPBYTE AllocMemImage(void)
// Allocate memory for 'memimage' (and the alias 'mem')
// . Setup so we have 2 consecutive virtual 64K pages pointing to the same physical 64K page.
// . This is a fix (and optimisation) for 6502 opcodes that do a 16-bit at 6502 address $FFFF. (GH#1285)
// . This is a fix (and optimisation) for 6502 opcodes that do a 16-bit read at 6502 address $FFFF. (GH#1285)
SYSTEM_INFO info;
GetSystemInfo(&info);
bool res = info.dwAllocationGranularity <= _6502_MEM_LEN;
@ -1559,23 +1559,22 @@ static LPBYTE AllocMemImage(void)
{
VirtualFree(baseAddr, 0, MEM_RELEASE);
res = true;
for (UINT i = 0; i < num64KPages; i++)
UINT count = 0;
while (count < num64KPages)
{
// MSDN: "To specify a suggested base address for the view, use the MapViewOfFileEx function. However, this practice is not recommended."
// This is why we retry 10 times.
if (!MapViewOfFileEx(g_hMemImage, FILE_MAP_ALL_ACCESS, 0, 0, _6502_MEM_LEN, baseAddr + i * _6502_MEM_LEN))
{
res = false;
if (!MapViewOfFileEx(g_hMemImage, FILE_MAP_ALL_ACCESS, 0, 0, _6502_MEM_LEN, baseAddr + count * _6502_MEM_LEN))
break;
}
count++;
}
res = (count == num64KPages);
if (res)
break;
// Failed this time, so clean-up and retry...
for (UINT i = 0; i < num64KPages; i++)
for (UINT i = 0; i < count; i++)
UnmapViewOfFile(baseAddr + i * _6502_MEM_LEN);
}
}