gpt4 book ai didi

c++ - mmap 是否返回对齐的指针值

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:33:36 27 4
gpt4 key购买 nike

mmap() 是否保证返回值与系统上的最大对齐对齐?即,是否由 POSIX 标准保证 mmap 必须返回 alignof(std::max_align_t) 的倍数的指针值?

我无法在 Ubuntu linux mmap(2) 手册页或 mac osx mmap(2) 手册页上找到此信息

最佳答案

是的。

http://man7.org/linux/man-pages/man2/mmap.2.html

在大多数“流行”的 NULL 映射的情况下

If addr is NULL, then the kernel chooses the address at which to create the mapping; this is the most portable method of creating a new mapping. If addr is not NULL, then the kernel takes it as a hint about where to place the mapping; on Linux, the mapping will be created at a nearby page boundary. The address of the new mapping is returned as the result of the call.

即使你指定了 MAP_FIXED 而不是

Don't interpret addr as a hint: place the mapping at exactly that address. addr must be a multiple of the page size. If the memory region specified by addr and len overlaps pages of any existing mapping(s), then the overlapped part of the existing mapping(s) will be discarded. If the specified address cannot be used, mmap() will fail. Because requiring a fixed address for a mapping is less portable, the use of this option is discouraged.

事实上最小的页面是 4096B(对于 x86,但对于其他平台它无论如何是 1024B 的倍数)并且因为 std::max_align_t 在 64 位系统上最有可能是 16B,它将被对齐。

关于c++ - mmap 是否返回对齐的指针值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42259495/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com