I'm on a 64-bit system, but want to use mmap
to allocate pages within the first 2GB of memory. On Linux, I can do this with the MAP_32BIT
flag:
#include <sys/mman.h>
#include <stdio.h>
int main() {
void *addr = mmap(
NULL, // address hint
4096, // size
PROT_READ | PROT_WRITE, // permissions
MAP_32BIT | MAP_PRIVATE | MAP_ANONYMOUS, // flags
-1, // file descriptor
0 // offset
);
if (addr == MAP_FAILED)
perror("mmap");
else
printf("%p", addr);
}
Godbolt link demonstrating that this works on Linux. As of version 10.15, MacOS also allegedly supports the MAP_32BIT
flag. However, when I compile and run the program on my system (11.3), it fails with ENOMEM
. The mapping does work when MAP_32BIT
is removed.
I have a few potential explanations for why this doesn't work, but none of them are very compelling:
- The permissions are wrong somehow (although removing either
PROT_READ
orPROT_WRITE
didn't solve it). - I need to specify an address hint for this to work, for some reason.
- MacOS (or my version of it) simply doesn't support
MAP_32BIT
for anonymous mappings.