5

Platform: Mac OS X
Lang: Obj-C/C

Is it possible to somehow make 'malloc' to allocate memory within first 4GB of process address space ?

I'm emulating i386 stack and need to guarantee that address will lie within allowed 32bit range.

Using mmap+MAP_FIXED requires to RESERVE memory before any 'malloc', it's not quite convenient. 'malloc' with constraints would be much more handy.

  • 1
    On Linux there is a flag `MAP_32BIT` that can be passed to mmap to request an address in the first 2 GB. Maybe Mac OS X has a similar feature? – ar31 Feb 22 '15 at 18:30

1 Answers1

7

It is not possible, unless you code your own implementation of malloc (or dive into the implementation details of some existing malloc then change it to suit your needs).

Most malloc-s implementations are using the system mmap (or sbrk) syscalls (see e.g. syscalls(2) on Linux, and memory(3) for MacOSX), and these are giving some arbitrary memory addresses (e.g. because of ASLR, which is very useful).

PS. On Linux, you might use mmap(2) with MAP_NORESERVE or MAP_32BIT, but MacOSX mmap(2) does not seem to have them.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547