ByteMemoryPool.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. using System;
  2. using System.Buffers;
  3. namespace Ryujinx.Common.Memory
  4. {
  5. /// <summary>
  6. /// Provides a pool of re-usable byte array instances.
  7. /// </summary>
  8. public sealed partial class ByteMemoryPool
  9. {
  10. private static readonly ByteMemoryPool _shared = new ByteMemoryPool();
  11. /// <summary>
  12. /// Constructs a <see cref="ByteMemoryPool"/> instance. Private to force access through
  13. /// the <see cref="ByteMemoryPool.Shared"/> instance.
  14. /// </summary>
  15. private ByteMemoryPool()
  16. {
  17. // No implementation
  18. }
  19. /// <summary>
  20. /// Retrieves a shared <see cref="ByteMemoryPool"/> instance.
  21. /// </summary>
  22. public static ByteMemoryPool Shared => _shared;
  23. /// <summary>
  24. /// Returns the maximum buffer size supported by this pool.
  25. /// </summary>
  26. public int MaxBufferSize => Array.MaxLength;
  27. /// <summary>
  28. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  29. /// The buffer may contain data from a prior use.
  30. /// </summary>
  31. /// <param name="length">The buffer's required length in bytes</param>
  32. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  33. /// <exception cref="ArgumentOutOfRangeException"></exception>
  34. public IMemoryOwner<byte> Rent(long length)
  35. => RentImpl(checked((int)length));
  36. /// <summary>
  37. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  38. /// The buffer may contain data from a prior use.
  39. /// </summary>
  40. /// <param name="length">The buffer's required length in bytes</param>
  41. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  42. /// <exception cref="ArgumentOutOfRangeException"></exception>
  43. public IMemoryOwner<byte> Rent(ulong length)
  44. => RentImpl(checked((int)length));
  45. /// <summary>
  46. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  47. /// The buffer may contain data from a prior use.
  48. /// </summary>
  49. /// <param name="length">The buffer's required length in bytes</param>
  50. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  51. /// <exception cref="ArgumentOutOfRangeException"></exception>
  52. public IMemoryOwner<byte> Rent(int length)
  53. => RentImpl(length);
  54. /// <summary>
  55. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  56. /// The buffer's contents are cleared (set to all 0s) before returning.
  57. /// </summary>
  58. /// <param name="length">The buffer's required length in bytes</param>
  59. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  60. /// <exception cref="ArgumentOutOfRangeException"></exception>
  61. public IMemoryOwner<byte> RentCleared(long length)
  62. => RentCleared(checked((int)length));
  63. /// <summary>
  64. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  65. /// The buffer's contents are cleared (set to all 0s) before returning.
  66. /// </summary>
  67. /// <param name="length">The buffer's required length in bytes</param>
  68. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  69. /// <exception cref="ArgumentOutOfRangeException"></exception>
  70. public IMemoryOwner<byte> RentCleared(ulong length)
  71. => RentCleared(checked((int)length));
  72. /// <summary>
  73. /// Rents a byte memory buffer from <see cref="ArrayPool{Byte}.Shared"/>.
  74. /// The buffer's contents are cleared (set to all 0s) before returning.
  75. /// </summary>
  76. /// <param name="length">The buffer's required length in bytes</param>
  77. /// <returns>A <see cref="IMemoryOwner{Byte}"/> wrapping the rented memory</returns>
  78. /// <exception cref="ArgumentOutOfRangeException"></exception>
  79. public IMemoryOwner<byte> RentCleared(int length)
  80. {
  81. var buffer = RentImpl(length);
  82. buffer.Memory.Span.Clear();
  83. return buffer;
  84. }
  85. private static ByteMemoryPoolBuffer RentImpl(int length)
  86. {
  87. if ((uint)length > Array.MaxLength)
  88. {
  89. throw new ArgumentOutOfRangeException(nameof(length), length, null);
  90. }
  91. return new ByteMemoryPoolBuffer(length);
  92. }
  93. }
  94. }