aboutsummaryrefslogtreecommitdiff
path: root/src/Ryujinx.Cpu/AppleHv/HvIpaAllocator.cs
blob: 35375ee60e7d5bd162a3b805201b6dd1084d6581 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
using System;

namespace Ryujinx.Cpu.AppleHv
{
    class HvIpaAllocator
    {
        private const ulong AllocationGranule = 1UL << 14;
        private const ulong IpaRegionSize = 1UL << 35;

        private readonly PrivateMemoryAllocator.Block _block;

        public HvIpaAllocator()
        {
            _block = new PrivateMemoryAllocator.Block(null, IpaRegionSize);
        }

        public ulong Allocate(ulong size, ulong alignment = AllocationGranule)
        {
            ulong offset = _block.Allocate(size, alignment);

            if (offset == PrivateMemoryAllocator.InvalidOffset)
            {
                throw new InvalidOperationException($"No enough free IPA memory to allocate 0x{size:X} bytes with alignment 0x{alignment:X}.");
            }

            return offset;
        }

        public void Free(ulong offset, ulong size)
        {
            _block.Free(offset, size);
        }
    }
}