c++clinuxmmapmprotect

Does mmap allocate a page or part of a page?


I'm confused, does mmap allocate an entire page of memory (regardless of size specified), or does it just allocate the size you request? Really, I'm curious about what happens on subsequent calls to mmap -- would a second call allocate a new page (even if both calls use an amount under the page size) or would it allocate a block adjacent to the previous call?

Same thing for mprotect - does that protect the entire page, or just the part specified?


Solution

  • If the length argument is not a page size multiple it will be rounded up to page size multiple.

    As a consequence, the answer to your question is yes mmap() virtually allocates only entire pages.

    Regarding mprotect() the man page clearly answer to your question:

    mprotect() changes protection for the calling process's memory page(s) containing any part of the address range in the interval [addr, addr+len-1]. addr must be aligned to a page boundary.