aboutsummaryrefslogtreecommitdiff
path: root/Ryujinx.Cpu/MemoryEhMeilleure.cs
blob: a82295819f32f17a74a254da6cca007e439848a9 (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
35
36
37
38
39
40
41
using ARMeilleure.Signal;
using Ryujinx.Memory;
using Ryujinx.Memory.Tracking;
using System;
using System.Runtime.InteropServices;

namespace Ryujinx.Cpu
{
    class MemoryEhMeilleure : IDisposable
    {
        private delegate bool TrackingEventDelegate(ulong address, ulong size, bool write, bool precise = false);

        private readonly MemoryBlock _addressSpace;
        private readonly MemoryTracking _tracking;
        private readonly TrackingEventDelegate _trackingEvent;

        private readonly ulong _baseAddress;

        public MemoryEhMeilleure(MemoryBlock addressSpace, MemoryTracking tracking)
        {
            _addressSpace = addressSpace;
            _tracking = tracking;

            _baseAddress = (ulong)_addressSpace.Pointer;
            ulong endAddress = _baseAddress + addressSpace.Size;

            _trackingEvent = new TrackingEventDelegate(tracking.VirtualMemoryEventEh);
            bool added = NativeSignalHandler.AddTrackedRegion((nuint)_baseAddress, (nuint)endAddress, Marshal.GetFunctionPointerForDelegate(_trackingEvent));

            if (!added)
            {
                throw new InvalidOperationException("Number of allowed tracked regions exceeded.");
            }
        }

        public void Dispose()
        {
            NativeSignalHandler.RemoveTrackedRegion((nuint)_baseAddress);
        }
    }
}