using System;
using System.Runtime.InteropServices;
using CpuAddress = System.UInt64;
using DspAddress = System.UInt64;
namespace Ryujinx.Audio.Renderer.Server.MemoryPool
{
///
/// Represents the information of a region shared between the CPU and DSP.
///
public struct AddressInfo
{
///
/// The target CPU address of the region.
///
public CpuAddress CpuAddress;
///
/// The size of the region.
///
public ulong Size;
private unsafe MemoryPoolState* _memoryPools;
///
/// The forced DSP address of the region.
///
public DspAddress ForceMappedDspAddress;
private readonly unsafe ref MemoryPoolState MemoryPoolState => ref *_memoryPools;
public readonly unsafe bool HasMemoryPoolState => (IntPtr)_memoryPools != IntPtr.Zero;
///
/// Create an new empty .
///
/// A new empty .
public static AddressInfo Create()
{
return Create(0, 0);
}
///
/// Create a new .
///
/// The target of the region.
/// The target size of the region.
/// A new .
public static AddressInfo Create(CpuAddress cpuAddress, ulong size)
{
unsafe
{
return new AddressInfo
{
CpuAddress = cpuAddress,
_memoryPools = MemoryPoolState.Null,
Size = size,
ForceMappedDspAddress = 0,
};
}
}
///
/// Setup the CPU address and size of the .
///
/// The target of the region.
/// The size of the region.
public void Setup(CpuAddress cpuAddress, ulong size)
{
CpuAddress = cpuAddress;
Size = size;
ForceMappedDspAddress = 0;
unsafe
{
_memoryPools = MemoryPoolState.Null;
}
}
///
/// Set the associated.
///
/// The associated.
public void SetupMemoryPool(Span memoryPoolState)
{
unsafe
{
fixed (MemoryPoolState* ptr = &MemoryMarshal.GetReference(memoryPoolState))
{
SetupMemoryPool(ptr);
}
}
}
///
/// Set the associated.
///
/// The associated.
public unsafe void SetupMemoryPool(MemoryPoolState* memoryPoolState)
{
_memoryPools = memoryPoolState;
}
///
/// Check if the is mapped.
///
/// Returns true if the is mapped.
public readonly bool HasMappedMemoryPool()
{
return HasMemoryPoolState && MemoryPoolState.IsMapped();
}
///
/// Get the DSP address associated to the .
///
/// If true, mark the as used.
/// Returns the DSP address associated to the .
public readonly DspAddress GetReference(bool markUsed)
{
if (!HasMappedMemoryPool())
{
return ForceMappedDspAddress;
}
if (markUsed)
{
MemoryPoolState.IsUsed = true;
}
return MemoryPoolState.Translate(CpuAddress, Size);
}
}
}