thunk.h 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // Copyright 2013 Dolphin Emulator Project / 2014 Citra Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <map>
  6. #include "common/common_types.h"
  7. // This simple class creates a wrapper around a C/C++ function that saves all fp state
  8. // before entering it, and restores it upon exit. This is required to be able to selectively
  9. // call functions from generated code, without inflicting the performance hit and increase
  10. // of complexity that it means to protect the generated code from this problem.
  11. // This process is called thunking.
  12. // There will only ever be one level of thunking on the stack, plus,
  13. // we don't want to pollute the stack, so we store away regs somewhere global.
  14. // NOT THREAD SAFE. This may only be used from the CPU thread.
  15. // Any other thread using this stuff will be FATAL.
  16. class ThunkManager : public Gen::XCodeBlock
  17. {
  18. std::map<void *, const u8 *> thunks;
  19. const u8 *save_regs;
  20. const u8 *load_regs;
  21. public:
  22. ThunkManager() {
  23. Init();
  24. }
  25. ~ThunkManager() {
  26. Shutdown();
  27. }
  28. void *ProtectFunction(void *function, int num_params);
  29. private:
  30. void Init();
  31. void Shutdown();
  32. void Reset();
  33. };