MemoryPoolState.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. //
  2. // Copyright (c) 2019-2020 Ryujinx
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. //
  17. using System;
  18. using System.Runtime.InteropServices;
  19. using DspAddress = System.UInt64;
  20. using CpuAddress = System.UInt64;
  21. namespace Ryujinx.Audio.Renderer.Server.MemoryPool
  22. {
  23. /// <summary>
  24. /// Server state for a memory pool.
  25. /// </summary>
  26. [StructLayout(LayoutKind.Sequential, Size = 0x20, Pack = Alignment)]
  27. public struct MemoryPoolState
  28. {
  29. public const int Alignment = 0x10;
  30. /// <summary>
  31. /// The location of the <see cref="MemoryPoolState"/>.
  32. /// </summary>
  33. public enum LocationType : uint
  34. {
  35. /// <summary>
  36. /// <see cref="MemoryPoolState"/> located on the CPU side for user use.
  37. /// </summary>
  38. Cpu,
  39. /// <summary>
  40. /// <see cref="MemoryPoolState"/> located on the DSP side for system use.
  41. /// </summary>
  42. Dsp
  43. }
  44. /// <summary>
  45. /// The CPU address associated to the <see cref="MemoryPoolState"/>.
  46. /// </summary>
  47. public CpuAddress CpuAddress;
  48. /// <summary>
  49. /// The DSP address associated to the <see cref="MemoryPoolState"/>.
  50. /// </summary>
  51. public DspAddress DspAddress;
  52. /// <summary>
  53. /// The size associated to the <see cref="MemoryPoolState"/>.
  54. /// </summary>
  55. public ulong Size;
  56. /// <summary>
  57. /// The <see cref="LocationType"/> associated to the <see cref="MemoryPoolState"/>.
  58. /// </summary>
  59. public LocationType Location;
  60. /// <summary>
  61. /// Set to true if the <see cref="MemoryPoolState"/> is used.
  62. /// </summary>
  63. [MarshalAs(UnmanagedType.I1)]
  64. public bool IsUsed;
  65. public static unsafe MemoryPoolState* Null => (MemoryPoolState*)IntPtr.Zero.ToPointer();
  66. /// <summary>
  67. /// Create a new <see cref="MemoryPoolState"/> with the given <see cref="LocationType"/>.
  68. /// </summary>
  69. /// <param name="location">The location type to use.</param>
  70. /// <returns>A new <see cref="MemoryPoolState"/> with the given <see cref="LocationType"/>.</returns>
  71. public static MemoryPoolState Create(LocationType location)
  72. {
  73. return new MemoryPoolState
  74. {
  75. CpuAddress = 0,
  76. DspAddress = 0,
  77. Size = 0,
  78. Location = location
  79. };
  80. }
  81. /// <summary>
  82. /// Set the <see cref="CpuAddress"/> and size of the <see cref="MemoryPoolState"/>.
  83. /// </summary>
  84. /// <param name="cpuAddress">The <see cref="CpuAddress"/>.</param>
  85. /// <param name="size">The size.</param>
  86. public void SetCpuAddress(CpuAddress cpuAddress, ulong size)
  87. {
  88. CpuAddress = cpuAddress;
  89. Size = size;
  90. }
  91. /// <summary>
  92. /// Check if the given <see cref="CpuAddress"/> and size is contains in the <see cref="MemoryPoolState"/>.
  93. /// </summary>
  94. /// <param name="targetCpuAddress">The <see cref="CpuAddress"/>.</param>
  95. /// <param name="size">The size.</param>
  96. /// <returns>True if the <see cref="CpuAddress"/> is contained inside the <see cref="MemoryPoolState"/>.</returns>
  97. public bool Contains(CpuAddress targetCpuAddress, ulong size)
  98. {
  99. if (CpuAddress <= targetCpuAddress && size + targetCpuAddress <= Size + CpuAddress)
  100. {
  101. return true;
  102. }
  103. return false;
  104. }
  105. /// <summary>
  106. /// Translate the given CPU address to a DSP address.
  107. /// </summary>
  108. /// <param name="targetCpuAddress">The <see cref="CpuAddress"/>.</param>
  109. /// <param name="size">The size.</param>
  110. /// <returns>the target DSP address.</returns>
  111. public DspAddress Translate(CpuAddress targetCpuAddress, ulong size)
  112. {
  113. if (Contains(targetCpuAddress, size) && IsMapped())
  114. {
  115. ulong offset = targetCpuAddress - CpuAddress;
  116. return DspAddress + offset;
  117. }
  118. return 0;
  119. }
  120. /// <summary>
  121. /// Is the <see cref="MemoryPoolState"/> mapped on the DSP?
  122. /// </summary>
  123. /// <returns>Returns true if the <see cref="MemoryPoolState"/> is mapped on the DSP.</returns>
  124. public bool IsMapped()
  125. {
  126. return DspAddress != 0;
  127. }
  128. }
  129. }