mmap 是否总是返回更高的地址

发布于 2024-12-21 15:25:33 字数 104 浏览 3 评论 0原文

mmap(当使用 MAP_ANONYMOUS 调用时,即用于分配内存时)是否总是返回比先前调用更高的内存地址?如果不是这样,有什么办法让它始终返回更高的地址吗?

Does mmap (when called with MAP_ANONYMOUS, that is, for allocating memory) always return higher memory address than a previous call? If not so, Is there any way to make it return a higher address always?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

金兰素衣 2024-12-28 15:25:33

默认情况下,mmap 可以以任何顺序返回在页面边界上对齐的任何地址。

如果您想强制返回的地址是您指定的地址,则可以使用 MAP_FIXED 标志,但这不太便携且可靠。通过这种方式,您可以将代码与特定内核上的 mmap 的特定实现联系起来。

但无论如何,为什么你总是需要比前一个地址更高的地址呢?可能更好的方法是改变程序的逻辑。

By default, mmap can return any address aligned on a page boundary, in any order.

If you want to enforce that the returned address is the one you specify, you can use the MAP_FIXED flag, but that isn't very portable and reliable. This way you are tying your code with the particular implementation of mmap on a particular kernel.

But anyway, why would you always need a higher address than the previous one? Possibly a better way is to change the logic of your program.

回忆那么伤 2024-12-28 15:25:33

不一定,至少根据它的定义不是。

我相信,使用 ASLR 可能会发生高地址不再可用的情况,因此 mmap 必须选择一些较低的地址范围。

显然,在 32 位处理器(和内核)上,内存空间可能几乎被填满,因此当要求一个大的 mmap 范围时,内核应该找到一个合适的,并且可以在任何地方。

如果你想要一个单调的方向,请使用 sbrk (但我真的建议不要使用它)。

另一种可能性是在程序初始化时使用 mmapMAP_NORESERVE 预先分配大量(例如几 TB)的地址空间,并调用 mmap code> 与 MAP_FIXED 再次在该范围内以获得真正可用的空间(在更易于管理的块中,例如几十兆字节)。

@MetallicPriest:你真的应该激励并解释更多你的问题。这些问题是如此神秘和奇怪(甚至我无法猜出所有上下文),以至于回答它们并不是很有趣。

Not necessarily, at least not according to its definition.

And I would believe that with ASLR it could happen that upper addresses are no more available, so mmap has to choose some lower address range.

Obviously, on 32 bits processors (& kernels) the memory space could be nearly filled, so when asking for a big mmap-ed range the kernel should find one which fits, and that could be anywhere.

If you want a monotone direction, use sbrk (but I really recommend against using it).

Another possibility could be to pre-allocate a very large amount (e.g. several terabytes) of address space using mmap with MAP_NORESERVE at program initialization, and call mmap with MAP_FIXED inside that range again to get the really usable space (in more manageable chunks, e.g. dozens of megabytes).

@MetallicPriest: you really should motivate and explain much more your questions. There are so mysterious and weird (and even I cannot guess all the context) that it is not very fun to answer them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文