tags:

views:

112

answers:

2

Hello.

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.

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?

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.

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.

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

Is this possible in userspace?

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

Thank you.

+9  A: 

Short answer: it already works that way.

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.

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.

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

Amigable Clark Kant
That's good to know. Can you please tell me how to find documentation on this?
spraff
@spraff: some ---> http://www.ioremap.net/node/125
Amigable Clark Kant
A: 

I think, a simple way would be to do that with a large static array.

On any modern system this will not be mapped to existing memory (in the executable file on disk or in RAM of your execution machine) unless you will really access it. Once you will access it (and the system has enough resources) it will be miraculously initialized to all zeros.

And your program will seriously slow down once you reach the limit of physical memory and then randomly crash if you run out of swap.

Jens Gustedt