TranslatedFunction.cs 940 B

123456789101112131415161718192021222324252627282930313233343536
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using System.Threading;
  4. namespace ARMeilleure.Translation
  5. {
  6. sealed 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 = 0;
  11. public bool HighCq { get; }
  12. public IntPtr FuncPtr { get; }
  13. public TranslatedFunction(GuestFunction func, bool highCq)
  14. {
  15. _func = func;
  16. HighCq = highCq;
  17. FuncPtr = Marshal.GetFunctionPointerForDelegate<GuestFunction>(func);
  18. }
  19. public ulong Execute(State.ExecutionContext context)
  20. {
  21. return _func(context.NativeContextPtr);
  22. }
  23. public bool ShouldRejit()
  24. {
  25. return !HighCq && Interlocked.Increment(ref _callCount) == MinCallsForRejit;
  26. }
  27. }
  28. }