aboutsummaryrefslogtreecommitdiff
path: root/ARMeilleure/Translation/TranslatedFunction.cs
blob: af01aaab3123da9d04631b4d54fa5afec758cad6 (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
using System;
using System.Runtime.InteropServices;
using System.Threading;

namespace ARMeilleure.Translation
{
    class TranslatedFunction
    {
        private const int MinCallsForRejit = 100;

        private GuestFunction _func;

        private bool _rejit;
        private int  _callCount;

        public bool HighCq => !_rejit;

        public TranslatedFunction(GuestFunction func, bool rejit)
        {
            _func  = func;
            _rejit = rejit;
        }

        public ulong Execute(State.ExecutionContext context)
        {
            return _func(context.NativeContextPtr);
        }

        public bool ShouldRejit()
        {
            return _rejit && Interlocked.Increment(ref _callCount) == MinCallsForRejit;
        }

        public IntPtr GetPointer()
        {
            return Marshal.GetFunctionPointerForDelegate(_func);
        }
    }
}