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