I'm working on a JIT compiler which will generate machine code in memory. This JIT is targeted at 64-bit POSIX x86 systems primarily, and I'm concerned about jumps in the code always being encodeable as 32-bit relative offsets. What I'd like to do is to mmap a 2-4GB chunk of executable memory for machine code, and manage this memory area myself.
What I'm wondering about specifically is: is it safe for me to mmap 4GB of memory at once, on a 64-bit system, even if the system doesn't have 4GBs of memory? I'm assuming that most (or all) OSes won't really be allocating the pages I don't write to, and so if I always allocate in the lower addresses first, I'm going to be OK, so long as I don't actually use more memory than the system physically has.
I would also be curious to hear alternative suggestions as to how to manage machine code allocation so that the machine code always resides in the same 4GB space on a 64-bit machine.