jit这个概念我想大家在网上并不少见,特别是在看java,c#等资料的时候,jit这个词都被用烂了。
那什么是jit?这个地方反而在网上的资料中说的很含糊。“静态编译的程序在执行前全部被翻译为机器码,而直译执行的则是一句一句边运行边翻译。”,这是在wiki上唯一useful但又不useful的句子。
但有一篇博文写的很好,解释了jit的工作原理:http://eli.thegreenplace.net/2013/11/05/how-to-jit-an-introduction/
里面写了这么一段代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
// Allocates RWX memory of given size and returns a pointer to it. On failure,
// prints out the error and returns NULL.
void* alloc_executable_memory(size_t size) {
void* ptr = mmap(0, size,
PROT_READ | PROT_WRITE | PROT_EXEC,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == (void*)-1) {
perror("mmap");