3

I've read that sbrk is a deprecated call and one should prefer mmap with MAP_ANONYMOUS flag. I need one continous (logical) memory block that can grow. However, mmap treats first parameter as a hint, so it can make gaps, which is unacceptable in my case. I tried to use MAP_FIXED flag (which as documentation states is not recommended) and I can get continuos memory, but after mapping several pages I get strange behaviour of my program: system functions like printf and clock_gettime begin to fail. I guess the first mmap which I call without MAP_FIXED returns page that has some mapped pages after it, which contain system data. So what is the right way to use mmap instead of sbrk?

Alexey Andreev
  • 1,980
  • 2
  • 17
  • 29

2 Answers2

4

With Linux you can use mmap with MAP_NORESERVE (and possibly PROT_NONE) to claim a large chunk of address space without actually allocating any memory. You map the largest area you could possibly want (and can get), and then remap bits of it with MAP_FIXED to actually allocate memory as needed.

Chris Dodd
  • 119,907
  • 13
  • 134
  • 226
  • Is this what `malloc` actually does? And what initial amount of memory it uses for MAP_NORESERVE? – Alexey Andreev Jun 30 '16 at 06:09
  • This answer is the same as my answer: if OP can determine the maximum size of the block, then he can `mmap` the entire maximum size. And `MAP_NORESERVE` isn't needed either: if OP doesn't touch the memory, it will not be paged in. – Employed Russian Jun 30 '16 at 06:28
  • @EmployedRussian: without MAP_NORESERVE, it will allocate swap space (which may fail), and will definitely allocate PTEs for all pages to refer to that swap space. With MAP_NORESERVE and PROT_NONE it will just reserve the virtual address range, and won't even allocate PTEs. So on a 64-bit machine you can reserve a multi-terabyte region quickly, so no other library or whatever will try to use it. – Chris Dodd Jun 30 '16 at 17:23
-1

I've read that sbrk is a deprecated call

Don't believe everything you read, especially if the source is not authoritative.

I need one continous (logical) memory block that can grow.

In that case, mmap is not for you, unless you are willing to declare the maximum size to which that block can grow.

I tried to use MAP_FIXED flag (which as documentation states is not recommended) and I can get continuos memory, but after mapping several pages I get strange behaviour of my program

With MMAP_FIXED you have to be very careful: the system will happily map over whatever (if anything) was there before, including libc data and code.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362