mremap tries to increase the selection in place, but returns to distributing the new region if it cannot increase the size of the current region.
mremap () extends (or shrinks) the existing memory mapping, potentially moving it at the same time (controlled by the flags argument and the available virtual address space). src
If mremap fails, the old memory is just fine (like realloc).
If mremap fails, there is nothing munmap (from this call, at least). See paragraph 1.
If mremap succeeds and needs to move, the old memory is copied to the new one (and the old munmap is for you). If mremap is able to increase the size in place, the memory does not move and a new location is not created.
source share