Skip to content

Instantly share code, notes, and snippets.

@feliwir
Created March 4, 2016 10:20
Show Gist options
  • Save feliwir/edf03083871ff97beddb to your computer and use it in GitHub Desktop.
Save feliwir/edf03083871ff97beddb to your computer and use it in GitHub Desktop.
void* asjit::jitcode(dasm_State **state)
{
size_t sz;
void* buf;
dasm_link(state, &sz);
#ifdef _WIN32
buf = VirtualAlloc(0, sz, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
#else
buf = mmap(0, sz, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
#endif
dasm_encode(state, buf);
#ifdef _WIN32
{DWORD dwOld; VirtualProtect(buf, sz, PAGE_EXECUTE_READ, &dwOld); }
#else
mprotect(buf, sz, PROT_READ | PROT_EXEC);
#endif
return buf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment