Alexander Smirnov
Alexander Smirnov

Reputation: 575

How to 'malloc' within first 4GB on x86_64

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.

Upvotes: 6

Views: 782

Answers (1)

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.

Upvotes: 7

Related Questions