2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Memory
|
2018-11-28 17:18:09 -05: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 09:25:22 -04:00
|
|
|
public bool TryCoalesce(int index, int count)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-04-25 09:25:22 -04:00
|
|
|
long mask = ((1L << count) - 1) << (index & 63);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
index /= 64;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-04-25 09:25:22 -04:00
|
|
|
if (count >= 64)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2020-04-25 09:25:22 -04: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 17:18:09 -05:00
|
|
|
}
|
2020-04-25 09:25:22 -04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
long value = Masks[MaxLevel - 1][index];
|
|
|
|
|
|
|
|
if ((mask & ~value) != 0)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-04-25 09:25:22 -04:00
|
|
|
value &= ~mask;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2020-04-25 09:25:22 -04: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 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
Masks[level][index / 64] &= ~(1L << (index & 63));
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (Masks[level][index / 64] != 0)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|