Created
September 19, 2011 08:21
-
-
Save 7shi/1226147 to your computer and use it in GitHub Desktop.
make closure by thunk
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" | |
void call(void (*f)()) { f(); } | |
int main() { | |
int a = 1; | |
struct _ : public Xbyak::CodeGenerator { | |
_(int *a) { | |
push(reinterpret_cast<intptr_t>(a)); | |
call(reinterpret_cast<void *>(run)); | |
add(esp, 4); | |
ret(); | |
} | |
static void run(int &a) { | |
printf("%d\n", a++); | |
} | |
} closure(&a); | |
auto f = reinterpret_cast<void (*)()>( | |
const_cast<Xbyak::uint8 *>(closure.getCode())); | |
f(); | |
f(); | |
call(f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment