AddressInfo.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. /// Represents the information of a region shared between the CPU and DSP.
  9. /// </summary>
  10. public struct AddressInfo
  11. {
  12. /// <summary>
  13. /// The target CPU address of the region.
  14. /// </summary>
  15. public CpuAddress CpuAddress;
  16. /// <summary>
  17. /// The size of the region.
  18. /// </summary>
  19. public ulong Size;
  20. private unsafe MemoryPoolState* _memoryPools;
  21. /// <summary>
  22. /// The forced DSP address of the region.
  23. /// </summary>
  24. public DspAddress ForceMappedDspAddress;
  25. private unsafe ref MemoryPoolState MemoryPoolState => ref *_memoryPools;
  26. public unsafe bool HasMemoryPoolState => (IntPtr)_memoryPools != IntPtr.Zero;
  27. /// <summary>
  28. /// Create an new empty <see cref="AddressInfo"/>.
  29. /// </summary>
  30. /// <returns>A new empty <see cref="AddressInfo"/>.</returns>
  31. public static AddressInfo Create()
  32. {
  33. return Create(0, 0);
  34. }
  35. /// <summary>
  36. /// Create a new <see cref="AddressInfo"/>.
  37. /// </summary>
  38. /// <param name="cpuAddress">The target <see cref="CpuAddress"/> of the region.</param>
  39. /// <param name="size">The target size of the region.</param>
  40. /// <returns>A new <see cref="AddressInfo"/>.</returns>
  41. public static AddressInfo Create(CpuAddress cpuAddress, ulong size)
  42. {
  43. unsafe
  44. {
  45. return new AddressInfo
  46. {
  47. CpuAddress = cpuAddress,
  48. _memoryPools = MemoryPoolState.Null,
  49. Size = size,
  50. ForceMappedDspAddress = 0
  51. };
  52. }
  53. }
  54. /// <summary>
  55. /// Setup the CPU address and size of the <see cref="AddressInfo"/>.
  56. /// </summary>
  57. /// <param name="cpuAddress">The target <see cref="CpuAddress"/> of the region.</param>
  58. /// <param name="size">The size of the region.</param>
  59. public void Setup(CpuAddress cpuAddress, ulong size)
  60. {
  61. CpuAddress = cpuAddress;
  62. Size = size;
  63. ForceMappedDspAddress = 0;
  64. unsafe
  65. {
  66. _memoryPools = MemoryPoolState.Null;
  67. }
  68. }
  69. /// <summary>
  70. /// Set the <see cref="MemoryPoolState"/> associated.
  71. /// </summary>
  72. /// <param name="memoryPoolState">The <see cref="MemoryPoolState"/> associated.</param>
  73. public void SetupMemoryPool(Span<MemoryPoolState> memoryPoolState)
  74. {
  75. unsafe
  76. {
  77. fixed (MemoryPoolState* ptr = &MemoryMarshal.GetReference(memoryPoolState))
  78. {
  79. SetupMemoryPool(ptr);
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Set the <see cref="MemoryPoolState"/> associated.
  85. /// </summary>
  86. /// <param name="memoryPoolState">The <see cref="MemoryPoolState"/> associated.</param>
  87. public unsafe void SetupMemoryPool(MemoryPoolState* memoryPoolState)
  88. {
  89. _memoryPools = memoryPoolState;
  90. }
  91. /// <summary>
  92. /// Check if the <see cref="MemoryPoolState"/> is mapped.
  93. /// </summary>
  94. /// <returns>Returns true if the <see cref="MemoryPoolState"/> is mapped.</returns>
  95. public bool HasMappedMemoryPool()
  96. {
  97. return HasMemoryPoolState && MemoryPoolState.IsMapped();
  98. }
  99. /// <summary>
  100. /// Get the DSP address associated to the <see cref="AddressInfo"/>.
  101. /// </summary>
  102. /// <param name="markUsed">If true, mark the <see cref="MemoryPoolState"/> as used.</param>
  103. /// <returns>Returns the DSP address associated to the <see cref="AddressInfo"/>.</returns>
  104. public DspAddress GetReference(bool markUsed)
  105. {
  106. if (!HasMappedMemoryPool())
  107. {
  108. return ForceMappedDspAddress;
  109. }
  110. if (markUsed)
  111. {
  112. MemoryPoolState.IsUsed = true;
  113. }
  114. return MemoryPoolState.Translate(CpuAddress, Size);
  115. }
  116. }
  117. }