简体   繁体   English

Mac上的mmap手册页

[英]mmap man page on Mac

The mmap man page on my mac says the following 我的Mac上的mmap手册页显示以下内容

The mmap() system call causes the pages starting at addr and continuing for at most len bytes to be mapped from the object described by fd, starting at byte offset offset. mmap()系统调用使页面从addr开始,并从fd描述的对象开始映射最多len个字节 ,从字节偏移量offset开始。

Does this mean that the call can fail without an error and return a portion of memory mapped to an address range that is smaller than what was asked for? 这是否意味着调用可以失败而不会出错,并返回映射到小于要求范围的地址范围的一部分内存?

For example if I do the following 例如,如果我执行以下操作

void* memory = mmap(nullptr, range, PROT_READ | PROT_WRITE, MAP_ANONYMOUS, -1, 0);

Then can the call succeed without an error and return an address range that is not range bytes long? 那么,调用可以成功无误并返回不长于range字节的地址范围吗?

The "address range" will still be range bytes long (or possibly longer if you haven't aligned things properly), but may not be mapped. “地址范围”仍将是range字节长(如果未正确对齐,则可能更长),但可能未映射。 len + offset may extend past the end of the mapped object (for example, past the end of a file). len + offset可能会超出映射对象的末尾(例如,超出文件末尾)。 If that occurs, then the docs indicate "any extension beyond the end of the mapped object will be zero-filled." 如果发生这种情况,则文档指示“超出映射对象末尾的任何扩展名都将为零。”

The problem you are facing is a system service that does many different related things. 您面临的问题是系统服务,它执行许多不同的相关操作。

(assuming "offset=0") IF you were mapping a 65-byte file and you specified "range=50000" (assuming the page size is less than 50000) then the mapped region is going to be smaller than the range because the file size is less than the range. (假设“ offset = 0”),如果您要映射一个65字节的文件,并且指定了“ range = 50000”(假设页面大小小于50000),则映射的区域将小于该范围,因为该文件大小小于范围。

Thus, While not usually documented, you are likely to get a virtual memory allocation that is "range" rounded DOWN to the file size then rounded UP to a page size WHEN you are mapping to a file (which you are not doing in your example). 因此,尽管通常没有记录,但是您可能会获得虚拟内存分配,该虚拟内存分配是“范围”向下舍入为文件大小,然后向上舍入为页面大小(当您映射到文件时)(在示例中未这样做) )。

In your case, where you are not mapping to file, the allocation is likely to be rounded up to the nearest page. 在您的情况下,如果您不映射到文件,则分配可能会四舍五入到最接近的页面。

Again, I am assuming that you are using a paging system. 同样,我假设您正在使用寻呼系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM