MemoryAllocator.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. using Ryujinx.Common.Memory;
  2. using System;
  3. using System.Runtime.CompilerServices;
  4. using System.Runtime.InteropServices;
  5. namespace Ryujinx.Graphics.Nvdec.Vp9.Common
  6. {
  7. internal class MemoryAllocator : IDisposable
  8. {
  9. private const int PoolEntries = 10;
  10. private struct PoolItem
  11. {
  12. public nint Pointer;
  13. public int Length;
  14. public bool InUse;
  15. }
  16. private readonly PoolItem[] _pool = new PoolItem[PoolEntries];
  17. public ArrayPtr<T> Allocate<T>(int length) where T : unmanaged
  18. {
  19. int lengthInBytes = Unsafe.SizeOf<T>() * length;
  20. nint ptr = nint.Zero;
  21. for (int i = 0; i < PoolEntries; i++)
  22. {
  23. ref PoolItem item = ref _pool[i];
  24. if (!item.InUse && item.Length == lengthInBytes)
  25. {
  26. item.InUse = true;
  27. ptr = item.Pointer;
  28. break;
  29. }
  30. }
  31. if (ptr == nint.Zero)
  32. {
  33. ptr = Marshal.AllocHGlobal(lengthInBytes);
  34. for (int i = 0; i < PoolEntries; i++)
  35. {
  36. ref PoolItem item = ref _pool[i];
  37. if (!item.InUse)
  38. {
  39. item.InUse = true;
  40. if (item.Pointer != nint.Zero)
  41. {
  42. Marshal.FreeHGlobal(item.Pointer);
  43. }
  44. item.Pointer = ptr;
  45. item.Length = lengthInBytes;
  46. break;
  47. }
  48. }
  49. }
  50. ArrayPtr<T> allocation = new ArrayPtr<T>(ptr, length);
  51. allocation.AsSpan().Fill(default);
  52. return allocation;
  53. }
  54. public unsafe void Free<T>(ArrayPtr<T> arr) where T : unmanaged
  55. {
  56. nint ptr = (nint)arr.ToPointer();
  57. for (int i = 0; i < PoolEntries; i++)
  58. {
  59. ref PoolItem item = ref _pool[i];
  60. if (item.Pointer == ptr)
  61. {
  62. item.InUse = false;
  63. break;
  64. }
  65. }
  66. }
  67. public void Dispose()
  68. {
  69. for (int i = 0; i < PoolEntries; i++)
  70. {
  71. ref PoolItem item = ref _pool[i];
  72. if (item.Pointer != nint.Zero)
  73. {
  74. Marshal.FreeHGlobal(item.Pointer);
  75. item.Pointer = nint.Zero;
  76. }
  77. }
  78. }
  79. }
  80. }