KHandleTable.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using Ryujinx.HLE.HOS.Kernel.Common;
  2. using Ryujinx.HLE.HOS.Kernel.Threading;
  3. using Ryujinx.Horizon.Common;
  4. using System;
  5. namespace Ryujinx.HLE.HOS.Kernel.Process
  6. {
  7. class KHandleTable
  8. {
  9. public const int SelfThreadHandle = (0x1ffff << 15) | 0;
  10. public const int SelfProcessHandle = (0x1ffff << 15) | 1;
  11. private readonly KernelContext _context;
  12. private KHandleEntry[] _table;
  13. private KHandleEntry _tableHead;
  14. private KHandleEntry _nextFreeEntry;
  15. private int _activeSlotsCount;
  16. private uint _size;
  17. private ushort _idCounter;
  18. public KHandleTable(KernelContext context)
  19. {
  20. _context = context;
  21. }
  22. public Result Initialize(uint size)
  23. {
  24. if (size > 1024)
  25. {
  26. return KernelResult.OutOfMemory;
  27. }
  28. if (size < 1)
  29. {
  30. size = 1024;
  31. }
  32. _size = size;
  33. _idCounter = 1;
  34. _table = new KHandleEntry[size];
  35. _tableHead = new KHandleEntry(0);
  36. KHandleEntry entry = _tableHead;
  37. for (int index = 0; index < size; index++)
  38. {
  39. _table[index] = entry;
  40. entry.Next = new KHandleEntry(index + 1);
  41. entry = entry.Next;
  42. }
  43. _table[size - 1].Next = null;
  44. _nextFreeEntry = _tableHead;
  45. return Result.Success;
  46. }
  47. public Result GenerateHandle(KAutoObject obj, out int handle)
  48. {
  49. handle = 0;
  50. lock (_table)
  51. {
  52. if (_activeSlotsCount >= _size)
  53. {
  54. return KernelResult.HandleTableFull;
  55. }
  56. KHandleEntry entry = _nextFreeEntry;
  57. _nextFreeEntry = entry.Next;
  58. entry.Obj = obj;
  59. entry.HandleId = _idCounter;
  60. _activeSlotsCount++;
  61. handle = (_idCounter << 15) | entry.Index;
  62. obj.IncrementReferenceCount();
  63. if ((short)(_idCounter + 1) >= 0)
  64. {
  65. _idCounter++;
  66. }
  67. else
  68. {
  69. _idCounter = 1;
  70. }
  71. }
  72. return Result.Success;
  73. }
  74. public Result ReserveHandle(out int handle)
  75. {
  76. handle = 0;
  77. lock (_table)
  78. {
  79. if (_activeSlotsCount >= _size)
  80. {
  81. return KernelResult.HandleTableFull;
  82. }
  83. KHandleEntry entry = _nextFreeEntry;
  84. _nextFreeEntry = entry.Next;
  85. _activeSlotsCount++;
  86. handle = (_idCounter << 15) | entry.Index;
  87. if ((short)(_idCounter + 1) >= 0)
  88. {
  89. _idCounter++;
  90. }
  91. else
  92. {
  93. _idCounter = 1;
  94. }
  95. }
  96. return Result.Success;
  97. }
  98. public void CancelHandleReservation(int handle)
  99. {
  100. int index = (handle >> 0) & 0x7fff;
  101. lock (_table)
  102. {
  103. KHandleEntry entry = _table[index];
  104. entry.Obj = null;
  105. entry.Next = _nextFreeEntry;
  106. _nextFreeEntry = entry;
  107. _activeSlotsCount--;
  108. }
  109. }
  110. public void SetReservedHandleObj(int handle, KAutoObject obj)
  111. {
  112. int index = (handle >> 0) & 0x7fff;
  113. int handleId = (handle >> 15);
  114. lock (_table)
  115. {
  116. KHandleEntry entry = _table[index];
  117. entry.Obj = obj;
  118. entry.HandleId = (ushort)handleId;
  119. obj.IncrementReferenceCount();
  120. }
  121. }
  122. public bool CloseHandle(int handle)
  123. {
  124. if ((handle >> 30) != 0 ||
  125. handle == SelfThreadHandle ||
  126. handle == SelfProcessHandle)
  127. {
  128. return false;
  129. }
  130. int index = (handle >> 0) & 0x7fff;
  131. int handleId = (handle >> 15);
  132. KAutoObject obj = null;
  133. bool result = false;
  134. lock (_table)
  135. {
  136. if (handleId != 0 && index < _size)
  137. {
  138. KHandleEntry entry = _table[index];
  139. if ((obj = entry.Obj) != null && entry.HandleId == handleId)
  140. {
  141. entry.Obj = null;
  142. entry.Next = _nextFreeEntry;
  143. _nextFreeEntry = entry;
  144. _activeSlotsCount--;
  145. result = true;
  146. }
  147. }
  148. }
  149. if (result)
  150. {
  151. obj.DecrementReferenceCount();
  152. }
  153. return result;
  154. }
  155. public T GetObject<T>(int handle) where T : KAutoObject
  156. {
  157. int index = (handle >> 0) & 0x7fff;
  158. int handleId = (handle >> 15);
  159. lock (_table)
  160. {
  161. if ((handle >> 30) == 0 && handleId != 0 && index < _size)
  162. {
  163. KHandleEntry entry = _table[index];
  164. if (entry.HandleId == handleId && entry.Obj is T obj)
  165. {
  166. return obj;
  167. }
  168. }
  169. }
  170. return default;
  171. }
  172. public KThread GetKThread(int handle)
  173. {
  174. if (handle == SelfThreadHandle)
  175. {
  176. return KernelStatic.GetCurrentThread();
  177. }
  178. else
  179. {
  180. return GetObject<KThread>(handle);
  181. }
  182. }
  183. public KProcess GetKProcess(int handle)
  184. {
  185. if (handle == SelfProcessHandle)
  186. {
  187. return KernelStatic.GetCurrentProcess();
  188. }
  189. else
  190. {
  191. return GetObject<KProcess>(handle);
  192. }
  193. }
  194. public void Destroy()
  195. {
  196. lock (_table)
  197. {
  198. for (int index = 0; index < _size; index++)
  199. {
  200. KHandleEntry entry = _table[index];
  201. if (entry.Obj != null)
  202. {
  203. if (entry.Obj is IDisposable disposableObj)
  204. {
  205. disposableObj.Dispose();
  206. }
  207. entry.Obj.DecrementReferenceCount();
  208. entry.Obj = null;
  209. entry.Next = _nextFreeEntry;
  210. _nextFreeEntry = entry;
  211. }
  212. }
  213. }
  214. }
  215. }
  216. }