3

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.

user207421
  • 305,947
  • 44
  • 307
  • 483
Maxime C.
  • 381
  • 2
  • 6
  • 1
    Every modern operating system I know of will not allocate all of this at once and will instead lazily allocate memory as needed. That said, I don't have any references to formally back this up... – templatetypedef Oct 29 '13 at 00:44
  • You could try to `mmap` the whole 4GB region and if it fails, `mmap` does let you choose where you'd prefer to create the mapping. Of course, if both fail, then you'd probably have to error out. Although, I think most modern OSs will let you map the whole 4GB and lazily allocate the memory. – tangrs Oct 29 '13 at 00:49
  • You might want to pass `MAP_NORESERVE` to `mmap()` to remain compatible with systems with little swap space. http://stackoverflow.com/questions/8506366/does-mmap-with-map-noreserve-reserve-physical-memory – Pascal Cuoq Oct 29 '13 at 21:18

1 Answers1

1

Your mmap of 4GB may succeed in allocating the virtual memory, and physical pages will be allocated as they are "dirtied", or modified by your program. If you run out of physical memory, your process may be terminated. See, also, this question.

Community
  • 1
  • 1
troydj
  • 304
  • 2
  • 5