2018-11-28 17:18:09 -05:00
|
|
|
using Ryujinx.Common;
|
|
|
|
using System;
|
|
|
|
|
2018-12-18 00:33:36 -05:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel.Process
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
class KContextIdManager
|
|
|
|
{
|
|
|
|
private const int IdMasksCount = 8;
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private int[] _idMasks;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private int _nextFreeBitHint;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
public KContextIdManager()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_idMasks = new int[IdMasksCount];
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public int GetId()
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_idMasks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int id = 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (!TestBit(_nextFreeBitHint))
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
id = _nextFreeBitHint;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
for (int index = 0; index < IdMasksCount; index++)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int mask = _idMasks[index];
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
int firstFreeBit = BitUtils.CountLeadingZeros32((mask + 1) & ~mask);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
if (firstFreeBit < 32)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
int baseBit = index * 32 + 31;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
id = baseBit - firstFreeBit;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2018-12-06 06:16:24 -05:00
|
|
|
else if (index == IdMasksCount - 1)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Maximum number of Ids reached!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
_nextFreeBitHint = id + 1;
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
SetBit(id);
|
2018-11-28 17:18:09 -05:00
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
return id;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
public void PutId(int id)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
lock (_idMasks)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
ClearBit(id);
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private bool TestBit(int bit)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
return (_idMasks[_nextFreeBitHint / 32] & (1 << (_nextFreeBitHint & 31))) != 0;
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void SetBit(int bit)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_idMasks[_nextFreeBitHint / 32] |= (1 << (_nextFreeBitHint & 31));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
|
2018-12-06 06:16:24 -05:00
|
|
|
private void ClearBit(int bit)
|
2018-11-28 17:18:09 -05:00
|
|
|
{
|
2018-12-06 06:16:24 -05:00
|
|
|
_idMasks[_nextFreeBitHint / 32] &= ~(1 << (_nextFreeBitHint & 31));
|
2018-11-28 17:18:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|