2018-09-23 18:11:46 +00:00
|
|
|
using System;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-08-16 23:47:36 +00:00
|
|
|
namespace Ryujinx.HLE.HOS.Kernel
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
class KHandleTable
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-09-23 18:11:46 +00:00
|
|
|
private const int SelfThreadHandle = (0x1ffff << 15) | 0;
|
|
|
|
private const int SelfProcessHandle = (0x1ffff << 15) | 1;
|
2018-03-12 04:04:52 +00:00
|
|
|
|
2018-09-23 18:11:46 +00:00
|
|
|
private Horizon System;
|
|
|
|
|
|
|
|
private KHandleEntry[] Table;
|
|
|
|
|
|
|
|
private KHandleEntry TableHead;
|
|
|
|
private KHandleEntry NextFreeEntry;
|
|
|
|
|
|
|
|
private int ActiveSlotsCount;
|
|
|
|
|
|
|
|
private int Size;
|
|
|
|
|
|
|
|
private ushort IdCounter;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KHandleTable(Horizon System)
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-09-23 18:11:46 +00:00
|
|
|
this.System = System;
|
2018-11-28 22:18:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult Initialize(int Size)
|
|
|
|
{
|
|
|
|
if ((uint)Size > 1024)
|
|
|
|
{
|
|
|
|
return KernelResult.OutOfMemory;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Size < 1)
|
|
|
|
{
|
|
|
|
Size = 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.Size = Size;
|
2018-09-23 18:11:46 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
return KernelResult.Success;
|
2018-09-23 18:11:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public KernelResult GenerateHandle(object Obj, out int Handle)
|
|
|
|
{
|
|
|
|
Handle = 0;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
lock (Table)
|
2018-09-23 18:11:46 +00:00
|
|
|
{
|
|
|
|
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;
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:11:46 +00:00
|
|
|
public bool CloseHandle(int Handle)
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-09-23 18:11:46 +00:00
|
|
|
if ((Handle >> 30) != 0 ||
|
|
|
|
Handle == SelfThreadHandle ||
|
|
|
|
Handle == SelfProcessHandle)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
int Index = (Handle >> 0) & 0x7fff;
|
2018-09-23 18:11:46 +00:00
|
|
|
int HandleId = (Handle >> 15);
|
|
|
|
|
|
|
|
bool Result = false;
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
lock (Table)
|
2018-09-23 18:11:46 +00:00
|
|
|
{
|
|
|
|
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;
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:11:46 +00:00
|
|
|
public T GetObject<T>(int Handle)
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
int Index = (Handle >> 0) & 0x7fff;
|
2018-09-23 18:11:46 +00:00
|
|
|
int HandleId = (Handle >> 15);
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
lock (Table)
|
2018-09-23 18:11:46 +00:00
|
|
|
{
|
|
|
|
if ((Handle >> 30) == 0 && HandleId != 0)
|
|
|
|
{
|
|
|
|
KHandleEntry Entry = Table[Index];
|
|
|
|
|
|
|
|
if (Entry.HandleId == HandleId && Entry.Obj is T Obj)
|
|
|
|
{
|
|
|
|
return Obj;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return default(T);
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2018-09-23 18:11:46 +00:00
|
|
|
public KThread GetKThread(int Handle)
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-09-23 18:11:46 +00:00
|
|
|
if (Handle == SelfThreadHandle)
|
|
|
|
{
|
|
|
|
return System.Scheduler.GetCurrentThread();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return GetObject<KThread>(Handle);
|
|
|
|
}
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
|
2018-11-28 22:18:09 +00:00
|
|
|
public KProcess GetKProcess(int Handle)
|
|
|
|
{
|
|
|
|
if (Handle == SelfProcessHandle)
|
|
|
|
{
|
|
|
|
return System.Scheduler.GetCurrentProcess();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return GetObject<KProcess>(Handle);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-23 18:11:46 +00:00
|
|
|
public void Destroy()
|
2018-03-12 04:04:52 +00:00
|
|
|
{
|
2018-11-28 22:18:09 +00:00
|
|
|
lock (Table)
|
2018-09-23 18:11:46 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-12 04:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|