Guest User

Untitled

a guest
Jan 13th, 2018
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "xbyak/xbyak.h"
  3.  
  4. template <class T, class TA1>
  5. struct Thunk : public Xbyak::CodeGenerator {
  6. Thunk(T *t, void (T::*p)(TA1)) {
  7. mov(ecx, reinterpret_cast<intptr_t>(t));
  8. push(ptr[esp+4]);
  9. call(*reinterpret_cast<void **>(&p));
  10. ret();
  11. }
  12.  
  13. void (*getPtr())(TA1) {
  14. return reinterpret_cast<void (*)(TA1)>(
  15. const_cast<Xbyak::uint8 *>(getCode()));
  16. }
  17. };
  18.  
  19. struct Test {
  20. int n;
  21. Test(int n) : n(n) {}
  22. void show(int n2) {
  23. printf("%d, %d\n", n, n2);
  24. }
  25. };
  26.  
  27. void show(int n) {
  28. printf("%d\n", n);
  29. }
  30.  
  31. void call(void (*f)(int), int n) { f(n); }
  32.  
  33. int main() {
  34. Test t(1);
  35. Thunk<Test, int> thunk(&t, &Test::show);
  36. call(thunk.getPtr(), 2);
  37. call(show, 3);
  38. }
Add Comment
Please, Sign In to add comment