Features of the mremap function in Linux
In Linux, the mremap function is used to remap memory mapped using mmap. Please help me clarify the following:
- If the mremap function fails, what is the state of the old mapped memory?
- If the mremap function fails, do I need to call the munmap function?
- If the mremap function succeeds, is there previous data in remapped memory?
MreMap attempts to increase the allocation in place, but falls back to allocating a new region if it cannot increase the size of the current region.
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
If the mremap fails, the old memory is fine (like realloc).
If mremap fails, there is nothing to munmap (at least from this call). See item 1.
If the mremap succeeds and must be moved, the old memory is copied to the new memory (the old memory does the munmap for you). If the mremap is able to increase the size in place, memory is not moved and no new allocations are created.