Created
October 8, 2011 23:49
-
-
Save 7shi/1273073 to your computer and use it in GitHub Desktop.
クロージャをサンクで包む
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <functional> | |
#include <stdio.h> | |
#include "xbyak/xbyak.h" | |
template <class T> | |
struct Thunk : public Xbyak::CodeGenerator { | |
Thunk(T *f) { | |
mov(ecx, reinterpret_cast<intptr_t>(f)); | |
auto p = &T::operator(); | |
jmp(*reinterpret_cast<void **>(&p)); | |
} | |
void(*getPtr())() { | |
return reinterpret_cast<void(*)()>( | |
const_cast<Xbyak::uint8 *>(getCode())); | |
} | |
}; | |
void call(void(*f)()) { f(); } | |
int main() { | |
int a = 1; | |
auto f = [&] { printf("%d\n", a++); }; | |
f(); | |
f(); | |
Thunk<decltype(f)> thunk(&f); | |
auto f2 = thunk.getPtr(); | |
f2(); | |
call(f2); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment