Conversation

I'd strongly recommend measuring the size of the address space and reserving a huge portion as a massive PROT_NONE mapping rather than using hints though. mmap hints aren't respected everywhere and you can end up with other mappings getting in the way and screwing up the hints.
2
3
Basically, make a massive PROT_NONE mapping and then you allocate with mprotect to PROT_READ|PROT_WRITE and free by using MAP_FIXED mmap to replace a section with a new fresh PROT_NONE region. It prevents anything else from getting that via mmap outside of your own mmap usage.
2
9
I mean, if this is linux then you could just skip some steps by doing the massive mmap, make it all rw, and then only touch a page when you want the kernel to allocate a page for it. There will be a bit of read ahead but Linux’s allocation uses an “optimistic” strategy.
1
It won't work well in the default heuristic overcommit mode which gives you the worst of both worlds. It only works properly in the full overcommit mode. Linux has support for a memory accounting mode too, and it doesn't work for that, since rw anon memory is accountable.
2
1
Making a many terabyte rw mapping will work on Android, but it won't work as expected on typical desktop/server Linux distributions because they use the default nonsense heuristic overcommit mode. Also don't think breaking compatibility with memory accounting mode is a good idea.
1
1