Rename Reader/Writer to SpanReader/SpanWriter and move to Ryujinx.Common.Memory

This commit is contained in:
gdk 2022-12-31 16:54:55 -03:00
parent 30fc25b66e
commit ce86b81961
5 changed files with 106 additions and 65 deletions

View file

@ -0,0 +1,51 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Common.Memory
{
public ref struct SpanReader
{
private ReadOnlySpan<byte> _input;
public int Length => _input.Length;
public SpanReader(ReadOnlySpan<byte> input)
{
_input = input;
}
public T Read<T>() where T : unmanaged
{
T value = MemoryMarshal.Cast<byte, T>(_input)[0];
_input = _input.Slice(Unsafe.SizeOf<T>());
return value;
}
public ReadOnlySpan<byte> GetSpan(int size)
{
ReadOnlySpan<byte> data = _input.Slice(0, size);
_input = _input.Slice(size);
return data;
}
public T ReadAt<T>(int offset) where T : unmanaged
{
return MemoryMarshal.Cast<byte, T>(_input.Slice(offset))[0];
}
public ReadOnlySpan<byte> GetSpanAt(int offset, int size)
{
return _input.Slice(offset, size);
}
public void Skip(int size)
{
_input = _input.Slice(size);
}
}
}

View file

@ -0,0 +1,45 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Common.Memory
{
public ref struct SpanWriter
{
private Span<byte> _output;
public int Length => _output.Length;
public SpanWriter(Span<byte> output)
{
_output = output;
}
public void Write<T>(T value) where T : unmanaged
{
MemoryMarshal.Cast<byte, T>(_output)[0] = value;
_output = _output.Slice(Unsafe.SizeOf<T>());
}
public void Write(ReadOnlySpan<byte> data)
{
data.CopyTo(_output.Slice(0, data.Length));
_output = _output.Slice(data.Length);
}
public void WriteAt<T>(int offset, T value) where T : unmanaged
{
MemoryMarshal.Cast<byte, T>(_output.Slice(offset))[0] = value;
}
public void WriteAt(int offset, ReadOnlySpan<byte> data)
{
data.CopyTo(_output.Slice(offset, data.Length));
}
public void Skip(int size)
{
_output = _output.Slice(size);
}
}
}

View file

@ -1,4 +1,5 @@
using Ryujinx.Common.Logging; using Ryujinx.Common.Logging;
using Ryujinx.Common.Memory;
using Ryujinx.Horizon.Common; using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Lm; using Ryujinx.Horizon.Sdk.Lm;
using Ryujinx.Horizon.Sdk.Sf; using Ryujinx.Horizon.Sdk.Sf;
@ -15,41 +16,6 @@ namespace Ryujinx.Horizon.LogManager
private readonly LmLog _log; private readonly LmLog _log;
private readonly ulong _clientProcessId; private readonly ulong _clientProcessId;
private ref struct Reader
{
private ReadOnlySpan<byte> _message;
public int Length => _message.Length;
public Reader(ReadOnlySpan<byte> message)
{
_message = message;
}
public T Read<T>() where T : unmanaged
{
T value = MemoryMarshal.Cast<byte, T>(_message)[0];
_message = _message.Slice(Unsafe.SizeOf<T>());
return value;
}
public ReadOnlySpan<byte> GetSpan(int size)
{
ReadOnlySpan<byte> data = _message.Slice(0, size);
_message = _message.Slice(size);
return data;
}
public void Skip(int size)
{
_message = _message.Slice(size);
}
}
public LmLogger(LmLog log, ulong clientProcessId) public LmLogger(LmLog log, ulong clientProcessId)
{ {
_log = log; _log = log;
@ -97,7 +63,7 @@ namespace Ryujinx.Horizon.LogManager
private static string LogImpl(ReadOnlySpan<byte> message) private static string LogImpl(ReadOnlySpan<byte> message)
{ {
Reader reader = new Reader(message); SpanReader reader = new SpanReader(message);
LogPacketHeader header = reader.Read<LogPacketHeader>(); LogPacketHeader header = reader.Read<LogPacketHeader>();
@ -151,7 +117,7 @@ namespace Ryujinx.Horizon.LogManager
return sb.ToString(); return sb.ToString();
} }
private static int ReadUleb128(ref Reader reader) private static int ReadUleb128(ref SpanReader reader)
{ {
int result = 0; int result = 0;
int count = 0; int count = 0;

View file

@ -1,4 +1,5 @@
using Ryujinx.Horizon.Common; using Ryujinx.Common.Memory;
using Ryujinx.Horizon.Common;
using Ryujinx.Horizon.Sdk.Sf.Cmif; using Ryujinx.Horizon.Sdk.Sf.Cmif;
using System; using System;
@ -32,7 +33,7 @@ namespace Ryujinx.Horizon.Sdk.Sm
{ {
Span<byte> data = stackalloc byte[8]; Span<byte> data = stackalloc byte[8];
Writer writer = new Writer(data); SpanWriter writer = new SpanWriter(data);
writer.Write(0UL); writer.Write(0UL);
@ -43,7 +44,7 @@ namespace Ryujinx.Horizon.Sdk.Sm
{ {
Span<byte> data = stackalloc byte[8]; Span<byte> data = stackalloc byte[8];
Writer writer = new Writer(data); SpanWriter writer = new SpanWriter(data);
writer.Write(name); writer.Write(name);
@ -63,7 +64,7 @@ namespace Ryujinx.Horizon.Sdk.Sm
{ {
Span<byte> data = stackalloc byte[16]; Span<byte> data = stackalloc byte[16];
Writer writer = new Writer(data); SpanWriter writer = new SpanWriter(data);
writer.Write(name); writer.Write(name);
writer.Write(isLight ? 1 : 0); writer.Write(isLight ? 1 : 0);
@ -85,7 +86,7 @@ namespace Ryujinx.Horizon.Sdk.Sm
{ {
Span<byte> data = stackalloc byte[8]; Span<byte> data = stackalloc byte[8];
Writer writer = new Writer(data); SpanWriter writer = new SpanWriter(data);
writer.Write(name); writer.Write(name);
@ -96,7 +97,7 @@ namespace Ryujinx.Horizon.Sdk.Sm
{ {
Span<byte> data = stackalloc byte[8]; Span<byte> data = stackalloc byte[8];
Writer writer = new Writer(data); SpanWriter writer = new SpanWriter(data);
writer.Write(0UL); writer.Write(0UL);

View file

@ -1,22 +0,0 @@
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Ryujinx.Horizon.Sdk
{
ref struct Writer
{
private Span<byte> _output;
public Writer(Span<byte> output)
{
_output = output;
}
public void Write<T>(T value) where T : unmanaged
{
MemoryMarshal.Cast<byte, T>(_output)[0] = value;
_output = _output.Slice(Unsafe.SizeOf<T>());
}
}
}