Reputation: 48662
I'm writing a program that's doing JIT, and I'm currently setting up the executable memory like this:
void *mem = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
// write the JIT output to mem
mprotect(mem, len, PROT_READ|PROT_EXEC);
I then clean it up like this:
munmap(mem, len);
This is rather inefficient, though, as I'm allocating an entire page (4KB) for everything I JIT, most of which only need a few bytes. (Note that my concern is more with the amount of page mappings, and possibly falling afoul of vm.max_map_count
, than just the raw amount of RAM used.) For regular memory allocation, malloc
takes care of this by wrapping (s)brk
and mmap
and keeping track of what parts of the page are used. I obviously can't just use malloc
because it allocates non-executable memory. Is there a simple way to get similar behavior when allocating executable memory, or would I have to use a custom replacement for malloc
to do this?
Upvotes: 1
Views: 241