Ryujinx/Ryujinx.HLE/HOS/Kernel/KTlsPageManager.cs

60 lines
1.3 KiB
C#
Raw Normal View History

using System;
namespace Ryujinx.HLE.HOS.Kernel
{
class KTlsPageManager
{
private const int TlsEntrySize = 0x200;
2018-12-01 21:01:59 +01:00
private long _pagePosition;
2018-12-01 21:01:59 +01:00
private int _usedSlots;
2018-12-01 21:01:59 +01:00
private bool[] _slots;
2018-12-01 21:01:59 +01:00
public bool IsEmpty => _usedSlots == 0;
public bool IsFull => _usedSlots == _slots.Length;
2018-12-01 21:01:59 +01:00
public KTlsPageManager(long pagePosition)
{
2018-12-01 21:24:37 +01:00
_pagePosition = pagePosition;
2018-12-01 21:01:59 +01:00
_slots = new bool[KMemoryManager.PageSize / TlsEntrySize];
}
2018-12-01 21:01:59 +01:00
public bool TryGetFreeTlsAddr(out long position)
{
2018-12-01 21:01:59 +01:00
position = _pagePosition;
2018-12-01 21:01:59 +01:00
for (int index = 0; index < _slots.Length; index++)
{
2018-12-01 21:01:59 +01:00
if (!_slots[index])
{
2018-12-01 21:01:59 +01:00
_slots[index] = true;
2018-12-01 21:01:59 +01:00
_usedSlots++;
return true;
}
2018-12-01 21:01:59 +01:00
position += TlsEntrySize;
}
2018-12-01 21:01:59 +01:00
position = 0;
return false;
}
2018-12-01 21:01:59 +01:00
public void FreeTlsSlot(int slot)
{
2018-12-01 21:01:59 +01:00
if ((uint)slot > _slots.Length)
{
2018-12-01 21:01:59 +01:00
throw new ArgumentOutOfRangeException(nameof(slot));
}
2018-12-01 21:01:59 +01:00
_slots[slot] = false;
2018-12-01 21:01:59 +01:00
_usedSlots--;
}
}
}