blob: 682f08d4ff0b208c69d6589bdbff52550e1e7eff (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
|
using System;
namespace Ryujinx.HLE.HOS.Kernel
{
class KProcessHandleTable
{
private const int SelfThreadHandle = (0x1ffff << 15) | 0;
private const int SelfProcessHandle = (0x1ffff << 15) | 1;
private Horizon System;
private KHandleEntry[] Table;
private KHandleEntry TableHead;
private KHandleEntry NextFreeEntry;
private int ActiveSlotsCount;
private int Size;
private ushort IdCounter;
private object LockObj;
public KProcessHandleTable(Horizon System, int Size = 1024)
{
this.System = System;
this.Size = Size;
IdCounter = 1;
Table = new KHandleEntry[Size];
TableHead = new KHandleEntry(0);
KHandleEntry Entry = TableHead;
for (int Index = 0; Index < Size; Index++)
{
Table[Index] = Entry;
Entry.Next = new KHandleEntry(Index + 1);
Entry = Entry.Next;
}
Table[Size - 1].Next = null;
NextFreeEntry = TableHead;
LockObj = new object();
}
public KernelResult GenerateHandle(object Obj, out int Handle)
{
Handle = 0;
lock (LockObj)
{
if (ActiveSlotsCount >= Size)
{
return KernelResult.HandleTableFull;
}
KHandleEntry Entry = NextFreeEntry;
NextFreeEntry = Entry.Next;
Entry.Obj = Obj;
Entry.HandleId = IdCounter;
ActiveSlotsCount++;
Handle = (int)((IdCounter << 15) & (uint)0xffff8000) | Entry.Index;
if ((short)(IdCounter + 1) >= 0)
{
IdCounter++;
}
else
{
IdCounter = 1;
}
}
return KernelResult.Success;
}
public bool CloseHandle(int Handle)
{
if ((Handle >> 30) != 0 ||
Handle == SelfThreadHandle ||
Handle == SelfProcessHandle)
{
return false;
}
int Index = (Handle >> 0) & 0x7fff;
int HandleId = (Handle >> 15);
bool Result = false;
lock (LockObj)
{
if (HandleId != 0 && Index < Size)
{
KHandleEntry Entry = Table[Index];
if (Entry.Obj != null && Entry.HandleId == HandleId)
{
Entry.Obj = null;
Entry.Next = NextFreeEntry;
NextFreeEntry = Entry;
ActiveSlotsCount--;
Result = true;
}
}
}
return Result;
}
public T GetObject<T>(int Handle)
{
int Index = (Handle >> 0) & 0x7fff;
int HandleId = (Handle >> 15);
lock (LockObj)
{
if ((Handle >> 30) == 0 && HandleId != 0)
{
KHandleEntry Entry = Table[Index];
if (Entry.HandleId == HandleId && Entry.Obj is T Obj)
{
return Obj;
}
}
}
return default(T);
}
public KThread GetKThread(int Handle)
{
if (Handle == SelfThreadHandle)
{
return System.Scheduler.GetCurrentThread();
}
else
{
return GetObject<KThread>(Handle);
}
}
public void Destroy()
{
lock (LockObj)
{
for (int Index = 0; Index < Size; Index++)
{
KHandleEntry Entry = Table[Index];
if (Entry.Obj != null)
{
if (Entry.Obj is IDisposable DisposableObj)
{
DisposableObj.Dispose();
}
Entry.Obj = null;
Entry.Next = NextFreeEntry;
NextFreeEntry = Entry;
}
}
}
}
}
}
|