2023-01-29 12:37:52 +01:00
|
|
|
using Ryujinx.Memory;
|
2023-09-26 01:18:32 +02:00
|
|
|
using System.Runtime.Versioning;
|
2023-01-29 12:37:52 +01:00
|
|
|
|
|
|
|
namespace Ryujinx.Cpu.AppleHv
|
|
|
|
{
|
2023-09-26 01:18:32 +02:00
|
|
|
[SupportedOSPlatform("macos")]
|
2023-01-29 12:37:52 +01:00
|
|
|
class HvMemoryBlockAllocator : PrivateMemoryAllocatorImpl<HvMemoryBlockAllocator.Block>
|
|
|
|
{
|
|
|
|
public class Block : PrivateMemoryAllocator.Block
|
|
|
|
{
|
|
|
|
private readonly HvIpaAllocator _ipaAllocator;
|
|
|
|
public ulong Ipa { get; }
|
|
|
|
|
|
|
|
public Block(HvIpaAllocator ipaAllocator, MemoryBlock memory, ulong size) : base(memory, size)
|
|
|
|
{
|
|
|
|
_ipaAllocator = ipaAllocator;
|
|
|
|
|
|
|
|
lock (ipaAllocator)
|
|
|
|
{
|
|
|
|
Ipa = ipaAllocator.Allocate(size);
|
|
|
|
}
|
|
|
|
|
2023-07-01 04:18:52 +02:00
|
|
|
HvApi.hv_vm_map((ulong)Memory.Pointer, Ipa, size, HvMemoryFlags.Read | HvMemoryFlags.Write).ThrowOnError();
|
2023-01-29 12:37:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public override void Destroy()
|
|
|
|
{
|
|
|
|
HvApi.hv_vm_unmap(Ipa, Size).ThrowOnError();
|
|
|
|
|
|
|
|
lock (_ipaAllocator)
|
|
|
|
{
|
|
|
|
_ipaAllocator.Free(Ipa, Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Destroy();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly HvIpaAllocator _ipaAllocator;
|
|
|
|
|
|
|
|
public HvMemoryBlockAllocator(HvIpaAllocator ipaAllocator, int blockAlignment) : base(blockAlignment, MemoryAllocationFlags.None)
|
|
|
|
{
|
|
|
|
_ipaAllocator = ipaAllocator;
|
|
|
|
}
|
|
|
|
|
2023-07-01 04:18:52 +02:00
|
|
|
public HvMemoryBlockAllocation Allocate(ulong size, ulong alignment)
|
2023-01-29 12:37:52 +01:00
|
|
|
{
|
|
|
|
var allocation = Allocate(size, alignment, CreateBlock);
|
|
|
|
|
|
|
|
return new HvMemoryBlockAllocation(this, allocation.Block, allocation.Offset, allocation.Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
private Block CreateBlock(MemoryBlock memory, ulong size)
|
|
|
|
{
|
|
|
|
return new Block(_ipaAllocator, memory, size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|