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