3

In Linux mremap function is used to remap the memory that was mapped using the mmap. please help me to clarify the following:

  1. If the mremap function failed, what is the status of old mapped memory?
  2. If the mremap function failed, is there any need of calling the function munmap?
  3. If the mremap function succeeded, is any previous data in the remapped memory?
akhil
  • 732
  • 3
  • 13
  • 1
    Did you read the [mremap(2)](http://man7.org/linux/man-pages/man2/mremap.2.html) man page? As most syscalls, unless documented otherwise, if it has failed "nothing has changed" – Basile Starynkevitch Apr 18 '13 at 12:03

1 Answers1

4

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

  1. If mremap fails, the old memory is just fine (just like realloc).

  2. If mremap fails, there's nothing to munmap (from this call, at least). See item 1.

  3. If mremap succeeds and has to move, the old memory is copied into the new (and the old one munmap'ped for you). If mremap is able to increase the size in-place, the memory is not moved and no new allocation is created.

luser droog
  • 18,988
  • 3
  • 53
  • 105
  • It seems some problems in 1 and 2. That is "the old memory is just fine" and "there's nothing to munmap". I think the base address of the remapped address is same, only extent or truncate the size of mapped region and if there is no continuous memory to remap, then it creates new mapped region. isn't it? – akhil Apr 18 '13 at 12:03
  • Pragmatically, the memory having moved is the case to worry about. If you do an 'mremap', all old pointers *may* be (and probably *are*) invalid. – luser droog Apr 18 '13 at 12:11