Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does mremap "initialize" memory on growth?

Tags:

c

linux

mmap

If I've mmap() some PRIVATE and ANONYMOUS pages and then extend them with mremap(), does the new space also get initialized to zeroes?

I've tried reading the code for mremap (mm/mremap.c) in the linux source but it requires some domain-specific knowledge that I don't currently have (vma_### stuff). Not even sure that's the right place to look...

But, from what I've gathered I think that mremap()ed memory would be cleared, is this correct?

Allocation is done like this

list = mmap(NULL, newSize, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0)

and then remap is done like this

newList = mremap(list, oldSize, newSize, MREMAP_MAYMOVE)

Ah, and last, it's a Linux specific question, running a recent kernel (>=2.6.28) and libc (>= 2.11.1)

like image 654
Sven Almgren Avatar asked Oct 10 '22 14:10

Sven Almgren


1 Answers

Anonymous pages are copy-on-write mappings of the universal zero page. They always have been (on every system, not just Linux, that offers anonymous mappings) and always will be. When mremap (or brk) extends an anonymous mapping, you get new anonymous (zero) pages. There is no need to initialize them yourself.

like image 97
R.. GitHub STOP HELPING ICE Avatar answered Oct 13 '22 11:10

R.. GitHub STOP HELPING ICE