MemoryPoolState.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using CpuAddress = System.UInt64;
  4. using DspAddress = System.UInt64;
  5. namespace Ryujinx.Audio.Renderer.Server.MemoryPool
  6. {
  7. /// <summary>
  8. /// Server state for a memory pool.
  9. /// </summary>
  10. [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
  11. public struct MemoryPoolState
  12. {
  13. public const int Alignment = 0x10;
  14. /// <summary>
  15. /// The location of the <see cref="MemoryPoolState"/>.
  16. /// </summary>
  17. public enum LocationType : uint
  18. {
  19. /// <summary>
  20. /// <see cref="MemoryPoolState"/> located on the CPU side for user use.
  21. /// </summary>
  22. Cpu,
  23. /// <summary>
  24. /// <see cref="MemoryPoolState"/> located on the DSP side for system use.
  25. /// </summary>
  26. Dsp
  27. }
  28. /// <summary>
  29. /// The CPU address associated to the <see cref="MemoryPoolState"/>.
  30. /// </summary>
  31. public CpuAddress CpuAddress;
  32. /// <summary>
  33. /// The DSP address associated to the <see cref="MemoryPoolState"/>.
  34. /// </summary>
  35. public DspAddress DspAddress;
  36. /// <summary>
  37. /// The size associated to the <see cref="MemoryPoolState"/>.
  38. /// </summary>
  39. public ulong Size;
  40. /// <summary>
  41. /// The <see cref="LocationType"/> associated to the <see cref="MemoryPoolState"/>.
  42. /// </summary>
  43. public LocationType Location;
  44. /// <summary>
  45. /// Set to true if the <see cref="MemoryPoolState"/> is used.
  46. /// </summary>
  47. [MarshalAs(UnmanagedType.I1)]
  48. public bool IsUsed;
  49. public static unsafe MemoryPoolState* Null => (MemoryPoolState*)IntPtr.Zero.ToPointer();
  50. /// <summary>
  51. /// Create a new <see cref="MemoryPoolState"/> with the given <see cref="LocationType"/>.
  52. /// </summary>
  53. /// <param name="location">The location type to use.</param>
  54. /// <returns>A new <see cref="MemoryPoolState"/> with the given <see cref="LocationType"/>.</returns>
  55. public static MemoryPoolState Create(LocationType location)
  56. {
  57. return new MemoryPoolState
  58. {
  59. CpuAddress = 0,
  60. DspAddress = 0,
  61. Size = 0,
  62. Location = location
  63. };
  64. }
  65. /// <summary>
  66. /// Set the <see cref="CpuAddress"/> and size of the <see cref="MemoryPoolState"/>.
  67. /// </summary>
  68. /// <param name="cpuAddress">The <see cref="CpuAddress"/>.</param>
  69. /// <param name="size">The size.</param>
  70. public void SetCpuAddress(CpuAddress cpuAddress, ulong size)
  71. {
  72. CpuAddress = cpuAddress;
  73. Size = size;
  74. }
  75. /// <summary>
  76. /// Check if the given <see cref="CpuAddress"/> and size is contains in the <see cref="MemoryPoolState"/>.
  77. /// </summary>
  78. /// <param name="targetCpuAddress">The <see cref="CpuAddress"/>.</param>
  79. /// <param name="size">The size.</param>
  80. /// <returns>True if the <see cref="CpuAddress"/> is contained inside the <see cref="MemoryPoolState"/>.</returns>
  81. public bool Contains(CpuAddress targetCpuAddress, ulong size)
  82. {
  83. if (CpuAddress <= targetCpuAddress && size + targetCpuAddress <= Size + CpuAddress)
  84. {
  85. return true;
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// Translate the given CPU address to a DSP address.
  91. /// </summary>
  92. /// <param name="targetCpuAddress">The <see cref="CpuAddress"/>.</param>
  93. /// <param name="size">The size.</param>
  94. /// <returns>the target DSP address.</returns>
  95. public DspAddress Translate(CpuAddress targetCpuAddress, ulong size)
  96. {
  97. if (Contains(targetCpuAddress, size) && IsMapped())
  98. {
  99. ulong offset = targetCpuAddress - CpuAddress;
  100. return DspAddress + offset;
  101. }
  102. return 0;
  103. }
  104. /// <summary>
  105. /// Is the <see cref="MemoryPoolState"/> mapped on the DSP?
  106. /// </summary>
  107. /// <returns>Returns true if the <see cref="MemoryPoolState"/> is mapped on the DSP.</returns>
  108. public bool IsMapped()
  109. {
  110. return DspAddress != 0;
  111. }
  112. }
  113. }