diff options
author | TSR Berry <20988865+TSRBerry@users.noreply.github.com> | 2023-04-08 01:22:00 +0200 |
---|---|---|
committer | Mary <thog@protonmail.com> | 2023-04-27 23:51:14 +0200 |
commit | cee712105850ac3385cd0091a923438167433f9f (patch) | |
tree | 4a5274b21d8b7f938c0d0ce18736d3f2993b11b1 /src/Ryujinx.Common/Memory/MemoryStreamManager.cs | |
parent | cd124bda587ef09668a971fa1cac1c3f0cfc9f21 (diff) |
Move solution and projects to src
Diffstat (limited to 'src/Ryujinx.Common/Memory/MemoryStreamManager.cs')
-rw-r--r-- | src/Ryujinx.Common/Memory/MemoryStreamManager.cs | 99 |
1 files changed, 99 insertions, 0 deletions
diff --git a/src/Ryujinx.Common/Memory/MemoryStreamManager.cs b/src/Ryujinx.Common/Memory/MemoryStreamManager.cs new file mode 100644 index 00000000..68b82999 --- /dev/null +++ b/src/Ryujinx.Common/Memory/MemoryStreamManager.cs @@ -0,0 +1,99 @@ +using Microsoft.IO; +using System; + +namespace Ryujinx.Common.Memory +{ + public static class MemoryStreamManager + { + private static readonly RecyclableMemoryStreamManager _shared = new RecyclableMemoryStreamManager(); + + /// <summary> + /// We don't expose the <c>RecyclableMemoryStreamManager</c> directly because version 2.x + /// returns them as <c>MemoryStream</c>. This Shared class is here to a) offer only the GetStream() versions we use + /// and b) return them as <c>RecyclableMemoryStream</c> so we don't have to cast. + /// </summary> + public static class Shared + { + /// <summary> + /// Retrieve a new <c>MemoryStream</c> object with no tag and a default initial capacity. + /// </summary> + /// <returns>A <c>RecyclableMemoryStream</c></returns> + public static RecyclableMemoryStream GetStream() + => new RecyclableMemoryStream(_shared); + + /// <summary> + /// Retrieve a new <c>MemoryStream</c> object with the contents copied from the provided + /// buffer. The provided buffer is not wrapped or used after construction. + /// </summary> + /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks> + /// <param name="buffer">The byte buffer to copy data from</param> + /// <returns>A <c>RecyclableMemoryStream</c></returns> + public static RecyclableMemoryStream GetStream(byte[] buffer) + => GetStream(Guid.NewGuid(), null, buffer, 0, buffer.Length); + + /// <summary> + /// Retrieve a new <c>MemoryStream</c> object with the given tag and with contents copied from the provided + /// buffer. The provided buffer is not wrapped or used after construction. + /// </summary> + /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks> + /// <param name="buffer">The byte buffer to copy data from</param> + /// <returns>A <c>RecyclableMemoryStream</c></returns> + public static RecyclableMemoryStream GetStream(ReadOnlySpan<byte> buffer) + => GetStream(Guid.NewGuid(), null, buffer); + + /// <summary> + /// Retrieve a new <c>RecyclableMemoryStream</c> object with the given tag and with contents copied from the provided + /// buffer. The provided buffer is not wrapped or used after construction. + /// </summary> + /// <remarks>The new stream's position is set to the beginning of the stream when returned.</remarks> + /// <param name="id">A unique identifier which can be used to trace usages of the stream</param> + /// <param name="tag">A tag which can be used to track the source of the stream</param> + /// <param name="buffer">The byte buffer to copy data from</param> + /// <returns>A <c>RecyclableMemoryStream</c></returns> + public static RecyclableMemoryStream GetStream(Guid id, string tag, ReadOnlySpan<byte> buffer) + { + RecyclableMemoryStream stream = null; + try + { + stream = new RecyclableMemoryStream(_shared, id, tag, buffer.Length); + stream.Write(buffer); + stream.Position = 0; + return stream; + } + catch + { + stream?.Dispose(); + throw; + } + } + + /// <summary> + /// Retrieve a new <c>RecyclableMemoryStream</c> object with the given tag and with contents copied from the provided + /// buffer. The provided buffer is not wrapped or used after construction. + /// </summary> + /// <remarks>The new stream's position is set to the beginning of the stream when returned</remarks> + /// <param name="id">A unique identifier which can be used to trace usages of the stream</param> + /// <param name="tag">A tag which can be used to track the source of the stream</param> + /// <param name="buffer">The byte buffer to copy data from</param> + /// <param name="offset">The offset from the start of the buffer to copy from</param> + /// <param name="count">The number of bytes to copy from the buffer</param> + /// <returns>A <c>RecyclableMemoryStream</c></returns> + public static RecyclableMemoryStream GetStream(Guid id, string tag, byte[] buffer, int offset, int count) + { + RecyclableMemoryStream stream = null; + try + { + stream = new RecyclableMemoryStream(_shared, id, tag, count); + stream.Write(buffer, offset, count); + stream.Position = 0; + return stream; + } + catch + { + stream?.Dispose(); + throw; + } + } + } + } +} |