KHandleTable.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. using Ryujinx.HLE.HOS.Kernel.Common;
  2. using Ryujinx.HLE.HOS.Kernel.Threading;
  3. using System;
  4. namespace Ryujinx.HLE.HOS.Kernel.Process
  5. {
  6. class KHandleTable
  7. {
  8. private const int SelfThreadHandle = (0x1ffff << 15) | 0;
  9. private const int SelfProcessHandle = (0x1ffff << 15) | 1;
  10. private Horizon _system;
  11. private KHandleEntry[] _table;
  12. private KHandleEntry _tableHead;
  13. private KHandleEntry _nextFreeEntry;
  14. private int _activeSlotsCount;
  15. private int _size;
  16. private ushort _idCounter;
  17. public KHandleTable(Horizon system)
  18. {
  19. _system = system;
  20. }
  21. public KernelResult Initialize(int size)
  22. {
  23. if ((uint)size > 1024)
  24. {
  25. return KernelResult.OutOfMemory;
  26. }
  27. if (size < 1)
  28. {
  29. size = 1024;
  30. }
  31. _size = size;
  32. _idCounter = 1;
  33. _table = new KHandleEntry[size];
  34. _tableHead = new KHandleEntry(0);
  35. KHandleEntry entry = _tableHead;
  36. for (int index = 0; index < size; index++)
  37. {
  38. _table[index] = entry;
  39. entry.Next = new KHandleEntry(index + 1);
  40. entry = entry.Next;
  41. }
  42. _table[size - 1].Next = null;
  43. _nextFreeEntry = _tableHead;
  44. return KernelResult.Success;
  45. }
  46. public KernelResult GenerateHandle(object obj, out int handle)
  47. {
  48. handle = 0;
  49. lock (_table)
  50. {
  51. if (_activeSlotsCount >= _size)
  52. {
  53. return KernelResult.HandleTableFull;
  54. }
  55. KHandleEntry entry = _nextFreeEntry;
  56. _nextFreeEntry = entry.Next;
  57. entry.Obj = obj;
  58. entry.HandleId = _idCounter;
  59. _activeSlotsCount++;
  60. handle = (int)((_idCounter << 15) & 0xffff8000) | entry.Index;
  61. if ((short)(_idCounter + 1) >= 0)
  62. {
  63. _idCounter++;
  64. }
  65. else
  66. {
  67. _idCounter = 1;
  68. }
  69. }
  70. return KernelResult.Success;
  71. }
  72. public bool CloseHandle(int handle)
  73. {
  74. if ((handle >> 30) != 0 ||
  75. handle == SelfThreadHandle ||
  76. handle == SelfProcessHandle)
  77. {
  78. return false;
  79. }
  80. int index = (handle >> 0) & 0x7fff;
  81. int handleId = (handle >> 15);
  82. bool result = false;
  83. lock (_table)
  84. {
  85. if (handleId != 0 && index < _size)
  86. {
  87. KHandleEntry entry = _table[index];
  88. if (entry.Obj != null && entry.HandleId == handleId)
  89. {
  90. entry.Obj = null;
  91. entry.Next = _nextFreeEntry;
  92. _nextFreeEntry = entry;
  93. _activeSlotsCount--;
  94. result = true;
  95. }
  96. }
  97. }
  98. return result;
  99. }
  100. public T GetObject<T>(int handle)
  101. {
  102. int index = (handle >> 0) & 0x7fff;
  103. int handleId = (handle >> 15);
  104. lock (_table)
  105. {
  106. if ((handle >> 30) == 0 && handleId != 0)
  107. {
  108. KHandleEntry entry = _table[index];
  109. if (entry.HandleId == handleId && entry.Obj is T obj)
  110. {
  111. return obj;
  112. }
  113. }
  114. }
  115. return default(T);
  116. }
  117. public KThread GetKThread(int handle)
  118. {
  119. if (handle == SelfThreadHandle)
  120. {
  121. return _system.Scheduler.GetCurrentThread();
  122. }
  123. else
  124. {
  125. return GetObject<KThread>(handle);
  126. }
  127. }
  128. public KProcess GetKProcess(int handle)
  129. {
  130. if (handle == SelfProcessHandle)
  131. {
  132. return _system.Scheduler.GetCurrentProcess();
  133. }
  134. else
  135. {
  136. return GetObject<KProcess>(handle);
  137. }
  138. }
  139. public void Destroy()
  140. {
  141. lock (_table)
  142. {
  143. for (int index = 0; index < _size; index++)
  144. {
  145. KHandleEntry entry = _table[index];
  146. if (entry.Obj != null)
  147. {
  148. if (entry.Obj is IDisposable disposableObj)
  149. {
  150. disposableObj.Dispose();
  151. }
  152. entry.Obj = null;
  153. entry.Next = _nextFreeEntry;
  154. _nextFreeEntry = entry;
  155. }
  156. }
  157. }
  158. }
  159. }
  160. }