Ryujinx/Ryujinx.HLE/HOS/Kernel/KHandleTable.cs

207 lines
4.9 KiB
C#
Raw Normal View History

using System;
namespace Ryujinx.HLE.HOS.Kernel
{
class KHandleTable
{
private const int SelfThreadHandle = (0x1ffff << 15) | 0;
private const int SelfProcessHandle = (0x1ffff << 15) | 1;
2018-12-01 20:01:59 +00:00
private Horizon _system;
2018-12-01 20:01:59 +00:00
private KHandleEntry[] _table;
2018-12-01 20:01:59 +00:00
private KHandleEntry _tableHead;
private KHandleEntry _nextFreeEntry;
2018-12-01 20:01:59 +00:00
private int _activeSlotsCount;
2018-12-01 20:01:59 +00:00
private int _size;
2018-12-01 20:01:59 +00:00
private ushort _idCounter;
2018-12-01 20:01:59 +00:00
public KHandleTable(Horizon system)
{
2018-12-01 20:24:37 +00:00
_system = system;
}
2018-12-01 20:01:59 +00:00
public KernelResult Initialize(int size)
{
2018-12-01 20:01:59 +00:00
if ((uint)size > 1024)
{
return KernelResult.OutOfMemory;
}
2018-12-01 20:01:59 +00:00
if (size < 1)
{
2018-12-01 20:01:59 +00:00
size = 1024;
}
2018-12-01 20:24:37 +00:00
_size = size;
2018-12-01 20:01:59 +00:00
_idCounter = 1;
2018-12-01 20:01:59 +00:00
_table = new KHandleEntry[size];
2018-12-01 20:01:59 +00:00
_tableHead = new KHandleEntry(0);
2018-12-01 20:01:59 +00:00
KHandleEntry entry = _tableHead;
2018-12-01 20:01:59 +00:00
for (int index = 0; index < size; index++)
{
2018-12-01 20:01:59 +00:00
_table[index] = entry;
2018-12-01 20:01:59 +00:00
entry.Next = new KHandleEntry(index + 1);
2018-12-01 20:01:59 +00:00
entry = entry.Next;
}
2018-12-01 20:01:59 +00:00
_table[size - 1].Next = null;
2018-12-01 20:01:59 +00:00
_nextFreeEntry = _tableHead;
return KernelResult.Success;
}
2018-12-01 20:01:59 +00:00
public KernelResult GenerateHandle(object obj, out int handle)
{
2018-12-01 20:01:59 +00:00
handle = 0;
2018-12-01 20:01:59 +00:00
lock (_table)
{
2018-12-01 20:01:59 +00:00
if (_activeSlotsCount >= _size)
{
return KernelResult.HandleTableFull;
}
2018-12-01 20:01:59 +00:00
KHandleEntry entry = _nextFreeEntry;
2018-12-01 20:01:59 +00:00
_nextFreeEntry = entry.Next;
2018-12-01 20:01:59 +00:00
entry.Obj = obj;
entry.HandleId = _idCounter;
2018-12-01 20:01:59 +00:00
_activeSlotsCount++;
2018-12-01 20:01:59 +00:00
handle = (int)((_idCounter << 15) & (uint)0xffff8000) | entry.Index;
2018-12-01 20:01:59 +00:00
if ((short)(_idCounter + 1) >= 0)
{
2018-12-01 20:01:59 +00:00
_idCounter++;
}
else
{
2018-12-01 20:01:59 +00:00
_idCounter = 1;
}
}
return KernelResult.Success;
}
2018-12-01 20:01:59 +00:00
public bool CloseHandle(int handle)
{
2018-12-01 20:01:59 +00:00
if ((handle >> 30) != 0 ||
handle == SelfThreadHandle ||
handle == SelfProcessHandle)
{
return false;
}
2018-12-01 20:01:59 +00:00
int index = (handle >> 0) & 0x7fff;
int handleId = (handle >> 15);
2018-12-01 20:01:59 +00:00
bool result = false;
2018-12-01 20:01:59 +00:00
lock (_table)
{
2018-12-01 20:01:59 +00:00
if (handleId != 0 && index < _size)
{
2018-12-01 20:01:59 +00:00
KHandleEntry entry = _table[index];
2018-12-01 20:01:59 +00:00
if (entry.Obj != null && entry.HandleId == handleId)
{
2018-12-01 20:01:59 +00:00
entry.Obj = null;
entry.Next = _nextFreeEntry;
2018-12-01 20:01:59 +00:00
_nextFreeEntry = entry;
2018-12-01 20:01:59 +00:00
_activeSlotsCount--;
2018-12-01 20:01:59 +00:00
result = true;
}
}
}
2018-12-01 20:01:59 +00:00
return result;
}
2018-12-01 20:01:59 +00:00
public T GetObject<T>(int handle)
{
2018-12-01 20:01:59 +00:00
int index = (handle >> 0) & 0x7fff;
int handleId = (handle >> 15);
2018-12-01 20:01:59 +00:00
lock (_table)
{
2018-12-01 20:01:59 +00:00
if ((handle >> 30) == 0 && handleId != 0)
{
2018-12-01 20:01:59 +00:00
KHandleEntry entry = _table[index];
2018-12-01 20:01:59 +00:00
if (entry.HandleId == handleId && entry.Obj is T obj)
{
2018-12-01 20:01:59 +00:00
return obj;
}
}
}
return default(T);
}
2018-12-01 20:01:59 +00:00
public KThread GetKThread(int handle)
{
2018-12-01 20:01:59 +00:00
if (handle == SelfThreadHandle)
{
2018-12-01 20:01:59 +00:00
return _system.Scheduler.GetCurrentThread();
}
else
{
2018-12-01 20:01:59 +00:00
return GetObject<KThread>(handle);
}
}
2018-12-01 20:01:59 +00:00
public KProcess GetKProcess(int handle)
{
2018-12-01 20:01:59 +00:00
if (handle == SelfProcessHandle)
{
2018-12-01 20:01:59 +00:00
return _system.Scheduler.GetCurrentProcess();
}
else
{
2018-12-01 20:01:59 +00:00
return GetObject<KProcess>(handle);
}
}
public void Destroy()
{
2018-12-01 20:01:59 +00:00
lock (_table)
{
2018-12-01 20:01:59 +00:00
for (int index = 0; index < _size; index++)
{
2018-12-01 20:01:59 +00:00
KHandleEntry entry = _table[index];
2018-12-01 20:01:59 +00:00
if (entry.Obj != null)
{
2018-12-01 20:01:59 +00:00
if (entry.Obj is IDisposable disposableObj)
{
2018-12-01 20:01:59 +00:00
disposableObj.Dispose();
}
2018-12-01 20:01:59 +00:00
entry.Obj = null;
entry.Next = _nextFreeEntry;
2018-12-01 20:01:59 +00:00
_nextFreeEntry = entry;
}
}
}
}
}
}