//
// Copyright (c) 2019-2021 Ryujinx
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see .
//
using System;
using System.Runtime.InteropServices;
using DspAddress = System.UInt64;
using CpuAddress = 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 unsafe ref MemoryPoolState MemoryPoolState => ref *_memoryPools;
public 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 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 DspAddress GetReference(bool markUsed)
{
if (!HasMappedMemoryPool())
{
return ForceMappedDspAddress;
}
if (markUsed)
{
MemoryPoolState.IsUsed = true;
}
return MemoryPoolState.Translate(CpuAddress, Size);
}
}
}