在 Linux 中,mremap 函数用于重新映射使用 mmap 映射的内存。请帮助我澄清以下内容:
- 如果 mremap 函数失败,旧映射内存的状态是什么?
- 如果mremap函数失败,是否需要调用munmap函数?
- 如果 mremap 函数成功,重映射内存中是否有以前的数据?
最佳答案
mremap 尝试就地增加分配,但如果它不能增加当前区域的大小,则回退到分配新区域。
mremap() expands (or shrinks) an existing memory mapping, potentially moving it at the same time (controlled by the flags argument and the available virtual address space). src
如果 mremap 失败,旧内存就好了(就像 realloc)。
如果 mremap 失败,则没有任何东西可以 munmap(至少从 this 调用)。请参阅第 1 项。
如果 mremap 成功并且必须移动,旧内存将被复制到新内存中(旧内存会为您进行 munmap)。如果 mremap 能够就地增加大小,则不会移动内存,也不会创建新分配。
关于c++ - Linux中mremap函数的特点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16080192/