TranslatedFunction.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. namespace ARMeilleure.Translation
  5. {
  6. class TranslatedFunction
  7. {
  8. private const int MinCallsForRejit = 100;
  9. private readonly GuestFunction _func; // Ensure that this delegate will not be garbage collected.
  10. private int _callCount;
  11. public ulong GuestSize { get; }
  12. public bool HighCq { get; }
  13. public IntPtr FuncPtr { get; }
  14. public TranslatedFunction(GuestFunction func, ulong guestSize, bool highCq)
  15. {
  16. _func = func;
  17. GuestSize = guestSize;
  18. HighCq = highCq;
  19. FuncPtr = Marshal.GetFunctionPointerForDelegate(func);
  20. }
  21. public ulong Execute(State.ExecutionContext context)
  22. {
  23. return _func(context.NativeContextPtr);
  24. }
  25. public bool ShouldRejit()
  26. {
  27. return !HighCq && Interlocked.Increment(ref _callCount) == MinCallsForRejit;
  28. }
  29. public void ResetCallCount()
  30. {
  31. Interlocked.Exchange(ref _callCount, 0);
  32. }
  33. }
  34. }