TranslatedFunction.cs 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 GuestFunction _func;
  10. private IntPtr _funcPtr;
  11. private bool _rejit;
  12. private int _callCount;
  13. public bool HighCq => !_rejit;
  14. public TranslatedFunction(GuestFunction func, bool rejit)
  15. {
  16. _func = func;
  17. _rejit = rejit;
  18. }
  19. public ulong Execute(State.ExecutionContext context)
  20. {
  21. return _func(context.NativeContextPtr);
  22. }
  23. public bool ShouldRejit()
  24. {
  25. return _rejit && Interlocked.Increment(ref _callCount) == MinCallsForRejit;
  26. }
  27. public IntPtr GetPointer()
  28. {
  29. if (_funcPtr == IntPtr.Zero)
  30. {
  31. _funcPtr = Marshal.GetFunctionPointerForDelegate(_func);
  32. }
  33. return _funcPtr;
  34. }
  35. }
  36. }