Skip to content

Instantly share code, notes, and snippets.

@7shi
Created September 19, 2011 08:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 7shi/1226147 to your computer and use it in GitHub Desktop.
Save 7shi/1226147 to your computer and use it in GitHub Desktop.
make closure by thunk
#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