2018-12-18 06:33:36 +01:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
class KMemoryRegionBlock
|
|
|
|
{
|
|
|
|
public long[][] Masks;
|
|
|
|
|
|
|
|
public ulong FreeCount;
|
|
|
|
public int MaxLevel;
|
|
|
|
public ulong StartAligned;
|
|
|
|
public ulong SizeInBlocksTruncated;
|
|
|
|
public ulong SizeInBlocksRounded;
|
|
|
|
public int Order;
|
|
|
|
public int NextOrder;
|
|
|
|
|
2020-04-25 15:25:22 +02:00
|
|
|
public bool TryCoalesce(int index, int count)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-04-25 15:25:22 +02:00
|
|
|
long mask = ((1L << count) - 1) << (index & 63);
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
index /= 64;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-04-25 15:25:22 +02:00
|
|
|
if (count >= 64)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2020-04-25 15:25:22 +02:00
|
|
|
int remaining = count;
|
|
|
|
int tempIdx = index;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (Masks[MaxLevel - 1][tempIdx++] != -1L)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
remaining -= 64;
|
|
|
|
}
|
|
|
|
while (remaining != 0);
|
|
|
|
|
|
|
|
remaining = count;
|
|
|
|
tempIdx = index;
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
Masks[MaxLevel - 1][tempIdx] = 0;
|
|
|
|
|
|
|
|
ClearMaskBit(MaxLevel - 2, tempIdx++);
|
|
|
|
|
|
|
|
remaining -= 64;
|
|
|
|
}
|
|
|
|
while (remaining != 0);
|
2018-11-28 23:18:09 +01:00
|
|
|
}
|
2020-04-25 15:25:22 +02:00
|
|
|
else
|
|
|
|
{
|
|
|
|
long value = Masks[MaxLevel - 1][index];
|
|
|
|
|
|
|
|
if ((mask & ~value) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-04-25 15:25:22 +02:00
|
|
|
value &= ~mask;
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2020-04-25 15:25:22 +02:00
|
|
|
Masks[MaxLevel - 1][index] = value;
|
|
|
|
|
|
|
|
if (value == 0)
|
|
|
|
{
|
|
|
|
ClearMaskBit(MaxLevel - 2, index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
FreeCount -= (ulong)count;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void ClearMaskBit(int startLevel, int index)
|
|
|
|
{
|
|
|
|
for (int level = startLevel; level >= 0; level--, index /= 64)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
2018-12-06 12:16:24 +01:00
|
|
|
Masks[level][index / 64] &= ~(1L << (index & 63));
|
2018-11-28 23:18:09 +01:00
|
|
|
|
2018-12-06 12:16:24 +01:00
|
|
|
if (Masks[level][index / 64] != 0)
|
2018-11-28 23:18:09 +01:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|