aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Core/OsHle/Utilities
diff options
context:
space:
mode:
authoremmauss <emmausssss@gmail.com>2018-02-20 22:09:23 +0200
committergdkchan <gab.dark.100@gmail.com>2018-02-20 17:09:23 -0300
commit62b827f474f0aa2152dd339fcc7cf31084e16a0b (patch)
tree0e5c55b341aee4db0ccb841a084f253ec5e05657 /Ryujinx.Core/OsHle/Utilities
parentcb665bb715834526d73c9469d16114b287faaecd (diff)
Split main project into core,graphics and chocolarm4 subproject (#29)
Diffstat (limited to 'Ryujinx.Core/OsHle/Utilities')
-rw-r--r--Ryujinx.Core/OsHle/Utilities/IdPool.cs53
-rw-r--r--Ryujinx.Core/OsHle/Utilities/IdPoolWithObj.cs78
-rw-r--r--Ryujinx.Core/OsHle/Utilities/MemReader.cs44
-rw-r--r--Ryujinx.Core/OsHle/Utilities/MemWriter.cs38
4 files changed, 213 insertions, 0 deletions
diff --git a/Ryujinx.Core/OsHle/Utilities/IdPool.cs b/Ryujinx.Core/OsHle/Utilities/IdPool.cs
new file mode 100644
index 00000000..a7e181fa
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Utilities/IdPool.cs
@@ -0,0 +1,53 @@
+using System.Collections.Generic;
+
+namespace Ryujinx.Core.OsHle.Utilities
+{
+ class IdPool
+ {
+ private HashSet<int> Ids;
+
+ private int CurrId;
+ private int MinId;
+ private int MaxId;
+
+ public IdPool(int Min, int Max)
+ {
+ Ids = new HashSet<int>();
+
+ CurrId = Min;
+ MinId = Min;
+ MaxId = Max;
+ }
+
+ public IdPool() : this(1, int.MaxValue) { }
+
+ public int GenerateId()
+ {
+ lock (Ids)
+ {
+ for (int Cnt = MinId; Cnt < MaxId; Cnt++)
+ {
+ if (Ids.Add(CurrId))
+ {
+ return CurrId;
+ }
+
+ if (CurrId++ == MaxId)
+ {
+ CurrId = MinId;
+ }
+ }
+
+ return -1;
+ }
+ }
+
+ public bool DeleteId(int Id)
+ {
+ lock (Ids)
+ {
+ return Ids.Remove(Id);
+ }
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Utilities/IdPoolWithObj.cs b/Ryujinx.Core/OsHle/Utilities/IdPoolWithObj.cs
new file mode 100644
index 00000000..f0a339df
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Utilities/IdPoolWithObj.cs
@@ -0,0 +1,78 @@
+using System;
+using System.Collections;
+using System.Collections.Concurrent;
+using System.Collections.Generic;
+
+namespace Ryujinx.Core.OsHle.Utilities
+{
+ class IdPoolWithObj : IEnumerable<KeyValuePair<int, object>>
+ {
+ private IdPool Ids;
+
+ private ConcurrentDictionary<int, object> Objs;
+
+ public IdPoolWithObj()
+ {
+ Ids = new IdPool();
+
+ Objs = new ConcurrentDictionary<int, object>();
+ }
+
+ public int GenerateId(object Data)
+ {
+ int Id = Ids.GenerateId();
+
+ if (Id == -1 || !Objs.TryAdd(Id, Data))
+ {
+ throw new InvalidOperationException();
+ }
+
+ return Id;
+ }
+
+ public bool ReplaceData(int Id, object Data)
+ {
+ if (Objs.ContainsKey(Id))
+ {
+ Objs[Id] = Data;
+
+ return true;
+ }
+
+ return false;
+ }
+
+ public T GetData<T>(int Id)
+ {
+ if (Objs.TryGetValue(Id, out object Data) && Data is T)
+ {
+ return (T)Data;
+ }
+
+ return default(T);
+ }
+
+ public void Delete(int Id)
+ {
+ if (Objs.TryRemove(Id, out object Obj))
+ {
+ if (Obj is IDisposable DisposableObj)
+ {
+ DisposableObj.Dispose();
+ }
+
+ Ids.DeleteId(Id);
+ }
+ }
+
+ IEnumerator<KeyValuePair<int, object>> IEnumerable<KeyValuePair<int, object>>.GetEnumerator()
+ {
+ return Objs.GetEnumerator();
+ }
+
+ IEnumerator IEnumerable.GetEnumerator()
+ {
+ return Objs.GetEnumerator();
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Utilities/MemReader.cs b/Ryujinx.Core/OsHle/Utilities/MemReader.cs
new file mode 100644
index 00000000..fe92f68f
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Utilities/MemReader.cs
@@ -0,0 +1,44 @@
+using ChocolArm64.Memory;
+
+namespace Ryujinx.Core.OsHle.Utilities
+{
+ class MemReader
+ {
+ private AMemory Memory;
+
+ public long Position { get; private set; }
+
+ public MemReader(AMemory Memory, long Position)
+ {
+ this.Memory = Memory;
+ this.Position = Position;
+ }
+
+ public byte ReadByte()
+ {
+ byte Value = Memory.ReadByte(Position);
+
+ Position++;
+
+ return Value;
+ }
+
+ public int ReadInt32()
+ {
+ int Value = Memory.ReadInt32(Position);
+
+ Position += 4;
+
+ return Value;
+ }
+
+ public long ReadInt64()
+ {
+ long Value = Memory.ReadInt64(Position);
+
+ Position += 8;
+
+ return Value;
+ }
+ }
+} \ No newline at end of file
diff --git a/Ryujinx.Core/OsHle/Utilities/MemWriter.cs b/Ryujinx.Core/OsHle/Utilities/MemWriter.cs
new file mode 100644
index 00000000..21b6a3b6
--- /dev/null
+++ b/Ryujinx.Core/OsHle/Utilities/MemWriter.cs
@@ -0,0 +1,38 @@
+using ChocolArm64.Memory;
+
+namespace Ryujinx.Core.OsHle.Utilities
+{
+ class MemWriter
+ {
+ private AMemory Memory;
+
+ public long Position { get; private set; }
+
+ public MemWriter(AMemory Memory, long Position)
+ {
+ this.Memory = Memory;
+ this.Position = Position;
+ }
+
+ public void WriteByte(byte Value)
+ {
+ Memory.WriteByte(Position, Value);
+
+ Position++;
+ }
+
+ public void WriteInt32(int Value)
+ {
+ Memory.WriteInt32(Position, Value);
+
+ Position += 4;
+ }
+
+ public void WriteInt64(long Value)
+ {
+ Memory.WriteInt64(Position, Value);
+
+ Position += 8;
+ }
+ }
+} \ No newline at end of file