KHandleTable.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. public const int SelfThreadHandle = (0x1ffff << 15) | 0;
  9. public const int SelfProcessHandle = (0x1ffff << 15) | 1;
  10. private readonly KernelContext _context;
  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(KernelContext context)
  18. {
  19. _context = context;
  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(KAutoObject 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 = (_idCounter << 15) | entry.Index;
  61. obj.IncrementReferenceCount();
  62. if ((short)(_idCounter + 1) >= 0)
  63. {
  64. _idCounter++;
  65. }
  66. else
  67. {
  68. _idCounter = 1;
  69. }
  70. }
  71. return KernelResult.Success;
  72. }
  73. public KernelResult ReserveHandle(out int handle)
  74. {
  75. handle = 0;
  76. lock (_table)
  77. {
  78. if (_activeSlotsCount >= _size)
  79. {
  80. return KernelResult.HandleTableFull;
  81. }
  82. KHandleEntry entry = _nextFreeEntry;
  83. _nextFreeEntry = entry.Next;
  84. _activeSlotsCount++;
  85. handle = (_idCounter << 15) | entry.Index;
  86. if ((short)(_idCounter + 1) >= 0)
  87. {
  88. _idCounter++;
  89. }
  90. else
  91. {
  92. _idCounter = 1;
  93. }
  94. }
  95. return KernelResult.Success;
  96. }
  97. public void CancelHandleReservation(int handle)
  98. {
  99. int index = (handle >> 0) & 0x7fff;
  100. lock (_table)
  101. {
  102. KHandleEntry entry = _table[index];
  103. entry.Obj = null;
  104. entry.Next = _nextFreeEntry;
  105. _nextFreeEntry = entry;
  106. _activeSlotsCount--;
  107. }
  108. }
  109. public void SetReservedHandleObj(int handle, KAutoObject obj)
  110. {
  111. int index = (handle >> 0) & 0x7fff;
  112. int handleId = (handle >> 15);
  113. lock (_table)
  114. {
  115. KHandleEntry entry = _table[index];
  116. entry.Obj = obj;
  117. entry.HandleId = (ushort)handleId;
  118. obj.IncrementReferenceCount();
  119. }
  120. }
  121. public bool CloseHandle(int handle)
  122. {
  123. if ((handle >> 30) != 0 ||
  124. handle == SelfThreadHandle ||
  125. handle == SelfProcessHandle)
  126. {
  127. return false;
  128. }
  129. int index = (handle >> 0) & 0x7fff;
  130. int handleId = (handle >> 15);
  131. KAutoObject obj = null;
  132. bool result = false;
  133. lock (_table)
  134. {
  135. if (handleId != 0 && index < _size)
  136. {
  137. KHandleEntry entry = _table[index];
  138. if ((obj = entry.Obj) != null && entry.HandleId == handleId)
  139. {
  140. entry.Obj = null;
  141. entry.Next = _nextFreeEntry;
  142. _nextFreeEntry = entry;
  143. _activeSlotsCount--;
  144. result = true;
  145. }
  146. }
  147. }
  148. if (result)
  149. {
  150. obj.DecrementReferenceCount();
  151. }
  152. return result;
  153. }
  154. public T GetObject<T>(int handle) where T : KAutoObject
  155. {
  156. int index = (handle >> 0) & 0x7fff;
  157. int handleId = (handle >> 15);
  158. lock (_table)
  159. {
  160. if ((handle >> 30) == 0 && handleId != 0 && index < _size)
  161. {
  162. KHandleEntry entry = _table[index];
  163. if (entry.HandleId == handleId && entry.Obj is T obj)
  164. {
  165. return obj;
  166. }
  167. }
  168. }
  169. return default;
  170. }
  171. public KThread GetKThread(int handle)
  172. {
  173. if (handle == SelfThreadHandle)
  174. {
  175. return _context.Scheduler.GetCurrentThread();
  176. }
  177. else
  178. {
  179. return GetObject<KThread>(handle);
  180. }
  181. }
  182. public KProcess GetKProcess(int handle)
  183. {
  184. if (handle == SelfProcessHandle)
  185. {
  186. return _context.Scheduler.GetCurrentProcess();
  187. }
  188. else
  189. {
  190. return GetObject<KProcess>(handle);
  191. }
  192. }
  193. public void Destroy()
  194. {
  195. lock (_table)
  196. {
  197. for (int index = 0; index < _size; index++)
  198. {
  199. KHandleEntry entry = _table[index];
  200. if (entry.Obj != null)
  201. {
  202. if (entry.Obj is IDisposable disposableObj)
  203. {
  204. disposableObj.Dispose();
  205. }
  206. entry.Obj.DecrementReferenceCount();
  207. entry.Obj = null;
  208. entry.Next = _nextFreeEntry;
  209. _nextFreeEntry = entry;
  210. }
  211. }
  212. }
  213. }
  214. }
  215. }