TranslatedFunction.cs 886 B

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