我怎样才能保留内存地址不分配它们 [英] How can I reserve memory addresses without allocating them

查看:124
本文介绍了我怎样才能保留内存地址不分配它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想(在* nix中)分配一个大的,contigious地址空间,但不消耗资源的直线距离,也就是我要保留一个地址范围从一个在以后分配。

I would like (in *nix) to allocate a large, contigious address space, but without consuming resources straight away, i.e. I want to reserve an address range an allocate from it later.

假如我这样做富=的malloc(3 * 1024 * 1024 * 1024)来分配3G,而是一个1G的计算机交换文件的1G上。它会失败,对吧?

Suppose I do foo=malloc(3*1024*1024*1024) to allocate 3G, but on a 1G computer with 1G of swap file. It will fail, right?

我想要做的就是怎么说给我一个内存地址范围烟草工业富+ 3G的到了我会被分配的,所以我可以保证这个区域内的所有分配是连续的,但实际上不分配,立竿见影。

What I want to do is say "Give me a memory address range foo...foo+3G into which I will be allocating" so I can guarantee all allocations within this area are contiguous, but without actually allocating straight away.

在上面的例子中,我想跟着富= reserve_memory(3G)与酒吧=的malloc(123)调用应succeedd因为reserve_memory没有任何消耗的资源还称,它只是保证栏将不会范围内烟草工业富+ 3G。

In the example above, I want to follow the foo=reserve_memory(3G) call with a bar=malloc(123) call which should succeedd since reserve_memory hasn't consumed any resources yet, it just guarantees that bar will not be in the range foo...foo+3G.

后来我会做类似allocate_for_real(富,0234),以消耗Foo的范围的字节0..234。在这一点上,内核会分配一些虚拟页面并将其映射到foo ...美孚+ 123 + N

Later I would do something like allocate_for_real(foo,0,234) to consume bytes 0..234 of foo's range. At this point, the kernel would allocate some virtual pages and map them to foo...foo+123+N

在用户空间这可能吗?

(这样做的一点是,在烟草工业需要的对象是连续的,在创建后,他们不能合理地移动。)

(The point of this is that objects in foo... need to be contiguous and cannot reasonably be moved after they are created.)

感谢您。

推荐答案

简短的回答:它已经这样工作

Short answer: it already works that way.

稍长的答案:坏消息是,有一个保留的范围,但不分配它没有特殊的办法。但是,的消息是,当你分配一个范围,Linux不实际分配,它只是保留它由您使用更高版本。

Slightly longer answer: the bad news is that there is no special way of reserving a range, but not allocating it. However, the good news is that when you allocate a range, Linux does not actually allocate it, it just reserves it for use by you, later.

的Linux的默认行为是始终接受新的配置,只要有地址的范围的离开了。当你真正开始使用的内存虽然,有更好的一些记忆,或者至少掉备份。如果没有,内核会杀掉一个进程来释放内存,通常它分配的内存最多的进程。

The default behavior of Linux is to always accept a new allocation, as long as there is address range left. When you actually start using the memory though, there better be some memory or at least swap backing it up. If not, the kernel will kill a process to free memory, usually the process which allocated the most memory.

所以使用默认设置在Linux上的问题会从我多少可以分配转变,为能有多大我分配和的然后还活着的,当我开始使用的内存?

So the problem in Linux with default settings gets shifted from, "how much can I allocate", into "how much can I allocate and then still be alive when I start using the memory?"

这里的部分的信息的主体。

Here is some info on the subject.

这篇关于我怎样才能保留内存地址不分配它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆