From cee712105850ac3385cd0091a923438167433f9f Mon Sep 17 00:00:00 2001 From: TSR Berry <20988865+TSRBerry@users.noreply.github.com> Date: Sat, 8 Apr 2023 01:22:00 +0200 Subject: Move solution and projects to src --- src/Ryujinx.Common/Memory/ByteMemoryPool.cs | 108 ++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) create mode 100644 src/Ryujinx.Common/Memory/ByteMemoryPool.cs (limited to 'src/Ryujinx.Common/Memory/ByteMemoryPool.cs') diff --git a/src/Ryujinx.Common/Memory/ByteMemoryPool.cs b/src/Ryujinx.Common/Memory/ByteMemoryPool.cs new file mode 100644 index 00000000..2910f408 --- /dev/null +++ b/src/Ryujinx.Common/Memory/ByteMemoryPool.cs @@ -0,0 +1,108 @@ +using System; +using System.Buffers; + +namespace Ryujinx.Common.Memory +{ + /// + /// Provides a pool of re-usable byte array instances. + /// + public sealed partial class ByteMemoryPool + { + private static readonly ByteMemoryPool _shared = new ByteMemoryPool(); + + /// + /// Constructs a instance. Private to force access through + /// the instance. + /// + private ByteMemoryPool() + { + // No implementation + } + + /// + /// Retrieves a shared instance. + /// + public static ByteMemoryPool Shared => _shared; + + /// + /// Returns the maximum buffer size supported by this pool. + /// + public int MaxBufferSize => Array.MaxLength; + + /// + /// Rents a byte memory buffer from . + /// The buffer may contain data from a prior use. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner Rent(long length) + => RentImpl(checked((int)length)); + + /// + /// Rents a byte memory buffer from . + /// The buffer may contain data from a prior use. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner Rent(ulong length) + => RentImpl(checked((int)length)); + + /// + /// Rents a byte memory buffer from . + /// The buffer may contain data from a prior use. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner Rent(int length) + => RentImpl(length); + + /// + /// Rents a byte memory buffer from . + /// The buffer's contents are cleared (set to all 0s) before returning. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner RentCleared(long length) + => RentCleared(checked((int)length)); + + /// + /// Rents a byte memory buffer from . + /// The buffer's contents are cleared (set to all 0s) before returning. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner RentCleared(ulong length) + => RentCleared(checked((int)length)); + + /// + /// Rents a byte memory buffer from . + /// The buffer's contents are cleared (set to all 0s) before returning. + /// + /// The buffer's required length in bytes + /// A wrapping the rented memory + /// + public IMemoryOwner RentCleared(int length) + { + var buffer = RentImpl(length); + + buffer.Memory.Span.Clear(); + + return buffer; + } + + private static ByteMemoryPoolBuffer RentImpl(int length) + { + if ((uint)length > Array.MaxLength) + { + throw new ArgumentOutOfRangeException(nameof(length), length, null); + } + + return new ByteMemoryPoolBuffer(length); + } + } +} -- cgit v1.2.3