Ryujinx-uplift/Ryujinx.HLE/HOS/Kernel/KContextIdManager.cs
Alex Barney fb1d9493a3 Adjust naming conventions and general refactoring in HLE Project (#527)
* Rename enum fields

* Naming conventions

* Remove unneeded ".this"

* Remove unneeded semicolons

* Remove unused Usings

* Don't use var

* Remove unneeded enum underlying types

* Explicitly label class visibility

* Remove unneeded @ prefixes

* Remove unneeded commas

* Remove unneeded if expressions

* Method doesn't use unsafe code

* Remove unneeded casts

* Initialized objects don't need an empty constructor

* Remove settings from DotSettings

* Revert "Explicitly label class visibility"

This reverts commit ad5eb5787cc5b27a4631cd46ef5f551c4ae95e51.

* Small changes

* Revert external enum renaming

* Changes from feedback

* Apply previous refactorings to the merged code
2018-12-06 09:16:24 -02:00

83 lines
2.0 KiB
C#

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