ryujinx/Ryujinx.HLE/HOS/Ipc/IpcHandleDesc.cs
jhorv 5131b71437
Reducing memory allocations (#4537)
* add RecyclableMemoryStream dependency and MemoryStreamManager

* organize BinaryReader/BinaryWriter extensions

* add StreamExtensions to reduce need for BinaryWriter

* simple replacments of MemoryStream with RecyclableMemoryStream

* add write ReadOnlySequence<byte> support to IVirtualMemoryManager

* avoid 0-length array creation

* rework IpcMessage and related types to greatly reduce memory allocation by using RecylableMemoryStream, keeping streams around longer, avoiding their creation when possible, and avoiding creation of BinaryReader and BinaryWriter when possible

* reduce LINQ-induced memory allocations with custom methods to query KPriorityQueue

* use RecyclableMemoryStream in StreamUtils, and use StreamUtils in EmbeddedResources

* add constants for nanosecond/millisecond conversions

* code formatting

* XML doc adjustments

* fix: StreamExtension.WriteByte not writing non-zero values for lengths <= 16

* XML Doc improvements. Implement StreamExtensions.WriteByte() block writes for large-enough count values.

* add copyless path for StreamExtension.Write(ReadOnlySpan<int>)

* add default implementation of IVirtualMemoryManager.Write(ulong, ReadOnlySequence<byte>); remove previous explicit implementations

* code style fixes

* remove LINQ completely from KScheduler/KPriorityQueue by implementing a custom struct-based enumerator
2023-03-17 13:14:50 +01:00

93 lines
2.3 KiB
C#

using Microsoft.IO;
using Ryujinx.Common;
using Ryujinx.Common.Memory;
using System;
using System.IO;
namespace Ryujinx.HLE.HOS.Ipc
{
class IpcHandleDesc
{
public bool HasPId { get; private set; }
public ulong PId { get; private set; }
public int[] ToCopy { get; private set; }
public int[] ToMove { get; private set; }
public IpcHandleDesc(BinaryReader reader)
{
int word = reader.ReadInt32();
HasPId = (word & 1) != 0;
PId = HasPId ? reader.ReadUInt64() : 0;
int toCopySize = (word >> 1) & 0xf;
int[] toCopy = toCopySize == 0 ? Array.Empty<int>() : new int[toCopySize];
for (int index = 0; index < toCopy.Length; index++)
{
toCopy[index] = reader.ReadInt32();
}
ToCopy = toCopy;
int toMoveSize = (word >> 5) & 0xf;
int[] toMove = toMoveSize == 0 ? Array.Empty<int>() : new int[toMoveSize];
for (int index = 0; index < toMove.Length; index++)
{
toMove[index] = reader.ReadInt32();
}
ToMove = toMove;
}
public IpcHandleDesc(int[] copy, int[] move)
{
ToCopy = copy ?? throw new ArgumentNullException(nameof(copy));
ToMove = move ?? throw new ArgumentNullException(nameof(move));
}
public IpcHandleDesc(int[] copy, int[] move, ulong pId) : this(copy, move)
{
PId = pId;
HasPId = true;
}
public static IpcHandleDesc MakeCopy(params int[] handles)
{
return new IpcHandleDesc(handles, Array.Empty<int>());
}
public static IpcHandleDesc MakeMove(params int[] handles)
{
return new IpcHandleDesc(Array.Empty<int>(), handles);
}
public RecyclableMemoryStream GetStream()
{
RecyclableMemoryStream ms = MemoryStreamManager.Shared.GetStream();
int word = HasPId ? 1 : 0;
word |= (ToCopy.Length & 0xf) << 1;
word |= (ToMove.Length & 0xf) << 5;
ms.Write(word);
if (HasPId)
{
ms.Write(PId);
}
ms.Write(ToCopy);
ms.Write(ToMove);
ms.Position = 0;
return ms;
}
}
}