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