KTlsPageManager.cs 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. using Ryujinx.HLE.HOS.Kernel.Memory;
  2. using System;
  3. namespace Ryujinx.HLE.HOS.Kernel.Process
  4. {
  5. class KTlsPageManager
  6. {
  7. private const int TlsEntrySize = 0x200;
  8. private long _pagePosition;
  9. private int _usedSlots;
  10. private bool[] _slots;
  11. public bool IsEmpty => _usedSlots == 0;
  12. public bool IsFull => _usedSlots == _slots.Length;
  13. public KTlsPageManager(long pagePosition)
  14. {
  15. _pagePosition = pagePosition;
  16. _slots = new bool[KPageTableBase.PageSize / TlsEntrySize];
  17. }
  18. public bool TryGetFreeTlsAddr(out long position)
  19. {
  20. position = _pagePosition;
  21. for (int index = 0; index < _slots.Length; index++)
  22. {
  23. if (!_slots[index])
  24. {
  25. _slots[index] = true;
  26. _usedSlots++;
  27. return true;
  28. }
  29. position += TlsEntrySize;
  30. }
  31. position = 0;
  32. return false;
  33. }
  34. public void FreeTlsSlot(int slot)
  35. {
  36. if ((uint)slot > _slots.Length)
  37. {
  38. throw new ArgumentOutOfRangeException(nameof(slot));
  39. }
  40. _slots[slot] = false;
  41. _usedSlots--;
  42. }
  43. }
  44. }