问题描述
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?
推荐答案
不一定,至少不是根据其定义.
Not necessarily, at least not according to its definition.
而且我相信使用 ASLR 可能会发生上层地址不再可用的情况,所以 mmap
必须选择一些较低的地址范围.
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.
显然,在 32 位处理器(& 内核)上,内存空间可能几乎被填满,所以当请求一个大的 mmap
-ed 范围时,内核应该找到一个适合的,并且可以随时随地.
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.
如果你想要一个单调的方向,使用 sbrk(但我真的不建议使用它).
If you want a monotone direction, use sbrk (but I really recommend against using it).
另一种可能是在程序初始化时使用mmap
和MAP_NORESERVE
预先分配大量(例如几TB)的地址空间,并调用mmap
和 MAP_FIXED
再次位于该范围内以获得真正可用的空间(以更易于管理的块,例如几十兆字节).
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:你真的应该激励和解释更多你的问题.有如此神秘和怪异(甚至我无法猜测所有上下文),回答它们并不是很有趣.
@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.
这篇关于mmap 是否总是返回更高的地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!