Does mremap “initialize” memory on growth?

荒凉一梦 提交于 2019-12-22 07:30:09

问题


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)


回答1:


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.



来源:https://stackoverflow.com/questions/7978831/does-mremap-initialize-memory-on-growth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!